2016-11-22 23:39:00 -05:00
|
|
|
# frozen_string_literal: true
|
2012-01-22 16:01:46 -08:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
require "sidekiq/util"
|
|
|
|
require "sidekiq/processor"
|
|
|
|
require "sidekiq/fetch"
|
|
|
|
require "set"
|
2012-01-16 16:14:47 -08:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
module Sidekiq
|
2012-01-16 16:14:47 -08:00
|
|
|
##
|
2015-10-06 12:43:01 -07:00
|
|
|
# The Manager is the central coordination point in Sidekiq, controlling
|
2016-12-23 16:15:16 +00:00
|
|
|
# the lifecycle of the Processors.
|
2015-10-06 12:43:01 -07:00
|
|
|
#
|
|
|
|
# Tasks:
|
|
|
|
#
|
2015-10-09 15:33:42 -07:00
|
|
|
# 1. start: Spin up Processors.
|
|
|
|
# 3. processor_died: Handle job failure, throw away Processor, create new one.
|
|
|
|
# 4. quiet: shutdown idle Processors.
|
2015-10-06 12:43:01 -07:00
|
|
|
# 5. stop: hard stop the Processors by deadline.
|
|
|
|
#
|
2015-10-09 15:33:42 -07:00
|
|
|
# Note that only the last task requires its own Thread since it has to monitor
|
2015-10-06 12:43:01 -07:00
|
|
|
# the shutdown process. The other tasks are performed by other threads.
|
2012-01-16 16:14:47 -08:00
|
|
|
#
|
2012-02-03 10:02:57 -08:00
|
|
|
class Manager
|
2012-01-22 11:32:38 -08:00
|
|
|
include Util
|
|
|
|
|
2015-10-07 12:21:10 -07:00
|
|
|
attr_reader :workers
|
2015-10-08 09:37:37 -07:00
|
|
|
attr_reader :options
|
2013-05-10 20:43:53 -07:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
def initialize(options = {})
|
2012-02-14 09:00:26 -08:00
|
|
|
logger.debug { options.inspect }
|
2013-11-23 12:53:39 -05:00
|
|
|
@options = options
|
2018-09-17 10:10:27 -07:00
|
|
|
@count = options[:concurrency] || 10
|
2015-08-12 10:16:06 -07:00
|
|
|
raise ArgumentError, "Concurrency of #{@count} is not supported" if @count < 1
|
2012-01-22 11:32:38 -08:00
|
|
|
|
2012-01-22 16:01:46 -08:00
|
|
|
@done = false
|
2015-10-07 12:21:10 -07:00
|
|
|
@workers = Set.new
|
|
|
|
@count.times do
|
2020-06-19 08:39:18 -07:00
|
|
|
@workers << Processor.new(self, options)
|
2013-06-12 15:16:19 -07:00
|
|
|
end
|
2015-10-06 12:43:01 -07:00
|
|
|
@plock = Mutex.new
|
2012-01-22 11:32:38 -08:00
|
|
|
end
|
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
def start
|
2015-10-07 12:21:10 -07:00
|
|
|
@workers.each do |x|
|
2015-10-06 14:45:10 -07:00
|
|
|
x.start
|
2015-10-07 08:37:51 -07:00
|
|
|
end
|
2015-10-06 12:43:01 -07:00
|
|
|
end
|
2012-03-08 20:58:51 -08:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
def quiet
|
|
|
|
return if @done
|
|
|
|
@done = true
|
2012-01-22 11:32:38 -08:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
logger.info { "Terminating quiet workers" }
|
2015-10-07 12:21:10 -07:00
|
|
|
@workers.each { |x| x.terminate }
|
2018-01-11 09:37:55 -08:00
|
|
|
fire_event(:quiet, reverse: true)
|
2012-01-22 11:32:38 -08:00
|
|
|
end
|
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
def stop(deadline)
|
|
|
|
quiet
|
2018-01-11 09:37:55 -08:00
|
|
|
fire_event(:shutdown, reverse: true)
|
2015-10-30 14:50:44 -07:00
|
|
|
|
|
|
|
# some of the shutdown events can be async,
|
|
|
|
# we don't have any way to know when they're done but
|
|
|
|
# give them a little time to take effect
|
2016-01-06 10:00:18 -08:00
|
|
|
sleep PAUSE_TIME
|
2015-10-07 12:21:10 -07:00
|
|
|
return if @workers.empty?
|
2013-11-21 06:09:30 -05:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
logger.info { "Pausing to allow workers to finish..." }
|
2021-12-14 11:53:00 -08:00
|
|
|
wait_for(deadline) { @workers.empty? }
|
2015-10-07 12:21:10 -07:00
|
|
|
return if @workers.empty?
|
2012-01-16 16:18:36 -08:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
hard_shutdown
|
2012-02-03 10:02:57 -08:00
|
|
|
end
|
|
|
|
|
2015-10-08 09:48:28 -07:00
|
|
|
def processor_stopped(processor)
|
|
|
|
@plock.synchronize do
|
|
|
|
@workers.delete(processor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-25 13:32:51 -08:00
|
|
|
def processor_died(processor, reason)
|
2015-10-06 12:43:01 -07:00
|
|
|
@plock.synchronize do
|
2015-10-07 12:21:10 -07:00
|
|
|
@workers.delete(processor)
|
2015-10-07 09:47:53 -07:00
|
|
|
unless @done
|
2020-06-19 08:39:18 -07:00
|
|
|
p = Processor.new(self, options)
|
2015-10-07 12:21:10 -07:00
|
|
|
@workers << p
|
2015-10-06 14:45:10 -07:00
|
|
|
p.start
|
2012-03-24 13:28:18 -07:00
|
|
|
end
|
2012-01-22 16:01:46 -08:00
|
|
|
end
|
2013-01-30 00:43:44 +08:00
|
|
|
end
|
|
|
|
|
2015-06-09 13:09:53 -07:00
|
|
|
def stopped?
|
|
|
|
@done
|
|
|
|
end
|
|
|
|
|
2012-03-24 13:28:18 -07:00
|
|
|
private
|
2012-01-22 11:32:38 -08:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
def hard_shutdown
|
|
|
|
# We've reached the timeout and we still have busy workers.
|
2015-10-06 14:45:10 -07:00
|
|
|
# They must die but their jobs shall live on.
|
|
|
|
cleanup = nil
|
|
|
|
@plock.synchronize do
|
2015-10-07 12:21:10 -07:00
|
|
|
cleanup = @workers.dup
|
2015-10-06 14:45:10 -07:00
|
|
|
end
|
2012-04-06 20:53:03 -07:00
|
|
|
|
2015-10-06 14:45:10 -07:00
|
|
|
if cleanup.size > 0
|
2019-05-30 10:41:47 -07:00
|
|
|
jobs = cleanup.map { |p| p.job }.compact
|
2015-10-07 12:21:10 -07:00
|
|
|
|
2015-10-06 14:45:10 -07:00
|
|
|
logger.warn { "Terminating #{cleanup.size} busy worker threads" }
|
2015-10-07 12:21:10 -07:00
|
|
|
logger.warn { "Work still in progress #{jobs.inspect}" }
|
|
|
|
|
2015-10-06 14:45:10 -07:00
|
|
|
# Re-enqueue unfinished jobs
|
|
|
|
# NOTE: You may notice that we may push a job back to redis before
|
|
|
|
# the worker thread is terminated. This is ok because Sidekiq's
|
|
|
|
# contract says that jobs are run AT LEAST once. Process termination
|
|
|
|
# is delayed until we're certain the jobs are back in Redis because
|
|
|
|
# it is worse to lose a job than to run it twice.
|
2020-06-19 08:39:18 -07:00
|
|
|
strategy = @options[:fetch]
|
2015-10-07 12:21:10 -07:00
|
|
|
strategy.bulk_requeue(jobs, @options)
|
2015-10-06 14:45:10 -07:00
|
|
|
end
|
2012-04-06 20:53:03 -07:00
|
|
|
|
2015-10-07 12:21:10 -07:00
|
|
|
cleanup.each do |processor|
|
2015-10-06 12:43:01 -07:00
|
|
|
processor.kill
|
2012-04-06 20:53:03 -07:00
|
|
|
end
|
2021-12-14 11:53:00 -08:00
|
|
|
|
|
|
|
# when this method returns, we immediately call `exit` which may not give
|
|
|
|
# the remaining threads time to run `ensure` blocks, etc. We pause here up
|
|
|
|
# to 3 seconds to give threads a minimal amount of time to run `ensure` blocks.
|
|
|
|
deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + 3
|
|
|
|
wait_for(deadline) { @workers.empty? }
|
2012-04-06 20:53:03 -07:00
|
|
|
end
|
2012-01-16 16:14:47 -08:00
|
|
|
end
|
|
|
|
end
|