resque-concurrent-restriction 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,6 +74,12 @@ module Resque
74
74
  "concurrent.queue.#{queue}.#{parts[2..-1].join('.')}"
75
75
  end
76
76
 
77
+ # The redis key used to store the aggregate number of jobs
78
+ # in restriction queues by queue name
79
+ def queue_count_key
80
+ "concurrent.queue_counts"
81
+ end
82
+
77
83
  def restriction_queue_availability_key(tracking_key)
78
84
  parts = tracking_key.split(".")
79
85
  "concurrent.queue_availability.#{parts[2..-1].join('.')}"
@@ -139,6 +145,7 @@ module Resque
139
145
  else raise "Invalid location to ConcurrentRestriction.push_to_restriction_queue"
140
146
  end
141
147
 
148
+ increment_queue_count(job.queue)
142
149
  update_queues_available(tracking_key, job.queue, :add)
143
150
  mark_runnable(tracking_key, false)
144
151
  end
@@ -154,6 +161,8 @@ module Resque
154
161
  clear_runnable(tracking_key, queue)
155
162
  end
156
163
 
164
+ decrement_queue_count(queue)
165
+
157
166
  # increment by one to indicate that we are running
158
167
  # do this before update_queues_available so that the current queue gets cleaned
159
168
  increment_running_count(tracking_key) if str
@@ -209,6 +218,26 @@ module Resque
209
218
  return restricted
210
219
  end
211
220
 
221
+ def increment_queue_count(queue, by=1)
222
+ value = Resque.redis.hincrby(queue_count_key, queue, by)
223
+ return value
224
+ end
225
+
226
+ def decrement_queue_count(queue, by=1)
227
+ value = Resque.redis.hincrby(queue_count_key, queue, -by)
228
+ return value
229
+ end
230
+
231
+ def queue_counts
232
+ value = Resque.redis.hgetall(queue_count_key)
233
+ value = Hash[*value.collect {|k, v| [k, v.to_i] }.flatten]
234
+ return value
235
+ end
236
+
237
+ def set_queue_count(queue, count)
238
+ Resque.redis.hset(queue_count_key, queue, count)
239
+ end
240
+
212
241
  def runnable?(tracking_key, queue)
213
242
  Resque.redis.sismember(runnables_key(queue), tracking_key)
214
243
  end
@@ -375,14 +404,18 @@ module Resque
375
404
  runnable_keys = Resque.redis.keys("concurrent.runnable*")
376
405
  Resque.redis.del(*runnable_keys) if runnable_keys.size > 0
377
406
 
407
+ Resque.redis.del(queue_count_key)
378
408
  queues_enabled = 0
379
409
  queue_keys = Resque.redis.keys("concurrent.queue.*")
380
410
  queue_keys.each do |k|
381
- if Resque.redis.llen(k) > 0
411
+ len = Resque.redis.llen(k)
412
+ if len > 0
382
413
  parts = k.split(".")
383
414
  queue = parts[2]
384
415
  ident = parts[3..-1].join('.')
385
416
  tracking_key = "concurrent.tracking.#{ident}"
417
+
418
+ increment_queue_count(queue, len)
386
419
  update_queues_available(tracking_key, queue, :add)
387
420
  mark_runnable(tracking_key, true)
388
421
  queues_enabled += 1
@@ -393,46 +426,46 @@ module Resque
393
426
 
394
427
  end
395
428
 
396
- def stats
397
- results = {}
429
+ def stats(extended=false)
430
+ result = {}
398
431
 
399
- queue_keys = Resque.redis.keys("concurrent.queue.*")
432
+ result[:queues] = queue_counts
400
433
 
401
- queue_sizes = {}
402
- ident_sizes = {}
403
- queue_keys.each do |k|
404
- parts = k.split(".")
405
- ident = parts[3..-1].join(".")
406
- queue_name = parts[2]
407
- size = Resque.redis.llen(k)
408
- queue_sizes[queue_name] ||= 0
409
- queue_sizes[queue_name] += size
410
- ident_sizes[ident] ||= 0
411
- ident_sizes[ident] += size
412
- end
434
+ if extended
435
+ ident_sizes = {}
436
+ queue_keys = Resque.redis.keys("concurrent.queue.*")
437
+ queue_keys.each do |k|
438
+ parts = k.split(".")
439
+ ident = parts[3..-1].join(".")
440
+ queue_name = parts[2]
441
+ size = Resque.redis.llen(k)
442
+ ident_sizes[ident] ||= {}
443
+ ident_sizes[ident][queue_name] ||= 0
444
+ ident_sizes[ident][queue_name] += size
445
+ end
413
446
 
414
- count_keys = Resque.redis.keys("concurrent.count.*")
415
- running_counts = {}
416
- count_keys.each do |k|
417
- parts = k.split(".")
418
- ident = parts[2..-1].join(".")
419
- running_counts[ident] = Resque.redis.get(k).to_i
447
+ count_keys = Resque.redis.keys("concurrent.count.*")
448
+ running_counts = {}
449
+ count_keys.each do |k|
450
+ parts = k.split(".")
451
+ ident = parts[2..-1].join(".")
452
+ ident_sizes[ident] ||= {}
453
+ ident_sizes[ident]["running"] = Resque.redis.get(k).to_i
454
+ end
455
+
456
+ result[:identifiers] = ident_sizes
457
+ else
458
+ result[:identifiers] = {}
420
459
  end
421
460
 
461
+
422
462
  lock_keys = Resque.redis.keys("concurrent.lock.*")
423
- lock_count = lock_keys.size
463
+ result[:lock_count] = lock_keys.size
424
464
 
425
465
  runnable_count = Resque.redis.scard(runnables_key)
466
+ result[:runnable_count] = runnable_count
426
467
 
427
- return {
428
- :queue_totals => {
429
- :by_queue_name => queue_sizes,
430
- :by_identifier => ident_sizes
431
- },
432
- :running_counts => running_counts,
433
- :lock_count => lock_count,
434
- :runnable_count => runnable_count,
435
- }
468
+ return result
436
469
 
437
470
  end
438
471
 
@@ -1,7 +1,7 @@
1
1
  module Resque
2
2
  module Plugins
3
3
  module ConcurrentRestriction
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  end
6
6
  end
7
7
  end
@@ -295,6 +295,25 @@ describe Resque::Plugins::ConcurrentRestriction do
295
295
  ConcurrentRestrictionJob.runnables("somequeue2").sort.should == []
296
296
  end
297
297
 
298
+ it "should track queue_counts" do
299
+ ConcurrentRestrictionJob.queue_counts.should == {}
300
+
301
+ job1 = Resque::Job.new("somequeue", {"class" => "ConcurrentRestrictionJob", "args" => [1]})
302
+ job2 = Resque::Job.new("somequeue", {"class" => "ConcurrentRestrictionJob", "args" => [2]})
303
+ job3 = Resque::Job.new("somequeue2", {"class" => "ConcurrentRestrictionJob", "args" => [3]})
304
+
305
+ ConcurrentRestrictionJob.push_to_restriction_queue(job1)
306
+ ConcurrentRestrictionJob.push_to_restriction_queue(job2)
307
+ ConcurrentRestrictionJob.push_to_restriction_queue(job3)
308
+ ConcurrentRestrictionJob.queue_counts.should == {"somequeue"=>2, "somequeue2"=>1}
309
+
310
+ ConcurrentRestrictionJob.pop_from_restriction_queue(ConcurrentRestrictionJob.tracking_key, "somequeue")
311
+ ConcurrentRestrictionJob.queue_counts.should == {"somequeue"=>1, "somequeue2"=>1}
312
+
313
+ ConcurrentRestrictionJob.pop_from_restriction_queue(ConcurrentRestrictionJob.tracking_key, "somequeue")
314
+ ConcurrentRestrictionJob.pop_from_restriction_queue(ConcurrentRestrictionJob.tracking_key, "somequeue2")
315
+ ConcurrentRestrictionJob.queue_counts.should == {"somequeue"=>0, "somequeue2"=>0}
316
+ end
298
317
  end
299
318
 
300
319
  context "#stash_if_restricted" do
@@ -456,6 +475,8 @@ describe Resque::Plugins::ConcurrentRestriction do
456
475
 
457
476
  IdentifiedRestrictionJob.runnables.sort.should == [ConcurrentRestrictionJob.tracking_key, IdentifiedRestrictionJob.tracking_key(1), IdentifiedRestrictionJob.tracking_key(2)]
458
477
 
478
+ ConcurrentRestrictionJob.queue_counts.should == {"queue1"=>2, "queue2"=>2, "queue3"=>1}
479
+
459
480
  end
460
481
 
461
482
  end
@@ -464,11 +485,21 @@ describe Resque::Plugins::ConcurrentRestriction do
464
485
 
465
486
  it "should have blank info when nothing going on" do
466
487
  stats = ConcurrentRestrictionJob.stats
467
- stats[:queue_totals][:by_queue_name].should == {}
468
- stats[:queue_totals][:by_identifier].should == {}
469
- stats[:running_counts].should == {}
488
+ stats[:queues].should == {}
489
+ stats[:identifiers].should == {}
470
490
  stats[:lock_count].should == 0
471
491
  stats[:runnable_count].should == 0
492
+ estats = ConcurrentRestrictionJob.stats(true)
493
+ estats.should == stats
494
+ end
495
+
496
+ it "should not track identifiers when not extended" do
497
+ job1 = Resque::Job.new("queue1", {"class" => "IdentifiedRestrictionJob", "args" => [1]})
498
+ IdentifiedRestrictionJob.push_to_restriction_queue(job1)
499
+ IdentifiedRestrictionJob.set_running_count(IdentifiedRestrictionJob.tracking_key(1), 2)
500
+
501
+ stats = IdentifiedRestrictionJob.stats
502
+ stats[:identifiers].should == {}
472
503
  end
473
504
 
474
505
  it "should track queue_totals" do
@@ -484,17 +515,18 @@ describe Resque::Plugins::ConcurrentRestriction do
484
515
  IdentifiedRestrictionJob.push_to_restriction_queue(job4)
485
516
  ConcurrentRestrictionJob.push_to_restriction_queue(job5)
486
517
 
487
- stats = IdentifiedRestrictionJob.stats
488
- stats[:queue_totals][:by_queue_name].should == {"queue1" => 2, "queue2" => 2, "queue3" => 1}
489
- stats[:queue_totals][:by_identifier].should == {"IdentifiedRestrictionJob.1" => 2, "IdentifiedRestrictionJob.2" => 2, "ConcurrentRestrictionJob" => 1}
518
+ stats = IdentifiedRestrictionJob.stats(true)
519
+ stats[:queues].should == {"queue1" => 2, "queue2" => 2, "queue3" => 1}
520
+
521
+ stats[:identifiers].should == {"IdentifiedRestrictionJob.1"=>{"queue1"=>1, "queue2"=>1}, "IdentifiedRestrictionJob.2"=>{"queue1"=>1, "queue2"=>1}, "ConcurrentRestrictionJob"=>{"queue3"=>1}}
490
522
  end
491
523
 
492
524
  it "should track running_counts" do
493
525
  IdentifiedRestrictionJob.set_running_count(IdentifiedRestrictionJob.tracking_key(1), 2)
494
526
  IdentifiedRestrictionJob.set_running_count(IdentifiedRestrictionJob.tracking_key(2), 3)
495
527
  ConcurrentRestrictionJob.set_running_count(ConcurrentRestrictionJob.tracking_key, 4)
496
- stats = IdentifiedRestrictionJob.stats
497
- stats[:running_counts].should == {"IdentifiedRestrictionJob.1" => 2, "IdentifiedRestrictionJob.2" => 3, "ConcurrentRestrictionJob" => 4}
528
+ stats = IdentifiedRestrictionJob.stats(true)
529
+ stats[:identifiers].should == {"IdentifiedRestrictionJob.1"=>{"running"=>2}, "IdentifiedRestrictionJob.2"=>{"running"=>3}, "ConcurrentRestrictionJob"=>{"running"=>4}}
498
530
  end
499
531
 
500
532
  it "should track lock_count" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: resque-concurrent-restriction
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.1
5
+ version: 0.5.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Matt Conway
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-08 00:00:00 -04:00
13
+ date: 2011-04-11 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency