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-04-03 23:19:29 -04:00
|
|
|
# Timeout for Redis#blpop.
|
2012-03-25 22:52:15 -04:00
|
|
|
TIMEOUT = 1
|
|
|
|
|
2012-03-24 16:28:18 -04:00
|
|
|
def initialize(mgr, queues)
|
|
|
|
@mgr = mgr
|
2012-04-03 23:38:03 -04:00
|
|
|
@queues = queues.map { |q| "queue:#{q}" }
|
2012-04-04 02:35:46 -04:00
|
|
|
@unique_queues = @queues.uniq
|
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
|
2012-03-30 23:59:08 -04:00
|
|
|
queue = nil
|
2012-03-24 16:28:18 -04:00
|
|
|
msg = nil
|
2012-04-03 23:19:29 -04:00
|
|
|
Sidekiq.redis { |conn| queue, msg = conn.blpop(*queues_cmd) }
|
2012-03-24 16:28:18 -04:00
|
|
|
|
2012-03-30 23:59:08 -04:00
|
|
|
if msg
|
2012-04-03 10:09:43 -04:00
|
|
|
@mgr.assign!(msg, queue.gsub(/.*queue:/, ''))
|
2012-03-30 23:59:08 -04:00
|
|
|
else
|
|
|
|
after(0) { fetch }
|
|
|
|
end
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-03 23:19:29 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Creating the Redis#blpop command takes into account any
|
|
|
|
# configured queue weights. By default Redis#blpop returns
|
|
|
|
# data from the first queue that has pending elements. We
|
|
|
|
# recreate the queue command each time we invoke Redis#blpop
|
|
|
|
# to honor weights and avoid queue starvation.
|
|
|
|
def queues_cmd
|
2012-04-04 02:35:46 -04:00
|
|
|
queues = @queues.sample(@unique_queues.size).uniq
|
|
|
|
queues.concat(@unique_queues - queues)
|
|
|
|
queues << TIMEOUT
|
2012-04-03 23:19:29 -04:00
|
|
|
end
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
end
|