peons 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/LICENSE +19 -0
  2. data/README.markdown +60 -0
  3. data/lib/peons.rb +5 -5
  4. data/test/peons_test.rb +13 -1
  5. metadata +5 -3
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ Peons
2
+ =====
3
+
4
+ Peons
5
+
6
+ _n. Hard working dependable slaves, with a keen attention to mining your gold._
7
+
8
+ Quick Start
9
+ -----------
10
+
11
+ It's easy to get started with Peon.
12
+
13
+ $ [sudo] gem install peons
14
+ $ redis-server # Assuming you have redis-server installed and in your path
15
+
16
+ require "peons"
17
+
18
+ Peons[:fortress].push "1"
19
+ Peons[:fortress].push "2"
20
+ Peons[:fortress].push "3"
21
+ Peons[:fortress].push "4"
22
+
23
+ output = []
24
+
25
+ Peons[:fortress].each do |item|
26
+ output << item
27
+ end
28
+
29
+ output == ["1", "2", "3", "4"]
30
+ # => true
31
+
32
+ ### Atomic Pops
33
+
34
+ You can also do atomic pops, if you'd prefer that over looping your entire
35
+ queue.
36
+
37
+ Peons[:fortress].push "1"
38
+
39
+ popped = nil
40
+
41
+ Peons[:fortress].pop do |item|
42
+ popped = item
43
+ end
44
+
45
+ popped == "1"
46
+ # => true
47
+
48
+ ### Connecting to a different Redis connection
49
+
50
+ For cases where you want to connect to a Redis connection which isn't the
51
+ default, simply assign like so:
52
+
53
+ Peons.redis = Redis.connect(:url => "redis://127.0.0.1:22222/1")
54
+
55
+ ### TODO
56
+
57
+ 1. Testing for thread safety.
58
+ 2. A sinatra web interface to display all queues
59
+ 3. General hardening.
60
+
@@ -2,7 +2,7 @@ require "redis"
2
2
  require "nest"
3
3
 
4
4
  module Peons
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
 
7
7
  class Queue < Nest
8
8
  alias :push :lpush
@@ -15,7 +15,7 @@ module Peons
15
15
  rescue Exception => e
16
16
  fail(e, item)
17
17
  ensure
18
- backup.lrem 1, item
18
+ backup.del
19
19
  end
20
20
  end
21
21
 
@@ -28,7 +28,7 @@ module Peons
28
28
  end
29
29
 
30
30
  def backup
31
- self[:backup]
31
+ self[Process.pid][:backup]
32
32
  end
33
33
 
34
34
  def errors
@@ -49,11 +49,11 @@ module Peons
49
49
  end
50
50
 
51
51
  def self.[](key)
52
- Queue.new(key, redis)
52
+ Queue.new(:peons, redis)[key]
53
53
  end
54
54
 
55
55
  def self.redis
56
- threaded[:redis] ||= Redis.new
56
+ threaded[:redis] ||= Redis.connect
57
57
  end
58
58
 
59
59
  def self.redis=(redis)
@@ -30,6 +30,7 @@ test "popping from a queue with one element" do |q|
30
30
  end
31
31
 
32
32
  assert "1" == popped
33
+ assert 0 == q.llen
33
34
  assert 0 == q.backup.llen
34
35
  end
35
36
 
@@ -44,6 +45,7 @@ test "popping from a queue with two distinct elements" do |q|
44
45
  end
45
46
 
46
47
  assert "1" == popped
48
+ assert 1 == q.llen
47
49
  assert 0 == q.backup.llen
48
50
  end
49
51
 
@@ -58,6 +60,7 @@ test "popping from a queue with two equal elements" do |q|
58
60
  end
59
61
 
60
62
  assert "1" == popped
63
+ assert 1 == q.llen
61
64
  assert 0 == q.backup.llen
62
65
  end
63
66
 
@@ -71,7 +74,6 @@ test "popping and producing an error inside the block" do |q|
71
74
 
72
75
  assert 1 == q.llen
73
76
  assert 0 == q.backup.llen
74
-
75
77
  assert 1 == q.errors.llen
76
78
 
77
79
  assert q.errors.lrange(0, -1).grep(/1 => #<RuntimeError: Fabrication>/).any?
@@ -89,5 +91,15 @@ test "looping against the queue" do |q|
89
91
  end
90
92
 
91
93
  assert ["1", "2", "3"] == output
94
+
95
+ assert 0 == q.llen
96
+ assert 0 == q.backup.llen
97
+ assert 0 == q.errors.llen
98
+ end
99
+
100
+ test "all queues are namespaced" do
101
+ assert "peons:foo" == Peons[:foo]
102
+ assert "peons:bar" == Peons[:bar]
103
+ assert "peons:baz" == Peons[:baz]
92
104
  end
93
105
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-01 00:00:00 +08:00
17
+ date: 2010-10-05 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -66,6 +66,8 @@ extra_rdoc_files: []
66
66
 
67
67
  files:
68
68
  - lib/peons.rb
69
+ - README.markdown
70
+ - LICENSE
69
71
  - Rakefile
70
72
  - test/helper.rb
71
73
  - test/peons_test.rb