From f17e94acc86d245c8dd8e400e1df4c2240bb4530 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Wed, 30 Jan 2013 00:15:34 +0800 Subject: [PATCH] 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. --- lib/sidekiq/cli.rb | 16 +++++++--------- lib/sidekiq/launcher.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 lib/sidekiq/launcher.rb diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index a7ee6525..32b2a95d 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -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) diff --git a/lib/sidekiq/launcher.rb b/lib/sidekiq/launcher.rb new file mode 100644 index 00000000..0074d985 --- /dev/null +++ b/lib/sidekiq/launcher.rb @@ -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