simple_queues 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +42 -0
- data/lib/simple_queues/redis.rb +16 -1
- data/lib/simple_queues/version.rb +1 -1
- data/spec/dequeueing_spec.rb +8 -0
- data/spec/multi_dequeue_spec.rb +56 -11
- metadata +2 -2
data/README.textile
CHANGED
@@ -62,6 +62,48 @@ while $running do
|
|
62
62
|
end
|
63
63
|
</code></pre>
|
64
64
|
|
65
|
+
Alternatively, the block you provide to #on_dequeue can accept two parameters and will be provided with the queue name:
|
66
|
+
|
67
|
+
<pre><code>require "simple_queues"
|
68
|
+
# Provides #underscore, #classify, #constantize and friends
|
69
|
+
require "active_support/inflector"
|
70
|
+
|
71
|
+
# Provides #seconds, #minutes and friends
|
72
|
+
require "active_support/core_ext/numeric/time"
|
73
|
+
|
74
|
+
Queues = SimpleQueues::Redis.new
|
75
|
+
|
76
|
+
class BaseWorker
|
77
|
+
def initialize(message)
|
78
|
+
@message = message
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class CrawlerControlWorker < BaseWorker
|
83
|
+
def run
|
84
|
+
$running = false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class PagesToCrawlWorker < BaseWorker
|
89
|
+
def run
|
90
|
+
# Crawl, do your own stuff here
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def handler(queue_name, message)
|
95
|
+
klass = (queue_name.to_s << "_worker").classify.constantize
|
96
|
+
klass.new(message).run
|
97
|
+
end
|
98
|
+
|
99
|
+
Queues.on_dequeue :crawler_controls, &method(:handler)
|
100
|
+
Queues.on_dequeue :pages_to_crawl, &method(:handler)
|
101
|
+
|
102
|
+
while $running
|
103
|
+
Queues.dequeue_with_timeout(30.seconds)
|
104
|
+
end
|
105
|
+
</code></pre>
|
106
|
+
|
65
107
|
h2. LICENSE
|
66
108
|
|
67
109
|
(The MIT License)
|
data/lib/simple_queues/redis.rb
CHANGED
@@ -29,7 +29,13 @@ module SimpleQueues
|
|
29
29
|
@queues = Hash.new
|
30
30
|
end
|
31
31
|
|
32
|
+
# Saves a block for later execution from #dequeue_with_timeout or #dequeue.
|
33
|
+
#
|
34
|
+
# When the block's arity is 1, only the message will be passed.
|
35
|
+
# When the block's arity is 2, the queue's name and the message will be passed along, in that order.
|
36
|
+
# When the block's arity is negative (accepts a variable number of arguments), SimpleQueues::Redis behaves as if the block's arity was 2.
|
32
37
|
def on_dequeue(queue_name, &block)
|
38
|
+
raise ArgumentError, "The provided block must accept at least one argument - #{block.inspect} accepts no arguments" if block.arity.zero?
|
33
39
|
@queues[q_name(queue_name)] = block
|
34
40
|
end
|
35
41
|
|
@@ -86,7 +92,16 @@ module SimpleQueues
|
|
86
92
|
raise ArgumentError, "Timeout must not be nil" if timeout.nil? || timeout.to_s.empty?
|
87
93
|
|
88
94
|
queue, result = @redis.blpop(*[@queues.keys, timeout.to_i].flatten)
|
89
|
-
|
95
|
+
if queue then
|
96
|
+
block = @queues.fetch(queue)
|
97
|
+
message = decode(result)
|
98
|
+
|
99
|
+
if block.arity == 1 then
|
100
|
+
block.call(message)
|
101
|
+
else
|
102
|
+
block.call(queue, message)
|
103
|
+
end
|
104
|
+
end
|
90
105
|
queue
|
91
106
|
when 2
|
92
107
|
queue_name, timeout = args.shift, args.shift
|
data/spec/dequeueing_spec.rb
CHANGED
@@ -23,6 +23,14 @@ describe SimpleQueues::Redis, "dequeue_blocking" do
|
|
23
23
|
lambda { queue.dequeue_blocking(nil) }.should raise_error(ArgumentError)
|
24
24
|
lambda { queue.dequeue_blocking("") }.should raise_error(ArgumentError)
|
25
25
|
end
|
26
|
+
|
27
|
+
it "should reraise underlying connection errors"
|
28
|
+
# Errno::ECONNREFUSED
|
29
|
+
# Errno::EAGAIN
|
30
|
+
|
31
|
+
context "given #exception_handler= is set with a block" do
|
32
|
+
it "should call the block to handle underlying connection exceptions"
|
33
|
+
end
|
26
34
|
end
|
27
35
|
|
28
36
|
describe SimpleQueues::Redis, "dequeue_with_timeout" do
|
data/spec/multi_dequeue_spec.rb
CHANGED
@@ -13,17 +13,23 @@ describe SimpleQueues::Redis, "multiple dequeue" do
|
|
13
13
|
redis.flushdb
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
context "#on_dequeue" do
|
17
|
+
it "should accept setting up a dequeue block" do
|
18
|
+
lambda do
|
19
|
+
queue.on_dequeue(:pages_to_crawl) {|message| message}
|
20
|
+
end.should_not raise_error
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
it "should accept setting up multiple dequeue block" do
|
24
|
+
lambda do
|
25
|
+
queue.on_dequeue(:pages_to_crawl) {|message| message}
|
26
|
+
queue.on_dequeue(:pages_to_analyze) {|message| message}
|
27
|
+
end.should_not raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should raise an ArgumentError when the block accepts no arguments" do
|
31
|
+
lambda { queue.on_dequeue(:a) { fail } }.should raise_error(ArgumentError)
|
32
|
+
end
|
27
33
|
end
|
28
34
|
|
29
35
|
context "#dequeue_with_timeout" do
|
@@ -39,7 +45,7 @@ describe SimpleQueues::Redis, "multiple dequeue" do
|
|
39
45
|
queue.dequeue_with_timeout(1).should be_nil
|
40
46
|
end
|
41
47
|
|
42
|
-
it "should
|
48
|
+
it "should send only the message when the block has an arity of 1" do
|
43
49
|
a, b = [], []
|
44
50
|
queue.on_dequeue(:a) {|message| a << message}
|
45
51
|
queue.on_dequeue(:b) {|message| b << message}
|
@@ -53,5 +59,44 @@ describe SimpleQueues::Redis, "multiple dequeue" do
|
|
53
59
|
a.should == [{"sent_to" => "a", "serial" => 1}, {"sent_to" => "a", "serial" => 2}]
|
54
60
|
b.should == [{"sent_to" => "b", "serial" => 1}]
|
55
61
|
end
|
62
|
+
|
63
|
+
it "should send the queue name and the message when the block has an arity of 2" do
|
64
|
+
received = []
|
65
|
+
queue.on_dequeue(:a) {|queue, message| received << [queue, message]}
|
66
|
+
queue.on_dequeue(:b) {|queue, message| received << [queue, message]}
|
67
|
+
|
68
|
+
queue.enqueue(:a, "a" => 1)
|
69
|
+
queue.enqueue(:b, "b" => 2)
|
70
|
+
|
71
|
+
2.times { queue.dequeue_with_timeout(1) }
|
72
|
+
|
73
|
+
received.should == [["a", {"a" => 1}], ["b", {"b" => 2}]]
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should send the queue name and the message when the block has an arity of -1" do
|
77
|
+
received = []
|
78
|
+
queue.on_dequeue(:a) {|*args| received << args}
|
79
|
+
queue.on_dequeue(:b) {|*args| received << args}
|
80
|
+
|
81
|
+
queue.enqueue(:a, "a" => 1)
|
82
|
+
queue.enqueue(:b, "b" => 2)
|
83
|
+
|
84
|
+
2.times { queue.dequeue_with_timeout(1) }
|
85
|
+
|
86
|
+
received.should == [["a", {"a" => 1}], ["b", {"b" => 2}]]
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should send the queue name and the message when the block has an arity of -2" do
|
90
|
+
received = []
|
91
|
+
queue.on_dequeue(:a) {|queue, *args| received << [queue, args]}
|
92
|
+
queue.on_dequeue(:b) {|queue, *args| received << [queue, args]}
|
93
|
+
|
94
|
+
queue.enqueue(:a, "a" => 1)
|
95
|
+
queue.enqueue(:b, "b" => 2)
|
96
|
+
|
97
|
+
2.times { queue.dequeue_with_timeout(1) }
|
98
|
+
|
99
|
+
received.should == [["a", [{"a" => 1}]], ["b", [{"b" => 2}]]]
|
100
|
+
end
|
56
101
|
end
|
57
102
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_queues
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Fran\xC3\xA7ois Beausoleil"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-31 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|