redis-queue 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -43,6 +43,17 @@ end
43
43
  YourTask.new(message).perform.succeed?
44
44
  end
45
45
 
46
+ # Process messages with timeout (starting from version 0.0.3)
47
+ # Wait for 15 seconds for new messages, then exit
48
+ queue.process(false, 15) do |message|
49
+ puts "'#{message}'"
50
+ end
51
+
52
+ # Process messages in a non blocking-way
53
+ # A soon as the queue is empty, the block will exit
54
+ queue.process(true) do |message|
55
+ puts "'#{message}'"
56
+ end
46
57
  ```
47
58
  Contributing to redis-queue
48
59
  ----------------
@@ -0,0 +1,25 @@
1
+ require "redis-queue"
2
+ require "thread"
3
+
4
+ redis = Redis.new
5
+ #Create a queue that will listen for a new element for 10 seconds
6
+ queue = Redis::Queue.new('__test', 'bp__test', :redis => redis, :timeout => 10)
7
+ queue.clear true
8
+
9
+ 100.times { queue << rand(100) }
10
+
11
+ # Simulate a delayed insert
12
+ t = Thread.new do
13
+ sleep 3
14
+ # We should use a second connection here since the first one is busy
15
+ # on a blocking call
16
+ _redis = Redis.new
17
+ _queue = Redis::Queue.new('__test', 'bp__test', :redis => _redis)
18
+ 100.times { _queue << "e_#{rand(100)}" }
19
+ end
20
+
21
+ #When all elements are dequeud, process method will wait for 10 secods before exit
22
+ queue.process do |message|
23
+ puts "'#{message}'"
24
+ end
25
+ t.join
@@ -0,0 +1,10 @@
1
+ require "redis-queue"
2
+
3
+ redis = Redis.new
4
+
5
+ queue = Redis::Queue.new('__test', 'bp__test', :redis => redis)
6
+ queue.clear true
7
+
8
+ 100.times { queue << rand(100) }
9
+
10
+ queue.process(true) {|m| puts m}
@@ -1,7 +1,7 @@
1
1
  class Redis
2
2
  class Queue
3
3
 
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
 
6
6
  def self.version
7
7
  "redis-queue version #{VERSION}"
@@ -16,6 +16,7 @@ class Redis
16
16
  @queue_name = queue_name
17
17
  @process_queue_name = process_queue_name
18
18
  @last_message = nil
19
+ @timeout = options[:timeout] ||= 0
19
20
  end
20
21
 
21
22
  def length
@@ -39,7 +40,7 @@ class Redis
39
40
  if non_block
40
41
  @last_message = @redis.rpoplpush(@queue_name,@process_queue_name)
41
42
  else
42
- @last_message = @redis.brpoplpush(@queue_name,@process_queue_name)
43
+ @last_message = @redis.brpoplpush(@queue_name,@process_queue_name, @timeout)
43
44
  end
44
45
  @last_message
45
46
  end
@@ -48,11 +49,15 @@ class Redis
48
49
  @redis.lrem(@process_queue_name, 0, @last_message)
49
50
  end
50
51
 
51
- def process(non_block=false)
52
- while message=pop(non_block)
52
+ def process(non_block=false, timeout = nil)
53
+ @timeout = timeout unless timeout.nil?
54
+ loop do
55
+ message = pop(non_block)
53
56
  ret = yield message if block_given?
54
57
  commit if ret
58
+ break if message.nil? || (non_block && empty?)
55
59
  end
60
+
56
61
  end
57
62
 
58
63
  def refill
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require "timeout"
2
3
 
3
4
  describe Redis::Queue do
4
5
  before(:all) do
@@ -83,4 +84,38 @@ describe Redis::Queue do
83
84
  @redis.llen('bp__test').should be == 0
84
85
  end
85
86
 
87
+ it 'should work with the timeout parameters' do
88
+ @queue.clear(true)
89
+ 100.times { @queue << rand(100) }
90
+ is_ok = true
91
+ begin
92
+ Timeout::timeout(3) {
93
+ @queue.process(false, 2) {|m| true}
94
+ }
95
+ rescue Timeout::Error => e
96
+ is_ok = false
97
+ end
98
+
99
+ is_ok.should be_true
100
+
101
+ end
102
+
103
+ it 'should honor the timeout param in the initializer' do
104
+ redis = Redis.new
105
+ queue = Redis::Queue.new('__test_tm', 'bp__test_tm', :redis => redis, :timeout => 2)
106
+ queue.clear true
107
+
108
+ is_ok = true
109
+ begin
110
+ Timeout::timeout(4) {
111
+ queue.pop
112
+ }
113
+ rescue Timeout::Error => e
114
+ puts "#{e}"
115
+ is_ok = false
116
+ end
117
+ queue.clear
118
+ is_ok.should be_true
119
+ end
120
+
86
121
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-03 00:00:00.000000000 Z
12
+ date: 2013-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -74,6 +74,8 @@ files:
74
74
  - LICENSE.txt
75
75
  - README.md
76
76
  - Rakefile
77
+ - example/blocking.rb
78
+ - example/non-blocking.rb
77
79
  - lib/redis-queue.rb
78
80
  - lib/redis/queue.rb
79
81
  - redis-queue.gemspec