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 :on_booted, :on_restart, and :on_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

#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_on_booted!Object



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

def fire_on_booted!
  fire(:on_booted)
end

#fire_on_restart!Object



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

def fire_on_restart!
  fire(:on_restart)
end

#fire_on_stopped!Object



53
54
55
# File 'lib/puma/events.rb', line 53

def fire_on_stopped!
  fire(:on_stopped)
end

#on_booted(&block) ⇒ Object



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

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

#on_restart(&block) ⇒ Object



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

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

#on_stopped(&block) ⇒ Object



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

def on_stopped(&block)
  register(:on_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