2012-03-24 16:28:18 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
|
|
|
|
module Sidekiq
|
2015-10-08 12:37:37 -04:00
|
|
|
class BasicFetch
|
2015-10-09 18:33:42 -04:00
|
|
|
# We want the fetch operation to timeout every few seconds so the thread
|
|
|
|
# can check if the process is shutting down.
|
2015-10-07 15:21:10 -04:00
|
|
|
TIMEOUT = 2
|
2013-05-10 20:19:23 -04:00
|
|
|
|
2015-10-23 18:05:50 -04:00
|
|
|
UnitOfWork = Struct.new(:queue, :job) do
|
2015-10-09 18:33:42 -04:00
|
|
|
def acknowledge
|
|
|
|
# nothing to do
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_name
|
|
|
|
queue.gsub(/.*queue:/, ''.freeze)
|
|
|
|
end
|
|
|
|
|
|
|
|
def requeue
|
|
|
|
Sidekiq.redis do |conn|
|
2015-10-23 18:05:50 -04:00
|
|
|
conn.rpush("queue:#{queue_name}", job)
|
2015-10-09 18:33:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 00:17:08 -05:00
|
|
|
def initialize(options)
|
|
|
|
@strictly_ordered_queues = !!options[:strict]
|
|
|
|
@queues = options[:queues].map { |q| "queue:#{q}" }
|
2015-10-07 12:43:08 -04:00
|
|
|
if @strictly_ordered_queues
|
|
|
|
@queues = @queues.uniq
|
2015-10-08 12:37:37 -04:00
|
|
|
@queues << TIMEOUT
|
2015-10-07 12:43:08 -04:00
|
|
|
end
|
2013-01-06 00:17:08 -05:00
|
|
|
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
|
|
|
|
2015-10-09 18:33:42 -04:00
|
|
|
# Creating the Redis#brpop command takes into account any
|
|
|
|
# configured queue weights. By default Redis#brpop returns
|
|
|
|
# data from the first queue that has pending elements. We
|
|
|
|
# recreate the queue command each time we invoke Redis#brpop
|
|
|
|
# to honor weights and avoid queue starvation.
|
|
|
|
def queues_cmd
|
|
|
|
if @strictly_ordered_queues
|
|
|
|
@queues
|
|
|
|
else
|
|
|
|
queues = @queues.shuffle.uniq
|
|
|
|
queues << TIMEOUT
|
|
|
|
queues
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
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] ||= []
|
2015-10-23 18:05:50 -04:00
|
|
|
jobs_to_requeue[unit_of_work.queue_name] << unit_of_work.job
|
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
|
2015-10-23 18:05:50 -04:00
|
|
|
Sidekiq.logger.info("Pushed #{inprogress.size} jobs 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
|
|
|
|
|
2012-03-24 16:28:18 -04:00
|
|
|
end
|
|
|
|
end
|