resque-uniqueue 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -16,26 +16,32 @@ module Resque
16
16
  redis.del("queue:#{queue}:uniqueue")
17
17
  end
18
18
 
19
- def push_unique(queue, item)
19
+ def push_unique(queue, item, time = Time.now.utc.to_i)
20
20
  confirm_unique_queue_validity(queue)
21
21
  watch_queue(queue)
22
22
  queue = "queue:#{queue}"
23
- redis.evalsha push_unique_eval_sha, [queue], [encode(item)]
23
+ redis.evalsha push_unique_eval_sha, [queue], [encode(item), time]
24
24
  end
25
25
 
26
26
  def pop_unique(queue)
27
27
  confirm_unique_queue_validity(queue)
28
28
  queue = "queue:#{queue}"
29
- decode redis.evalsha pop_unique_eval_sha, [queue]
29
+ results = redis.evalsha pop_unique_eval_sha, [queue]
30
+ return nil unless results[0]
31
+ job = decode results[0]
32
+ job['start_at'] = results[1].to_i
33
+ return job
30
34
  end
31
35
 
32
36
  def push_unique_eval_sha
33
37
  @push_unique_eval_sha ||= load_script <<-LUA
34
- local list_name = KEYS[1]
35
- local set_name = list_name..':uniqueue'
36
- local in_set = redis.call('sadd', set_name , ARGV[1])
38
+ local queue_name = KEYS[1]
39
+ local uniqueue_name = queue_name..':uniqueue'
40
+ local start_at_name = queue_name..':start_at'
41
+ local in_set = redis.call('sadd', uniqueue_name , ARGV[1])
37
42
  if in_set == 1 then
38
- return redis.call('rpush', list_name, ARGV[1])
43
+ redis.call('rpush', start_at_name, ARGV[2])
44
+ return redis.call('rpush', queue_name, ARGV[1])
39
45
  end
40
46
  return false
41
47
  LUA
@@ -43,21 +49,31 @@ module Resque
43
49
 
44
50
  def pop_unique_eval_sha
45
51
  @pop_unique_eval_sha ||= load_script <<-LUA
46
- local list_name = KEYS[1]
47
- local set_name = list_name..':uniqueue'
48
- local job = redis.call('lpop', list_name)
49
- redis.call('srem', set_name, job)
50
- return job
52
+ local queue_name = KEYS[1]
53
+ local uniqueue_name = queue_name..':uniqueue'
54
+ local start_at_name = queue_name..':start_at'
55
+ local results = {}
56
+ results[1] = redis.call('lpop', queue_name)
57
+ results[2] = redis.call('lpop', start_at_name)
58
+ redis.call('srem', uniqueue_name, results[1])
59
+ return results
51
60
  LUA
52
61
  end
53
62
 
54
63
  def queue_and_set_length_equal_eval_sha
55
64
  @queue_and_set_length_equal_eval_sha ||= load_script <<-LUA
56
- local list_name = KEYS[1]
57
- local set_name = list_name..':uniqueue'
58
- local list_size = redis.call('llen', list_name)
59
- local set_size = redis.call('scard', set_name)
60
- return list_size == set_size
65
+ local queue_name = KEYS[1]
66
+ local uniqueue_name = queue_name..':uniqueue'
67
+ local start_at_name = queue_name..':start_at'
68
+ local queue_size = redis.call('llen', queue_name)
69
+ local uniqueue_size = redis.call('scard', uniqueue_name)
70
+ local start_at_size = redis.call('llen', start_at_name)
71
+ if queue_size == uniqueue_size then
72
+ if queue_size == start_at_size then
73
+ return true
74
+ end
75
+ end
76
+ return false
61
77
  LUA
62
78
  end
63
79
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "resque-uniqueue"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aaron Scruggs"]
@@ -195,17 +195,28 @@ class TestResqueUniqueue < Test::Unit::TestCase
195
195
  Resque.redis.flushall
196
196
  end
197
197
 
198
- should "return true if set length and list length are the same" do
198
+ should "return true if queue, uniqueue & start_at length are the same" do
199
199
  Resque.redis.sadd "priority_10:uniqueue", "test"
200
200
  Resque.redis.rpush "priority_10", "test"
201
+ Resque.redis.rpush "priority_10:start_at", "test"
201
202
  Resque.confirm_unique_queue_validity("priority_10")
202
203
  end
203
204
 
