2012-03-24 16:28:18 -04:00
|
|
|
require 'sidekiq'
|
2014-04-10 11:50:54 -04:00
|
|
|
require 'sidekiq/util'
|
2013-05-10 23:43:53 -04:00
|
|
|
require 'sidekiq/actor'
|
2012-03-24 16:28:18 -04:00
|
|
|
|
|
|
|
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
|
2013-05-10 23:43:53 -04:00
|
|
|
include Util
|
|
|
|
include Actor
|
2012-03-24 16:28:18 -04:00
|
|
|
|
2012-03-25 22:52:15 -04:00
|
|
|
TIMEOUT = 1
|
|
|
|
|
2014-02-01 18:04:20 -05:00
|
|
|
attr_reader :down
|
|
|
|
|
2013-01-06 00:17:08 -05:00
|
|
|
def initialize(mgr, options)
|
2013-05-10 19:37:11 -04:00
|
|
|
@down = nil
|
2012-03-24 16:28:18 -04:00
|
|
|
@mgr = mgr
|
2013-02-05 18:11:41 -05:00
|
|
|
@strategy = Fetcher.strategy.new(options)
|
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-05-12 00:25:38 -04:00
|
|
|
return if Sidekiq::Fetcher.done?
|
|
|
|
|
2012-04-18 19:31:17 -04:00
|
|
|
begin
|
2013-01-06 00:17:08 -05:00
|
|
|
work = @strategy.retrieve_work
|
2013-05-10 19:37:11 -04:00
|
|
|
::Sidekiq.logger.info("Redis is online, #{Time.now.to_f - @down.to_f} sec downtime") if @down
|
|
|
|
@down = nil
|
2012-03-24 16:28:18 -04:00
|
|
|
|
2013-01-06 00:17:08 -05:00
|
|
|
if work
|
|
|
|
@mgr.async.assign(work)
|
2012-04-18 19:31:17 -04:00
|
|
|
else
|
|
|
|
after(0) { fetch }
|
|
|
|
end
|
|
|
|
rescue => ex
|
2013-11-09 22:48:54 -05:00
|
|
|
handle_fetch_exception(ex)
|
2013-05-10 20:19:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-01 18:04:20 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def pause
|
|
|
|
sleep(TIMEOUT)
|
|
|
|
end
|
|
|
|
|
2013-11-09 22:48:54 -05:00
|
|
|
def handle_fetch_exception(ex)
|
2013-05-10 20:19:23 -04:00
|
|
|
if !@down
|
|
|
|
logger.error("Error fetching message: #{ex}")
|
|
|
|
ex.backtrace.each do |bt|
|
|
|
|
logger.error(bt)
|
2012-03-30 23:59:08 -04:00
|
|
|
end
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
2013-05-10 20:19:23 -04:00
|
|
|
@down ||= Time.now
|
2014-02-01 18:04:20 -05:00
|
|
|
pause
|
2013-05-10 20:19:23 -04:00
|
|
|
after(0) { fetch }
|
|
|
|
rescue Task::TerminatedError
|
|
|
|
# If redis is down when we try to shut down, all the fetch backlog
|
|
|
|
# raises these errors. Haven't been able to figure out what I'm doing wrong.
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
|
2012-05-12 00:25:38 -04:00
|
|
|
# Ugh. Say hello to a bloody hack.
|
|
|
|
# Can't find a clean way to get the fetcher to just stop processing
|
|
|
|
# its mailbox when shutdown starts.
|
|
|
|
def self.done!
|
|
|
|
@done = true
|
|
|
|
end
|
|
|
|
|
2014-02-01 18:04:20 -05:00
|
|
|
def self.reset # testing only
|
|
|
|
@done = nil
|
|
|
|
end
|
|
|
|
|
2012-05-12 00:25:38 -04:00
|
|
|
def self.done?
|
|
|
|
@done
|
|
|
|
end
|
2013-02-05 18:11:41 -05:00
|
|
|
|
|
|
|
def self.strategy
|
|
|
|
Sidekiq.options[:fetch] || BasicFetch
|
|
|
|
end
|
2013-01-06 00:17:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class BasicFetch
|
|
|
|
def initialize(options)
|
|
|
|
@strictly_ordered_queues = !!options[:strict]
|
|
|
|
@queues = options[:queues].map { |q| "queue:#{q}" }
|
|
|
|
@unique_queues = @queues.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_work
|
2013-01-08 11:43:34 -05:00
|
|
|
work = Sidekiq.redis { |conn| conn.brpop(*queues_cmd) }
|
|
|
|
UnitOfWork.new(*work) if work
|
2013-01-06 00:17:08 -05:00
|
|
|
end
|
2012-05-12 00:25:38 -04:00
|
|
|
|
2013-11-23 12:53:39 -05:00
|
|
|
# By leaving this as a class method, it can be pluggable and used by the Manager actor. Making it
|
|
|
|
# an instance method will make it async to the Fetcher actor
|
|
|
|
def self.bulk_requeue(inprogress, options)
|
2013-12-01 15:38:49 -05:00
|
|
|
return if inprogress.empty?
|
|
|
|
|
2013-01-17 00:53:48 -05:00
|
|
|
Sidekiq.logger.debug { "Re-queueing terminated jobs" }
|
2013-01-17 00:48:21 -05:00
|
|
|
jobs_to_requeue = {}
|
|
|
|
inprogress.each do |unit_of_work|
|
2013-03-02 18:59:01 -05:00
|
|
|
jobs_to_requeue[unit_of_work.queue_name] ||= []
|
|
|
|
jobs_to_requeue[unit_of_work.queue_name] << unit_of_work.message
|
2013-01-17 00:48:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Sidekiq.redis do |conn|
|
2014-05-28 16:33:33 -04:00
|
|
|
conn.pipelined do
|
|
|
|
jobs_to_requeue.each do |queue, jobs|
|
|
|
|
conn.rpush("queue:#{queue}", jobs)
|
|
|
|
end
|
2013-01-17 00:48:21 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-17 00:53:48 -05:00
|
|
|
Sidekiq.logger.info("Pushed #{inprogress.size} messages back to Redis")
|
2013-05-10 20:19:23 -04:00
|
|
|
rescue => ex
|
|
|
|
Sidekiq.logger.warn("Failed to requeue #{inprogress.size} jobs: #{ex.message}")
|
2013-01-17 00:48:21 -05:00
|
|
|
end
|
|
|
|
|
2013-01-06 00:17:08 -05:00
|
|
|
UnitOfWork = Struct.new(:queue, :message) do
|
|
|
|
def acknowledge
|
|
|
|
# nothing to do
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_name
|
|
|
|
queue.gsub(/.*queue:/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def requeue
|
|
|
|
Sidekiq.redis do |conn|
|
2013-03-02 18:59:01 -05:00
|
|
|
conn.rpush("queue:#{queue_name}", message)
|
2013-01-06 00:17:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-03 23:19:29 -04:00
|
|
|
|
2014-03-05 06:04:32 -05:00
|
|
|
# Creating the Redis#brpop command takes into account any
|
|
|
|
# configured queue weights. By default Redis#brpop returns
|
2012-04-03 23:19:29 -04:00
|
|
|
# data from the first queue that has pending elements. We
|
2014-03-05 06:04:32 -05:00
|
|
|
# recreate the queue command each time we invoke Redis#brpop
|
2012-04-03 23:19:29 -04:00
|
|
|
# to honor weights and avoid queue starvation.
|
|
|
|
def queues_cmd
|
2013-01-11 00:59:11 -05:00
|
|
|
queues = @strictly_ordered_queues ? @unique_queues.dup : @queues.shuffle.uniq
|
2013-01-06 00:17:08 -05:00
|
|
|
queues << Sidekiq::Fetcher::TIMEOUT
|
2012-04-03 23:19:29 -04:00
|
|
|
end
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
end
|