Class: Puma::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/events.rb

Overview

This is an event sink used by ‘Puma::Server` to handle lifecycle events such as :after_booted, :before_restart, and :after_stopped. Using `Puma::DSL` it is possible to register callback hooks for each event type.

Instance Method Summary collapse

Constructor Details

#initializeEvents

Returns a new instance of Events.



11
12
13
# File 'lib/puma/events.rb', line 11

def initialize
  @hooks = Hash.new { |h,k| h[k] = [] }
end

Instance Method Details

#after_booted(&block) ⇒ Object



33
34
35
# File 'lib/puma/events.rb', line 33

def after_booted(&block)
  register(:after_booted, &block)
end

#after_stopped(&block) ⇒ Object



41
42
43
# File 'lib/puma/events.rb', line 41

def after_stopped(&block)
  register(:after_stopped, &block)
end

#before_restart(&block) ⇒ Object



37
38
39
# File 'lib/puma/events.rb', line 37

def before_restart(&block)
  register(:before_restart, &block)
end

#fire(hook, *args) ⇒ Object

Fire callbacks for the named hook



16
17
18
# File 'lib/puma/events.rb', line 16

def fire(hook, *args)
  @hooks[hook].each { |t| t.call(*args) }
end

#fire_after_booted!Object



60
61
62
# File 'lib/puma/events.rb', line 60

def fire_after_booted!
  fire(:after_booted)
end

#fire_after_stopped!Object



68
69
70
# File 'lib/puma/events.rb', line 68

def fire_after_stopped!
  fire(:after_stopped)
end

#fire_before_restart!Object



64
65
66
# File 'lib/puma/events.rb', line 64

def fire_before_restart!
  fire(:before_restart)
end

#on_booted(&block) ⇒ Object



45
46
47
48
# File 'lib/puma/events.rb', line 45

def on_booted(&block)
  Puma.deprecate_method_change :on_booted, __callee__, :after_booted
  after_booted(&block)
end

#on_restart(&block) ⇒ Object



50
51
52
53
# File 'lib/puma/events.rb', line 50

def on_restart(&block)
  Puma.deprecate_method_change :on_restart, __callee__, :before_restart
  before_restart(&block)
end

#on_stopped(&block) ⇒ Object



55
56
57
58
# File 'lib/puma/events.rb', line 55

def on_stopped(&block)
  Puma.deprecate_method_change :on_stopped, __callee__, :after_stopped
  after_stopped(&block)
end

#register(hook, obj = nil, &blk) ⇒ Object

Register a callback for a given hook



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puma/events.rb', line 21

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