204
- should 'raise exception if length of set and list differ' do
205
+ should 'raise exception if length of queue is off' do
205
206
  Resque.redis.rpush "priority_10", "test"
206
207
  assert_raises(RuntimeError){ Resque.confirm_unique_queue_validity("priority_10") }
207
208
  end
208
209
 
210
+ should 'raise exception if length of uniqueue is off' do
211
+ Resque.redis.sadd "priority_10:uniqueue", "test"
212
+ assert_raises(RuntimeError){ Resque.confirm_unique_queue_validity("priority_10") }
213
+ end
214
+
215
+ should 'raise exception if length of start_at is off' do
216
+ Resque.redis.rpush "priority_10:start_at", "test"
217
+ assert_raises(RuntimeError){ Resque.confirm_unique_queue_validity("priority_10") }
218
+ end
219
+
209
220
  end
210
221
 
211
222
  context "queue names" do
@@ -257,9 +268,11 @@ class TestResqueUniqueue < Test::Unit::TestCase
257
268
  Resque.push('priority_10', {'name' => 'bob'})
258
269
  assert_equal Resque.redis.llen('queue:priority_10'), 1
259
270
  assert_equal Resque.redis.scard('queue:priority_10:uniqueue'), 1
271
+ assert_equal Resque.redis.llen('queue:priority_10:start_at'), 1
260
272
  Resque.push('priority_10', {'name' => 'bob'})
261
273
  assert_equal Resque.redis.llen('queue:priority_10'), 1
262
274
  assert_equal Resque.redis.scard('queue:priority_10:uniqueue'), 1
275
+ assert_equal Resque.redis.llen('queue:priority_10:start_at'), 1
263
276
  end
264
277
 
265
278
  should "return queue length if unique job" do
@@ -272,6 +285,18 @@ class TestResqueUniqueue < Test::Unit::TestCase
272
285
  assert_nil Resque.push('priority_10', {'name' => 'bob'})
273
286
  end
274
287
 
288
+ should 'create start_at list if does not exist' do
289
+ refute Resque.redis.exists 'queue:priority_10:start_at'
290
+ Resque.push('priority_10', {'name' => 'bob'})
291
+ assert Resque.redis.exists 'queue:priority_10:start_at'
292
+ end
293
+
294
+ should 'add current time to start_at list' do
295
+ Resque.push_unique('priority_10', {'name' => 'bob'}, 1370213794)
296
+ assert_equal Resque.redis.llen('queue:priority_10:start_at'), 1
297
+ assert_equal Resque.redis.rpop('queue:priority_10:start_at').to_i, 1370213794
298
+ end
299
+
275
300
  end
276
301
 
277
302
  context "pop_unique" do
@@ -281,7 +306,8 @@ class TestResqueUniqueue < Test::Unit::TestCase
281
306
  job = Resque.pop('priority_20')
282
307
  Resque.push('priority_10', {'name' => 'bob'})
283
308
  uniqueue_job = Resque.pop('priority_10')
284
- assert_equal job, uniqueue_job
309
+ assert_equal job['name'], 'bob'
310
+ assert_equal job['name'], uniqueue_job['name']
285
311
  end
286
312
 
287
313
  should "return same item whether unique queue or not when queue is empty" do
@@ -299,6 +325,19 @@ class TestResqueUniqueue < Test::Unit::TestCase
299
325
  assert_equal Resque.redis.scard('queue:priority_10:uniqueue'), 0
300
326
  end
301
327
 
328
+ should 'remove current time from start_at list' do
329
+ Resque.push_unique('priority_10', {'name' => 'bob'}, 1370213794)
330
+ assert_equal Resque.redis.llen('queue:priority_10:start_at'), 1
331
+ Resque.pop('priority_10')
332
+ assert_equal Resque.redis.llen('queue:priority_10:start_at'), 0
333
+ end
334
+
335
+ should 'return start_at as part of job hash' do
336
+ Resque.push_unique('priority_10', {'name' => 'bob'}, 1370213794)
337
+ job = Resque.pop('priority_10')
338
+ assert_equal job['start_at'], 1370213794
339
+ end
340
+
302
341
  end
303
342
 
304
343
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-uniqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -158,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
158
  version: '0'
159
159
  segments:
160
160
  - 0
161
- hash: 2315830065208087025
161
+ hash: -2707351707401238460
162
162
  required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  none: false
164
164
  requirements: