2012-03-24 16:28:18 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
require 'celluloid'
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
##
|
|
|
|
# The Fetcher blocks on Redis, waiting for a message to process
|
|
|
|
# from the queues. It gets the message and hands it to the Manager
|
|
|
|
# to assign to a ready Processor.
|
|
|
|
class Fetcher
|
|
|
|
include Celluloid
|
|
|
|
include Sidekiq::Util
|
|
|
|
|
2012-03-25 22:52:15 -04:00
|
|
|
TIMEOUT = 1
|
|
|
|
|
2012-03-24 16:28:18 -04:00
|
|
|
def initialize(mgr, queues)
|
|
|
|
@cmd = queues.map { |q| "queue:#{q}" }
|
|
|
|
@mgr = mgr
|
|
|
|
|
|
|
|
# One second timeout for blpop.
|
2012-03-25 22:52:15 -04:00
|
|
|
@cmd << TIMEOUT
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
|
2012-03-25 22:52:15 -04:00
|
|
|
# Fetching is straightforward: the Manager makes a fetch
|
|
|
|
# request for each idle processor when Sidekiq starts and
|
|
|
|
# then issues a new fetch request every time a Processor
|
|
|
|
# finishes a message.
|
|
|
|
#
|
|
|
|
# Because we have to shut down cleanly, we can't block
|
|
|
|
# forever and we can't loop forever. Instead we reschedule
|
|
|
|
# a new fetch if the current fetch turned up nothing.
|
2012-03-24 16:28:18 -04:00
|
|
|
def fetch
|
|
|
|
watchdog('Fetcher#fetch died') do
|
|
|
|
|
|
|
|
msg = nil
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
(queue, msg) = conn.blpop *@cmd
|
2012-03-25 22:52:15 -04:00
|
|
|
puts 'All quiet' unless msg
|
2012-03-24 16:28:18 -04:00
|
|
|
@mgr.assign! msg, queue.gsub(/\Aqueue:/, '') if msg
|
|
|
|
end
|
2012-03-25 22:52:15 -04:00
|
|
|
after(0) { fetch } if !msg
|
2012-03-24 16:28:18 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|