1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/manager.rb

135 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2012-01-22 16:01:46 -08:00
require "sidekiq/processor"
require "set"
2012-01-16 16:14:47 -08: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
#
class Manager
include Sidekiq::Component
2012-01-22 11:32:38 -08:00
attr_reader :workers
attr_reader :capsule
def initialize(capsule)
@config = @capsule = capsule
@count = capsule.concurrency
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
@workers = Set.new
@plock = Mutex.new
@count.times do
@workers << Processor.new(@config, &method(:processor_result))
2013-06-12 15:16:19 -07:00
end
2012-01-22 11:32:38 -08:00
end
2015-10-06 12:43:01 -07:00
def start
@workers.each(&:start)
2015-10-06 12:43:01 -07:00
end
2015-10-06 12:43:01 -07:00
def quiet
return if @done
@done = true
2012-01-22 11:32:38 -08:00
logger.info { "Terminating quiet threads for #{capsule.name} capsule" }
@workers.each(&:terminate)
2012-01-22 11:32:38 -08:00
end
2015-10-06 12:43:01 -07:00
def stop(deadline)
quiet
# 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
sleep PAUSE_TIME
return if @workers.empty?
logger.info { "Pausing to allow jobs to finish..." }
wait_for(deadline) { @workers.empty? }
return if @workers.empty?
2012-01-16 16:18:36 -08:00
2015-10-06 12:43:01 -07:00
hard_shutdown
ensure
capsule.stop
end
def processor_result(processor, reason = nil)
2015-10-06 12:43:01 -07:00
@plock.synchronize do
@workers.delete(processor)
2015-10-07 09:47:53 -07:00
unless @done
p = Processor.new(@config, &method(:processor_result))
@workers << p
2015-10-06 14:45:10 -07:00
p.start
end
2012-01-22 16:01:46 -08:00
end
end
def stopped?
@done
end
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 threads.
2015-10-06 14:45:10 -07:00
# They must die but their jobs shall live on.
cleanup = nil
@plock.synchronize do
cleanup = @workers.dup
2015-10-06 14:45:10 -07:00
end
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
logger.warn { "Terminating #{cleanup.size} busy threads" }
logger.debug { "Jobs 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 thread is terminated. This is ok because Sidekiq's
2015-10-06 14:45:10 -07:00
# 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.
capsule.fetcher.bulk_requeue(jobs, nil)
2015-10-06 14:45:10 -07:00
end
cleanup.each do |processor|
2015-10-06 12:43:01 -07:00
processor.kill
end
# 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? }
end
# hack for quicker development / testing environment #2774
PAUSE_TIME = $stdout.tty? ? 0.1 : 0.5
# Wait for the orblock to be true or the deadline passed.
def wait_for(deadline, &condblock)
remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
while remaining > PAUSE_TIME
return if condblock.call
sleep PAUSE_TIME
remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end
end
2012-01-16 16:14:47 -08:00
end
end