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 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 the <code>#on_dequeue</code> method:
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
@@ -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 = 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] The first message in the queue, or nil if the timeout was exceeded.
57
- # @raise ArgumentError If +queue_name+ is nil or the empty String.
58
- def dequeue_with_timeout(queue_name, timeout)
59
- queue, result = @redis.blpop(q_name(queue_name), timeout.to_i)
60
- deserialize(result)
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
@@ -1,3 +1,3 @@
1
1
  module SimpleQueues
2
- VERSION = "1.0.4"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'yard'
24
24
  s.add_development_dependency 'RedCloth'
25
+ s.add_development_dependency 'ruby-debug19'
25
26
  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.0.4
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