mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Add Sidekiq::Launcher, extracted from Sidekiq::CLI.
This is a step forward to run Sidekiq inside a process, making it more easily to integrate into existing process.
This commit is contained in:
parent
e2385f41fa
commit
f17e94acc8
2 changed files with 33 additions and 9 deletions
|
@ -18,7 +18,7 @@ end
|
|||
|
||||
trap 'USR1' do
|
||||
Sidekiq.logger.info "Received USR1, no longer accepting new work"
|
||||
mgr = Sidekiq::CLI.instance.manager
|
||||
mgr = Sidekiq::CLI.instance.launcher.manager
|
||||
mgr.async.stop if mgr
|
||||
end
|
||||
|
||||
|
@ -49,6 +49,7 @@ require 'erb'
|
|||
|
||||
require 'sidekiq'
|
||||
require 'sidekiq/util'
|
||||
require 'sidekiq/launcher'
|
||||
|
||||
module Sidekiq
|
||||
class CLI
|
||||
|
@ -57,7 +58,7 @@ module Sidekiq
|
|||
|
||||
# Used for CLI testing
|
||||
attr_accessor :code
|
||||
attr_accessor :manager
|
||||
attr_accessor :launcher
|
||||
attr_accessor :environment
|
||||
|
||||
def initialize
|
||||
|
@ -89,21 +90,18 @@ module Sidekiq
|
|||
logger.info 'Starting processing, hit Ctrl-C to stop'
|
||||
end
|
||||
|
||||
@manager = Sidekiq::Manager.new(options)
|
||||
poller = Sidekiq::Scheduled::Poller.new
|
||||
@launcher = Sidekiq::Launcher.new(options)
|
||||
|
||||
begin
|
||||
if options[:profile]
|
||||
require 'ruby-prof'
|
||||
RubyProf.start
|
||||
end
|
||||
@manager.async.start
|
||||
poller.async.poll(true)
|
||||
launcher.run
|
||||
sleep
|
||||
rescue Interrupt
|
||||
logger.info 'Shutting down'
|
||||
poller.async.terminate if poller.alive?
|
||||
@manager.async.stop(:shutdown => true, :timeout => options[:timeout])
|
||||
@manager.wait(:shutdown)
|
||||
launcher.stop
|
||||
# Explicitly exit so busy Processor threads can't block
|
||||
# process shutdown.
|
||||
exit(0)
|
||||
|
|
26
lib/sidekiq/launcher.rb
Normal file
26
lib/sidekiq/launcher.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
|
||||
require 'sidekiq/util'
|
||||
require 'sidekiq/manager'
|
||||
require 'sidekiq/scheduled'
|
||||
|
||||
module Sidekiq
|
||||
class Launcher
|
||||
attr_reader :manager, :poller, :options
|
||||
def initialize(options)
|
||||
@options = options
|
||||
@manager = Sidekiq::Manager.new(options)
|
||||
@poller = Sidekiq::Scheduled::Poller.new
|
||||
end
|
||||
|
||||
def run
|
||||
manager.async.start
|
||||
poller.async.poll(true)
|
||||
end
|
||||
|
||||
def stop
|
||||
poller.async.terminate if poller.alive?
|
||||
manager.async.stop(:shutdown => true, :timeout => options[:timeout])
|
||||
manager.wait(:shutdown)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue