diff options
author | Steve Kemp <steve@steve.org.uk> | 2015-12-18 13:29:28 +0200 |
---|---|---|
committer | Steve Kemp <steve@steve.org.uk> | 2015-12-18 13:29:28 +0200 |
commit | a1f292b0649376585281355bf6938598a730766a (patch) | |
tree | bb9e7273fc7487af3e0a8f6f38983bc23b47b5c2 | |
parent | 80489e3ffe9b7ba139043cef5b4a7f08a018e4d7 (diff) |
Updated to revert to a set with no ordering.
This is more reliable, albeit potentially racy and with the failure
case that a job might be readded twice.
-rw-r--r-- | lib/custodian/queue.rb | 39 |
1 files changed, 7 insertions, 32 deletions
diff --git a/lib/custodian/queue.rb b/lib/custodian/queue.rb index 969ba8e..4e0a385 100644 --- a/lib/custodian/queue.rb +++ b/lib/custodian/queue.rb @@ -82,26 +82,15 @@ module Custodian loop do - # Get the next job from the queue. - # - # NOTE: This returns an array - but the array will have only - # one element because we're picking from element 0 with a range - # of 0 - which really means 1,1. - # - job = @redis.ZREVRANGE('custodian_queue', '0', '0') + # Get a random job + job = @redis.spop('custodian_queue') - if ! job.empty? - - # We only have one entry in our array - job = job[0] - - # Remove from the queue - @redis.zrem('custodian_queue', job ); + # If that worked return it + if !job.nil? return job else sleep(timeout) end - end end @@ -111,23 +100,9 @@ module Custodian # def add(job_string) - # - # We need to build a "score" for this job - the score - # will be used for ordering by Redis. - # - # We don't care what order the jobs are running in, however - # we do care that this the order is always the same. - # - # On that basis we need to create a score for the string which - # will always be the same, and will always be a number. - # - # We'll sum up the ASCII values of each character in the test - # which gives us a "number" which that should be consistent for - # each distinct-test. - # - # - score = Time.now.to_i - @redis.zadd('custodian_queue', score, job_string) + # Add unless already present + @redis.sadd('custodian_queue', test) unless + ( @redis.sismember( 'custodian_queue', test ) ) end |