simple_queues 1.0.4 → 1.1.0
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.
- data/README.textile +1 -1
- data/lib/simple_queues/redis.rb +26 -7
- data/lib/simple_queues/version.rb +1 -1
- data/simple_queues.gemspec +1 -0
- metadata +12 -1
data/README.textile
CHANGED
@@ -36,7 +36,7 @@ end
|
|
36
36
|
|
37
37
|
h1. Multiple Queues
|
38
38
|
|
39
|
-
Sometimes, you want to dequeue from multiple queues simultaneously, and react appropriately. When that happens, you need to use
|
39
|
+
Sometimes, you want to dequeue from multiple queues simultaneously, and react appropriately. When that happens, you need to use <code>#on_dequeue</code>:
|
40
40
|
|
41
41
|
<pre><code>require "simple_queues"
|
42
42
|
Queues = SimpleQueues::Redis.new
|
data/lib/simple_queues/redis.rb
CHANGED
@@ -7,7 +7,12 @@ module SimpleQueues
|
|
7
7
|
# Messages are enqueued to the right, dequeued from the left - thus the most recent messages are at the end of the list.
|
8
8
|
class Redis
|
9
9
|
def initialize(redis = ::Redis.new)
|
10
|
-
@redis
|
10
|
+
@redis = redis
|
11
|
+
@queues = Hash.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_dequeue(queue_name, &block)
|
15
|
+
@queues[q_name(queue_name)] = block
|
11
16
|
end
|
12
17
|
|
13
18
|
def serialize(message)
|
@@ -51,13 +56,27 @@ module SimpleQueues
|
|
51
56
|
|
52
57
|
# Dequeues a message, or returns +nil+ if the timeout is exceeded.
|
53
58
|
#
|
54
|
-
# @param queue_name [String, Symbol] The queue name to read from.
|
59
|
+
# @param queue_name [String, Symbol] The queue name to read from. Optional if you used #on_dequeue.
|
55
60
|
# @param timeout [#to_f] The number of seconds to wait before returning nil.
|
56
|
-
# @return [String, nil]
|
57
|
-
# @raise ArgumentError If +queue_name+ is
|
58
|
-
def dequeue_with_timeout(
|
59
|
-
|
60
|
-
|
61
|
+
# @return [String, nil] When given two arguments, returns the message, or nil if the timeout was exceeded. When given a timeout only, always returns nil.
|
62
|
+
# @raise ArgumentError If +queue_name+ is absent and no #on_dequeue blocks were added.
|
63
|
+
def dequeue_with_timeout(*args)
|
64
|
+
case args.length
|
65
|
+
when 1 # Timeout only
|
66
|
+
timeout = args.shift
|
67
|
+
raise ArgumentError, "Timeout must not be nil" if timeout.nil? || timeout.to_s.empty?
|
68
|
+
|
69
|
+
queue, result = @redis.blpop(*@queues.keys, timeout.to_i)
|
70
|
+
@queues.fetch(queue).call(deserialize(result)) if queue
|
71
|
+
queue
|
72
|
+
when 2
|
73
|
+
queue_name, timeout = args.shift, args.shift
|
74
|
+
_, result = @redis.blpop(q_name(queue_name), timeout.to_i)
|
75
|
+
deserialize(result)
|
76
|
+
else
|
77
|
+
raise "NOT DONE"
|
78
|
+
end
|
79
|
+
|
61
80
|
end
|
62
81
|
|
63
82
|
private
|
data/simple_queues.gemspec
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_queues
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0
|
5
|
+
version: 1.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Fran\xC3\xA7ois Beausoleil"
|
@@ -68,6 +68,17 @@ dependencies:
|
|
68
68
|
version: "0"
|
69
69
|
type: :development
|
70
70
|
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: ruby-debug19
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id006
|
71
82
|
description: Program to an interface, not an implementation - hides Redis (used as a queue) behind a simple interface
|
72
83
|
email:
|
73
84
|
- francois@teksol.info
|