Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lifecycle event registration support #141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ module Shoryuken
queues: [],
aws: {},
delay: 0,
timeout: 8
timeout: 8,
lifecycle_events: {
startup: [],
quiet: [],
shutdown: [],
}
}

@@queues = []
Expand Down Expand Up @@ -112,6 +117,20 @@ def on_stop(&block)
@stop_callback = block
end

# Register a block to run at a point in the Shoryuken lifecycle.
# :startup, :quiet or :shutdown are valid events.
#
# Shoryuken.configure_server do |config|
# config.on(:shutdown) do
# puts "Goodbye cruel world!"
# end
# end
def on(event, &block)
raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
raise ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
options[:lifecycle_events][event] << block
end

attr_reader :aws_initialization_callback,
:start_callback,
:stop_callback
Expand Down
4 changes: 3 additions & 1 deletion lib/shoryuken/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def run(args)
callback.call
end

fire_event(:startup)

begin
launcher.run

Expand Down Expand Up @@ -173,7 +175,7 @@ def handle_signal(sig)
logger.info { 'Received USR1, will soft shutdown down' }

launcher.stop

fire_event(:quiet, true)
exit 0
when 'TTIN'
Thread.list.each do |thread|
Expand Down
2 changes: 2 additions & 0 deletions lib/shoryuken/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def stop(options = {})
callback.call
end

fire_event(:shutdown, true)

@fetcher.terminate if @fetcher.alive?

logger.info { "Shutting down #{@ready.size} quiet workers" }
Expand Down
14 changes: 14 additions & 0 deletions lib/shoryuken/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ def logger
Shoryuken.logger
end

def fire_event(event, reverse=false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for fire_event is too high. [15.26/15]

logger.info { "Firing '#{event}' lifecycle event" }
arr = Shoryuken.options[:lifecycle_events][event]
arr.reverse! if reverse
arr.each do |block|
begin
block.call
rescue => ex
logger.warn(event: event)
logger.warn "#{ex.class.name}: #{ex.message}"
end
end
end

def elapsed(started_at)
# elapsed in ms
(Time.now - started_at) * 1000
Expand Down