From 8312615574ab9b0d72b94bf6fe9dba13eabc0b6f Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 18 Feb 2015 04:10:36 +0000 Subject: Updated the way we wait for jobs. The new approach uses the redis gems timeout functionality and ensures we never return a null-job. Instead we timeout and repeat with a stalling-sleep in the way. This closes #9553. --- lib/custodian/queue.rb | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/custodian/queue.rb b/lib/custodian/queue.rb index 9a7d90f..c48bba3 100644 --- a/lib/custodian/queue.rb +++ b/lib/custodian/queue.rb @@ -87,27 +87,19 @@ end # # Fetch a job from the queue. # - # The timeout is used to specify the period we wait for a new job. + # The timeout is used to specify the period we wait for a new job, and + # we pause that same period between fetches. # - def fetch(timeout) + def fetch(timeout = 1) job = nil - timeout ||= 0 - # - # Don't melt the CPU. - # - sleep_interval = 0.5 + while( 1 ) - loop do - job = @redis.lpop( "queue" ) - break if job or timeout < 0 + foo, job = @redis.blpop( "queue", :timeout => timeout ) + return job if ( job ) - sleep( sleep_interval ) - - timeout -= sleep_interval + sleep( timeout ) end - - return( job ) end # -- cgit v1.2.1