2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-09-30 11:29:37 -04:00
|
|
|
module Puma
|
2012-09-02 23:33:09 -04:00
|
|
|
|
2022-02-05 12:06:22 -05:00
|
|
|
# This is an event sink used by `Puma::Server` to handle
|
|
|
|
# lifecycle events such as :on_booted, :on_restart, and :on_stopped.
|
|
|
|
# Using `Puma::DSL` it is possible to register callback hooks
|
|
|
|
# for each event type.
|
|
|
|
class Events
|
2013-07-15 17:29:10 -04:00
|
|
|
|
2022-02-05 12:06:22 -05:00
|
|
|
def initialize
|
2013-09-13 12:56:39 -04:00
|
|
|
@hooks = Hash.new { |h,k| h[k] = [] }
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
|
2013-09-13 12:56:39 -04:00
|
|
|
# Fire callbacks for the named hook
|
|
|
|
def fire(hook, *args)
|
|
|
|
@hooks[hook].each { |t| t.call(*args) }
|
|
|
|
end
|
|
|
|
|
2016-11-24 13:00:51 -05:00
|
|
|
# Register a callback for a given hook
|
2013-09-13 12:56:39 -04:00
|
|
|
def register(hook, obj=nil, &blk)
|
|
|
|
if obj and blk
|
|
|
|
raise "Specify either an object or a block, not both"
|
|
|
|
end
|
|
|
|
|
|
|
|
h = obj || blk
|
|
|
|
|
|
|
|
@hooks[hook] << h
|
|
|
|
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2016-11-24 13:00:51 -05:00
|
|
|
def on_booted(&block)
|
|
|
|
register(:on_booted, &block)
|
2013-07-05 19:54:15 -04:00
|
|
|
end
|
|
|
|
|
2020-10-26 18:02:31 -04:00
|
|
|
def on_restart(&block)
|
|
|
|
register(:on_restart, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_stopped(&block)
|
|
|
|
register(:on_stopped, &block)
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
def fire_on_booted!
|
2016-11-24 13:00:51 -05:00
|
|
|
fire(:on_booted)
|
2013-07-05 19:54:15 -04:00
|
|
|
end
|
|
|
|
|
2020-10-26 18:02:31 -04:00
|
|
|
def fire_on_restart!
|
|
|
|
fire(:on_restart)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fire_on_stopped!
|
|
|
|
fire(:on_stopped)
|
|
|
|
end
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
end
|