2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2012-03-24 13:28:18 -07:00
|
|
|
require 'sidekiq'
|
|
|
|
|
|
|
|
module Sidekiq
|
2015-10-08 09:37:37 -07:00
|
|
|
class BasicFetch
|
2015-10-09 15:33:42 -07: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 12:21:10 -07:00
|
|
|
TIMEOUT = 2
|
2013-05-10 17:19:23 -07:00
|
|
|
|
2015-10-23 15:05:50 -07:00
|
|
|
UnitOfWork = Struct.new(:queue, :job) do
|
2015-10-09 15:33:42 -07:00
|
|
|
def acknowledge
|
|
|
|
# nothing to do
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_name
|
2018-02-16 13:01:25 -08:00
|
|
|
queue.sub(/.*queue:/, '')
|
2015-10-09 15:33:42 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def requeue
|
|
|
|
Sidekiq.redis do |conn|
|
2015-10-23 15:05:50 -07:00
|
|
|
conn.rpush("queue:#{queue_name}", job)
|
2015-10-09 15:33:42 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 21:17:08 -08:00
|
|
|
def initialize(options)
|
|
|
|
@strictly_ordered_queues = !!options[:strict]
|
|
|
|
@queues = options[:queues].map { |q| "queue:#{q}" }
|
2015-10-07 09:43:08 -07:00
|
|
|
if @strictly_ordered_queues
|
|
|
|
@queues = @queues.uniq
|
2015-10-08 09:37:37 -07:00
|
|
|
@queues << TIMEOUT
|
2015-10-07 09:43:08 -07:00
|
|
|
end
|
2013-01-05 21:17:08 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_work
|
2013-01-08 08:43:34 -08:00
|
|
|
work = Sidekiq.redis { |conn| conn.brpop(*queues_cmd) }
|
|
|
|
UnitOfWork.new(*work) if work
|
2013-01-05 21:17:08 -08:00
|
|
|
end
|
2012-05-11 21:25:38 -07:00
|
|
|
|
2015-10-09 15:33:42 -07: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 12:38:49 -08:00
|
|
|
return if inprogress.empty?
|
|
|
|
|
2013-01-16 21:53:48 -08:00
|
|
|
Sidekiq.logger.debug { "Re-queueing terminated jobs" }
|
2013-01-16 21:48:21 -08:00
|
|
|
jobs_to_requeue = {}
|
|
|
|
inprogress.each do |unit_of_work|
|
2013-03-02 15:59:01 -08:00
|
|
|
jobs_to_requeue[unit_of_work.queue_name] ||= []
|
2015-10-23 15:05:50 -07:00
|
|
|
jobs_to_requeue[unit_of_work.queue_name] << unit_of_work.job
|
2013-01-16 21:48:21 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
Sidekiq.redis do |conn|
|
2014-05-28 13:33:33 -07:00
|
|
|
conn.pipelined do
|
|
|
|
jobs_to_requeue.each do |queue, jobs|
|
|
|
|
conn.rpush("queue:#{queue}", jobs)
|
|
|
|
end
|
2013-01-16 21:48:21 -08:00
|
|
|
end
|
|
|
|
end
|
2015-10-23 15:05:50 -07:00
|
|
|
Sidekiq.logger.info("Pushed #{inprogress.size} jobs back to Redis")
|
2013-05-10 17:19:23 -07:00
|
|
|
rescue => ex
|
|
|
|
Sidekiq.logger.warn("Failed to requeue #{inprogress.size} jobs: #{ex.message}")
|
2013-01-16 21:48:21 -08:00
|
|
|
end
|
|
|
|
|
2012-03-24 13:28:18 -07:00
|
|
|
end
|
|
|
|
end
|