mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
8a4ef0c16c
* Split out LogWriter from Events * Improve code comment * Fix constructor interfaces * Fix file includes * Fix specs and requires * Fix LogWriter * More fixes * Fix tests * Fix specs * Fix spec * Fix more specs * Refactor: Split out LogWriter from Events * Improve comments * Fix bundle pruner Co-authored-by: shields <shields@tablecheck.com>
57 lines
1.1 KiB
Ruby
57 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Puma
|
|
|
|
# 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
|
|
|
|
def initialize
|
|
@hooks = Hash.new { |h,k| h[k] = [] }
|
|
end
|
|
|
|
# Fire callbacks for the named hook
|
|
def fire(hook, *args)
|
|
@hooks[hook].each { |t| t.call(*args) }
|
|
end
|
|
|
|
# Register a callback for a given hook
|
|
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
|
|
|
|
def on_booted(&block)
|
|
register(:on_booted, &block)
|
|
end
|
|
|
|
def on_restart(&block)
|
|
register(:on_restart, &block)
|
|
end
|
|
|
|
def on_stopped(&block)
|
|
register(:on_stopped, &block)
|
|
end
|
|
|
|
def fire_on_booted!
|
|
fire(:on_booted)
|
|
end
|
|
|
|
def fire_on_restart!
|
|
fire(:on_restart)
|
|
end
|
|
|
|
def fire_on_stopped!
|
|
fire(:on_stopped)
|
|
end
|
|
end
|
|
end
|