2016-11-22 23:39:00 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-01 18:20:41 +02:00
|
|
|
|
|
|
|
require "sidekiq/manager"
|
|
|
|
require "sidekiq/fetch"
|
|
|
|
require "sidekiq/scheduled"
|
2013-01-30 00:15:34 +08:00
|
|
|
|
|
|
|
module Sidekiq
|
2019-08-28 09:59:28 -07:00
|
|
|
# The Launcher starts the Manager and Poller threads and provides the process heartbeat.
|
2013-01-30 00:15:34 +08:00
|
|
|
class Launcher
|
2013-09-22 14:05:29 -07:00
|
|
|
include Util
|
2013-09-21 17:05:16 -07:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
STATS_TTL = 5 * 365 * 24 * 60 * 60 # 5 years
|
2018-12-30 21:15:21 +01:00
|
|
|
|
|
|
|
PROCTITLES = [
|
2019-04-01 18:20:41 +02:00
|
|
|
proc { "sidekiq" },
|
2018-12-30 21:15:21 +01:00
|
|
|
proc { Sidekiq::VERSION },
|
2019-04-01 18:20:41 +02:00
|
|
|
proc { |me, data| data["tag"] },
|
|
|
|
proc { |me, data| "[#{Processor::WORKER_STATE.size} of #{data["concurrency"]} busy]" },
|
2018-12-30 21:15:21 +01:00
|
|
|
proc { |me, data| "stopping" if me.stopping? },
|
|
|
|
]
|
2018-08-10 05:57:56 -07:00
|
|
|
|
2018-12-30 21:15:21 +01:00
|
|
|
attr_accessor :manager, :poller, :fetcher
|
2013-09-21 17:05:16 -07:00
|
|
|
|
2013-01-30 00:15:34 +08:00
|
|
|
def initialize(options)
|
2015-10-08 09:37:37 -07:00
|
|
|
@manager = Sidekiq::Manager.new(options)
|
2015-10-06 12:43:01 -07:00
|
|
|
@poller = Sidekiq::Scheduled::Poller.new
|
2013-09-21 17:05:16 -07:00
|
|
|
@done = false
|
2013-01-30 00:15:34 +08:00
|
|
|
@options = options
|
2013-09-21 17:05:16 -07:00
|
|
|
end
|
2013-09-21 10:51:49 -04:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
def run
|
|
|
|
@thread = safe_thread("heartbeat", &method(:start_heartbeat))
|
|
|
|
@poller.start
|
|
|
|
@manager.start
|
|
|
|
end
|
2014-12-10 09:26:55 -08:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
# Stops this instance from processing any more jobs,
|
|
|
|
#
|
|
|
|
def quiet
|
2015-10-07 14:27:47 -07:00
|
|
|
@done = true
|
2015-10-06 12:43:01 -07:00
|
|
|
@manager.quiet
|
|
|
|
@poller.terminate
|
2013-01-30 00:15:34 +08:00
|
|
|
end
|
|
|
|
|
2015-10-06 12:43:01 -07: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
|
2018-10-18 13:51:58 -07:00
|
|
|
deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + @options[:timeout]
|
2014-03-21 08:53:22 -07:00
|
|
|
|
2015-10-07 14:27:47 -07:00
|
|
|
@done = true
|
2015-10-06 12:43:01 -07:00
|
|
|
@manager.quiet
|
|
|
|
@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.
|
2015-10-07 12:21:10 -07:00
|
|
|
strategy = (@options[:fetch] || Sidekiq::BasicFetch)
|
|
|
|
strategy.bulk_requeue([], @options)
|
2015-10-06 12:43:01 -07:00
|
|
|
|
2015-10-06 14:45:10 -07:00
|
|
|
clear_heartbeat
|
2013-01-30 00:15:34 +08:00
|
|
|
end
|
|
|
|
|
2015-10-07 14:27:47 -07:00
|
|
|
def stopping?
|
|
|
|
@done
|
|
|
|
end
|
|
|
|
|
2015-10-06 14:05:46 -07:00
|
|
|
private unless $TESTING
|
2015-10-06 12:43:01 -07:00
|
|
|
|
2018-12-30 21:15:21 +01:00
|
|
|
def start_heartbeat
|
2019-04-01 18:20:41 +02:00
|
|
|
loop do
|
2018-12-30 21:15:21 +01:00
|
|
|
heartbeat
|
|
|
|
sleep 5
|
|
|
|
end
|
|
|
|
Sidekiq.logger.info("Heartbeat stopping...")
|
|
|
|
end
|
|
|
|
|
|
|
|
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.
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.pipelined do
|
2019-04-01 18:20:41 +02:00
|
|
|
conn.srem("processes", identity)
|
2018-12-30 21:15:21 +01:00
|
|
|
conn.del("#{identity}:workers")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# best effort, ignore network errors
|
|
|
|
end
|
|
|
|
|
2016-11-25 13:39:22 -08:00
|
|
|
def heartbeat
|
2019-04-01 18:20:41 +02:00
|
|
|
$0 = PROCTITLES.map { |proc| proc.call(self, to_data) }.compact.join(" ")
|
2014-03-07 22:41:10 -08:00
|
|
|
|
2016-11-25 13:39:22 -08:00
|
|
|
❤
|
2013-01-30 00:15:34 +08:00
|
|
|
end
|
2013-01-30 00:43:44 +08:00
|
|
|
|
2016-11-25 13:39:22 -08:00
|
|
|
def ❤
|
|
|
|
key = identity
|
2015-10-14 11:08:36 -07:00
|
|
|
fails = procd = 0
|
2018-12-30 21:15:21 +01:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
begin
|
2018-04-23 15:46:58 -07:00
|
|
|
fails = Processor::FAILURE.reset
|
|
|
|
procd = Processor::PROCESSED.reset
|
2018-08-10 05:57:56 -07:00
|
|
|
curstate = Processor::WORKER_STATE.dup
|
2015-10-07 14:27:47 -07:00
|
|
|
|
2018-02-16 13:01:25 -08:00
|
|
|
workers_key = "#{key}:workers"
|
|
|
|
nowdate = Time.now.utc.strftime("%Y-%m-%d")
|
2018-12-30 21:15:21 +01:00
|
|
|
|
2015-10-07 14:27:47 -07:00
|
|
|
Sidekiq.redis do |conn|
|
2016-05-23 10:19:59 -07:00
|
|
|
conn.multi do
|
2018-02-16 13:01:25 -08:00
|
|
|
conn.incrby("stat:processed", procd)
|
2015-10-07 14:27:47 -07:00
|
|
|
conn.incrby("stat:processed:#{nowdate}", procd)
|
2018-08-06 19:46:00 +02:00
|
|
|
conn.expire("stat:processed:#{nowdate}", STATS_TTL)
|
2018-08-10 05:57:56 -07:00
|
|
|
|
2018-02-16 13:01:25 -08:00
|
|
|
conn.incrby("stat:failed", fails)
|
2015-10-07 14:27:47 -07:00
|
|
|
conn.incrby("stat:failed:#{nowdate}", fails)
|
2018-08-06 19:46:00 +02:00
|
|
|
conn.expire("stat:failed:#{nowdate}", STATS_TTL)
|
2018-08-10 05:57:56 -07:00
|
|
|
|
2015-10-07 14:27:47 -07:00
|
|
|
conn.del(workers_key)
|
2018-08-10 05:57:56 -07:00
|
|
|
curstate.each_pair do |tid, hash|
|
2015-10-07 14:27:47 -07:00
|
|
|
conn.hset(workers_key, tid, Sidekiq.dump_json(hash))
|
|
|
|
end
|
2016-05-04 14:43:15 -05:00
|
|
|
conn.expire(workers_key, 60)
|
2015-10-07 14:27:47 -07:00
|
|
|
end
|
|
|
|
end
|
2018-12-30 21:15:21 +01:00
|
|
|
|
2015-10-14 11:08:36 -07:00
|
|
|
fails = procd = 0
|
2015-10-07 14:27:47 -07:00
|
|
|
|
2019-04-01 18:20:41 +02:00
|
|
|
_, exists, _, _, msg = Sidekiq.redis { |conn|
|
|
|
|
res = conn.multi {
|
|
|
|
conn.sadd("processes", key)
|
2016-08-12 12:34:41 -07:00
|
|
|
conn.exists(key)
|
2019-04-01 18:20:41 +02:00
|
|
|
conn.hmset(key, "info", to_json, "busy", curstate.size, "beat", Time.now.to_f, "quiet", @done)
|
2015-10-06 12:43:01 -07:00
|
|
|
conn.expire(key, 60)
|
|
|
|
conn.rpop("#{key}-signals")
|
2019-04-01 18:20:41 +02:00
|
|
|
}
|
2018-12-30 21:15:21 +01:00
|
|
|
|
|
|
|
res
|
2019-04-01 18:20:41 +02:00
|
|
|
}
|
2015-10-06 12:43:01 -07:00
|
|
|
|
2016-08-12 12:34:41 -07:00
|
|
|
# first heartbeat or recovering from an outage and need to reestablish our heartbeat
|
2019-04-01 18:20:41 +02:00
|
|
|
fire_event(:heartbeat) unless exists
|
2016-08-12 12:34:41 -07:00
|
|
|
|
2015-10-06 12:43:01 -07:00
|
|
|
return unless msg
|
|
|
|
|
2019-04-26 12:27:16 -07:00
|
|
|
::Process.kill(msg, ::Process.pid)
|
2015-10-06 12:43:01 -07:00
|
|
|
rescue => e
|
|
|
|
# ignore all redis/network issues
|
|
|
|
logger.error("heartbeat: #{e.message}")
|
2015-10-14 11:08:36 -07:00
|
|
|
# don't lose the counts if there was a network issue
|
2018-04-23 15:46:58 -07:00
|
|
|
Processor::PROCESSED.incr(procd)
|
|
|
|
Processor::FAILURE.incr(fails)
|
2015-10-06 12:43:01 -07:00
|
|
|
end
|
|
|
|
end
|
2014-03-21 08:53:22 -07:00
|
|
|
|
2016-11-25 13:39:22 -08:00
|
|
|
def to_data
|
|
|
|
@data ||= begin
|
|
|
|
{
|
2019-04-01 18:20:41 +02:00
|
|
|
"hostname" => hostname,
|
|
|
|
"started_at" => Time.now.to_f,
|
2019-04-26 12:27:16 -07:00
|
|
|
"pid" => ::Process.pid,
|
2019-04-01 18:20:41 +02:00
|
|
|
"tag" => @options[:tag] || "",
|
|
|
|
"concurrency" => @options[:concurrency],
|
|
|
|
"queues" => @options[:queues].uniq,
|
|
|
|
"labels" => @options[:labels],
|
|
|
|
"identity" => identity,
|
2016-11-25 13:39:22 -08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_json
|
|
|
|
@json ||= begin
|
|
|
|
# this data changes infrequently so dump it to a string
|
|
|
|
# now so we don't need to dump it every heartbeat.
|
|
|
|
Sidekiq.dump_json(to_data)
|
|
|
|
end
|
|
|
|
end
|
2013-01-30 00:15:34 +08:00
|
|
|
end
|
|
|
|
end
|