Class: Whoosh::Shutdown

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/shutdown.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ Shutdown

Returns a new instance of Shutdown.



5
6
7
8
9
# File 'lib/whoosh/shutdown.rb', line 5

def initialize(logger: nil)
  @hooks = []
  @logger = logger
  @executed = false
end

Instance Method Details

#execute!Object



15
16
17
18
19
20
21
22
23
# File 'lib/whoosh/shutdown.rb', line 15

def execute!
  return if @executed
  @executed = true
  @hooks.reverse_each do |hook|
    hook.call
  rescue => e
    @logger&.error("shutdown_hook_error", message: e.message)
  end
end

#install_signal_handlers!Object



25
26
27
28
# File 'lib/whoosh/shutdown.rb', line 25

def install_signal_handlers!
  trap("TERM") { execute! }
  trap("INT") { execute! }
end

#register(&block) ⇒ Object



11
12
13
# File 'lib/whoosh/shutdown.rb', line 11

def register(&block)
  @hooks << block
end