2015-10-06 18:27:21 -04:00
|
|
|
# encoding: utf-8
|
2013-01-29 11:15:34 -05:00
|
|
|
require 'sidekiq/manager'
|
2013-09-21 23:17:37 -04:00
|
|
|
require 'sidekiq/fetch'
|
2013-01-29 11:15:34 -05:00
|
|
|
require 'sidekiq/scheduled'
|
|
|
|
|
|
|
|
module Sidekiq
|
2013-09-21 20:05:16 -04:00
|
|
|
# The Launcher is a very simple Actor whose job is to
|
|
|
|
# start, monitor and stop the core Actors in Sidekiq.
|
|
|
|
# If any of these actors die, the Sidekiq process exits
|
|
|
|
# immediately.
|
2013-01-29 11:15:34 -05:00
|
|
|
class Launcher
|
2013-09-22 17:05:29 -04:00
|
|
|
include Util
|
2013-09-21 20:05:16 -04:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
attr_accessor :manager, :poller, :fetcher
|
2013-09-21 20:05:16 -04:00
|
|
|
|
2013-01-29 11:15:34 -05:00
|
|
|
def initialize(options)
|
2015-10-06 15:43:01 -04:00
|
|
|
@condvar = ::ConditionVariable.new
|
|
|
|
@manager = Sidekiq::Manager.new(@condvar, options)
|
|
|
|
@poller = Sidekiq::Scheduled::Poller.new
|
|
|
|
@fetcher = Sidekiq::Fetcher.new(@manager, options)
|
2013-09-22 17:05:29 -04:00
|
|
|
@manager.fetcher = @fetcher
|
2013-09-21 20:05:16 -04:00
|
|
|
@done = false
|
2013-01-29 11:15:34 -05:00
|
|
|
@options = options
|
2013-09-21 20:05:16 -04:00
|
|
|
end
|
2013-09-21 10:51:49 -04:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
def run
|
|
|
|
@thread = safe_thread("heartbeat", &method(:start_heartbeat))
|
|
|
|
@fetcher.start
|
|
|
|
@poller.start
|
|
|
|
@manager.start
|
|
|
|
end
|
2014-12-10 12:26:55 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
# Stops this instance from processing any more jobs,
|
|
|
|
#
|
|
|
|
def quiet
|
|
|
|
@manager.quiet
|
|
|
|
@fetcher.terminate
|
|
|
|
@poller.terminate
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
# Shuts down the process. This method does not
|
|
|
|
# return until all work is complete and cleaned up.
|
|
|
|
# It can take up to the timeout to complete.
|
|
|
|
def stop
|
|
|
|
deadline = Time.now + @options[:timeout]
|
2014-03-21 11:53:22 -04:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
@manager.quiet
|
|
|
|
@fetcher.terminate
|
|
|
|
@poller.terminate
|
|
|
|
|
|
|
|
@manager.stop(deadline)
|
|
|
|
|
|
|
|
# Requeue everything in case there was a worker who grabbed work while stopped
|
|
|
|
# This call is a no-op in Sidekiq but necessary for Sidekiq Pro.
|
|
|
|
Sidekiq::Fetcher.strategy.bulk_requeue([], @options)
|
|
|
|
|
2015-10-06 17:45:10 -04:00
|
|
|
clear_heartbeat
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
|
2015-10-06 17:05:46 -04:00
|
|
|
private unless $TESTING
|
2015-10-06 15:43:01 -04:00
|
|
|
|
|
|
|
JVM_RESERVED_SIGNALS = ['USR1', 'USR2'] # Don't Process#kill if we get these signals via the API
|
2014-01-10 11:56:20 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
PROCTITLES = [
|
|
|
|
proc { 'sidekiq'.freeze },
|
|
|
|
proc { Sidekiq::VERSION },
|
|
|
|
proc { |me, data| data['tag'] },
|
|
|
|
proc { |me, data| "[#{me.manager.in_progress.size} of #{data['concurrency']} busy]" },
|
|
|
|
proc { |me, data| "stopping" if me.manager.stopped? },
|
|
|
|
]
|
2014-01-10 14:58:53 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
def heartbeat(key, data, json)
|
2015-10-06 17:05:46 -04:00
|
|
|
results = PROCTITLES.map {|x| x.(self, data) }
|
|
|
|
results.compact!
|
|
|
|
$0 = results.join(' ')
|
2014-03-08 01:41:10 -05:00
|
|
|
|
2015-10-06 17:05:46 -04:00
|
|
|
❤(key, json)
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
2013-01-29 11:43:44 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
def ❤(key, json)
|
|
|
|
begin
|
|
|
|
_, _, _, msg = Sidekiq.redis do |conn|
|
|
|
|
conn.pipelined do
|
|
|
|
conn.sadd('processes', key)
|
|
|
|
conn.hmset(key, 'info', json, 'busy', manager.in_progress.size, 'beat', Time.now.to_f)
|
|
|
|
conn.expire(key, 60)
|
|
|
|
conn.rpop("#{key}-signals")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless msg
|
|
|
|
|
|
|
|
if JVM_RESERVED_SIGNALS.include?(msg)
|
|
|
|
Sidekiq::CLI.instance.handle_signal(msg)
|
|
|
|
else
|
|
|
|
::Process.kill(msg, $$)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
# ignore all redis/network issues
|
|
|
|
logger.error("heartbeat: #{e.message}")
|
|
|
|
end
|
|
|
|
end
|
2014-03-21 11:53:22 -04:00
|
|
|
|
2014-03-21 23:46:14 -04:00
|
|
|
def start_heartbeat
|
2014-03-08 01:41:10 -05:00
|
|
|
key = identity
|
|
|
|
data = {
|
2014-03-02 19:36:00 -05:00
|
|
|
'hostname' => hostname,
|
2014-03-07 00:30:11 -05:00
|
|
|
'started_at' => Time.now.to_f,
|
2014-03-02 19:36:00 -05:00
|
|
|
'pid' => $$,
|
2014-03-21 23:46:14 -04:00
|
|
|
'tag' => @options[:tag] || '',
|
2014-03-02 19:36:00 -05:00
|
|
|
'concurrency' => @options[:concurrency],
|
|
|
|
'queues' => @options[:queues].uniq,
|
2015-10-06 15:43:01 -04:00
|
|
|
'labels' => @options[:labels],
|
2014-12-30 20:25:55 -05:00
|
|
|
'identity' => identity,
|
2014-03-08 01:41:10 -05:00
|
|
|
}
|
2014-08-08 00:03:34 -04:00
|
|
|
# this data doesn't change so dump it to a string
|
|
|
|
# now so we don't need to dump it every heartbeat.
|
|
|
|
json = Sidekiq.dump_json(data)
|
2015-10-06 17:05:46 -04:00
|
|
|
|
|
|
|
while !@done
|
|
|
|
heartbeat(key, data, json)
|
|
|
|
sleep 5
|
|
|
|
end
|
2015-10-06 17:45:10 -04:00
|
|
|
Sidekiq.logger.info("Heartbeat stopping...")
|
2014-03-08 01:41:10 -05:00
|
|
|
end
|
|
|
|
|
2015-10-06 17:45:10 -04:00
|
|
|
def clear_heartbeat
|
|
|
|
# Remove record from Redis since we are shutting down.
|
|
|
|
# Note we don't stop the heartbeat thread; if the process
|
|
|
|
# doesn't actually exit, it'll reappear in the Web UI.
|
2014-03-08 01:41:10 -05:00
|
|
|
Sidekiq.redis do |conn|
|
2014-06-06 11:37:23 -04:00
|
|
|
conn.pipelined do
|
|
|
|
conn.srem('processes', identity)
|
|
|
|
conn.del("#{identity}:workers")
|
|
|
|
end
|
2014-03-08 01:41:10 -05:00
|
|
|
end
|
2015-03-13 15:07:01 -04:00
|
|
|
rescue
|
|
|
|
# best effort, ignore network errors
|
2013-01-29 11:43:44 -05:00
|
|
|
end
|
2014-08-08 00:03:34 -04:00
|
|
|
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
end
|