2013-09-21 23:17:37 -04:00
|
|
|
require 'sidekiq/actor'
|
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-21 23:17:37 -04:00
|
|
|
include Actor
|
2013-09-22 17:05:29 -04:00
|
|
|
include Util
|
2013-09-21 20:05:16 -04:00
|
|
|
|
|
|
|
trap_exit :actor_died
|
|
|
|
|
|
|
|
attr_reader :manager, :poller, :fetcher
|
|
|
|
|
2013-01-29 11:15:34 -05:00
|
|
|
def initialize(options)
|
2013-09-21 20:05:16 -04:00
|
|
|
@manager = Sidekiq::Manager.new_link options
|
|
|
|
@poller = Sidekiq::Scheduled::Poller.new_link
|
|
|
|
@fetcher = Sidekiq::Fetcher.new_link @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
|
|
|
|
2013-09-21 20:05:16 -04:00
|
|
|
def actor_died(actor, reason)
|
|
|
|
return if @done
|
2013-09-22 23:48:17 -04:00
|
|
|
Sidekiq.logger.warn("Sidekiq died due to the following error, cannot recover, process exiting")
|
|
|
|
handle_exception(reason)
|
2013-09-21 20:05:16 -04:00
|
|
|
exit(1)
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2013-09-22 23:48:17 -04:00
|
|
|
watchdog('Launcher#run') do
|
2013-09-22 17:05:29 -04:00
|
|
|
manager.async.start
|
|
|
|
poller.async.poll(true)
|
|
|
|
end
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
2013-09-22 17:05:29 -04:00
|
|
|
watchdog('Launcher#stop') do
|
|
|
|
@done = true
|
|
|
|
Sidekiq::Fetcher.done!
|
|
|
|
fetcher.async.terminate if fetcher.alive?
|
|
|
|
poller.async.terminate if poller.alive?
|
|
|
|
|
|
|
|
manager.async.stop(:shutdown => true, :timeout => @options[:timeout])
|
|
|
|
manager.wait(:shutdown)
|
|
|
|
end
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
2013-01-29 11:43:44 -05:00
|
|
|
|
|
|
|
def procline(tag)
|
|
|
|
$0 = manager.procline(tag)
|
|
|
|
manager.after(5) { procline(tag) }
|
|
|
|
end
|
2013-01-29 11:15:34 -05:00
|
|
|
end
|
|
|
|
end
|