mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Implement process lifecycle events, fixes #1534
This commit is contained in:
parent
bbbe318a6c
commit
3d117e6051
4 changed files with 40 additions and 0 deletions
|
@ -20,6 +20,11 @@ module Sidekiq
|
|||
:timeout => 8,
|
||||
:profile => false,
|
||||
:error_handlers => [],
|
||||
:lifecycle_events => {
|
||||
:boot => [],
|
||||
:quiet => [],
|
||||
:shutdown => [],
|
||||
},
|
||||
}
|
||||
|
||||
def self.❨╯°□°❩╯︵┻━┻
|
||||
|
@ -121,6 +126,11 @@ module Sidekiq
|
|||
self.options[:error_handlers]
|
||||
end
|
||||
|
||||
def self.on(event, &block)
|
||||
raise ArgumentError, "Symbols only please: #{event}" if !event.is_a?(Symbol)
|
||||
raise ArgumentError, "Invalid event name: #{event}" if !options[:lifecycle_events].keys.include?(event)
|
||||
options[:lifecycle_events][event] << block
|
||||
end
|
||||
end
|
||||
|
||||
require 'sidekiq/extensions/class_methods'
|
||||
|
|
|
@ -74,6 +74,8 @@ module Sidekiq
|
|||
logger.info "Running in #{RUBY_DESCRIPTION}"
|
||||
logger.info Sidekiq::LICENSE
|
||||
|
||||
fire_event(:boot)
|
||||
|
||||
if !options[:daemon]
|
||||
logger.info 'Starting processing, hit Ctrl-C to stop'
|
||||
end
|
||||
|
@ -96,6 +98,7 @@ module Sidekiq
|
|||
rescue Interrupt
|
||||
logger.info 'Shutting down'
|
||||
launcher.stop
|
||||
fire_event(:shutdown)
|
||||
# Explicitly exit so busy Processor threads can't block
|
||||
# process shutdown.
|
||||
exit(0)
|
||||
|
@ -104,6 +107,16 @@ module Sidekiq
|
|||
|
||||
private
|
||||
|
||||
def fire_event(event)
|
||||
Sidekiq.options[:lifecycle_events][event].each do |block|
|
||||
begin
|
||||
block.call
|
||||
rescue => ex
|
||||
handle_exception(ex, { :event => event })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def handle_signal(sig)
|
||||
Sidekiq.logger.debug "Got #{sig} signal"
|
||||
case sig
|
||||
|
@ -124,6 +137,7 @@ module Sidekiq
|
|||
when 'USR1'
|
||||
Sidekiq.logger.info "Received USR1, no longer accepting new work"
|
||||
launcher.manager.async.stop
|
||||
fire_event(:quiet)
|
||||
when 'USR2'
|
||||
if Sidekiq.options[:logfile]
|
||||
Sidekiq.logger.info "Received USR2, reopening log file"
|
||||
|
|
|
@ -3,6 +3,9 @@ Sidekiq.configure_client do |config|
|
|||
end
|
||||
Sidekiq.configure_server do |config|
|
||||
config.redis = { :size => 25, :namespace => 'foo' }
|
||||
config.on(:boot) { puts "Hello!" }
|
||||
config.on(:quiet) { puts "Quiet down!" }
|
||||
config.on(:shutdown) { puts "Goodbye!" }
|
||||
end
|
||||
|
||||
class EmptyWorker
|
||||
|
|
|
@ -34,4 +34,17 @@ class TestSidekiq < Sidekiq::Test
|
|||
assert_equal "Calm down, bro\n", $stdout.string
|
||||
end
|
||||
end
|
||||
|
||||
describe 'lifecycle events' do
|
||||
it 'handles invalid input' do
|
||||
e = assert_raises ArgumentError do
|
||||
Sidekiq.on(:bot)
|
||||
end
|
||||
assert_match /Invalid event name/, e.message
|
||||
e = assert_raises ArgumentError do
|
||||
Sidekiq.on('boot')
|
||||
end
|
||||
assert_match /Symbols only/, e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue