class GracefulShutdown

Constants

DEFAULT_SIGNALS
HANDLER

Public Instance Methods

handle_signals(*signals) { || ... } click to toggle source

Execute a block of code with signal handlers.

# File lib/graceful_shutdown.rb, line 36
def handle_signals(*signals)
  signals = DEFAULT_SIGNALS if signals.empty?

  handlers = setup(signals)
  yield if block_given?
  teardown(handlers)
rescue Shutdown
  teardown(handlers)
  exit
end

Private Instance Methods

setup(signals) click to toggle source

Setup signal traps, keeping track of the original handlers. NOTE If something else below this sets a trap, these traps will not be invoked.

# File lib/graceful_shutdown.rb, line 52
def setup(signals)
  signals.each_with_object({}) do |signal, handlers|
    handlers[signal] = trap(signal, HANDLER)
  end
end
teardown(handlers) click to toggle source

Restore original signal handlers.

# File lib/graceful_shutdown.rb, line 59
def teardown(handlers)
  handlers.each do |signal, handler|
    trap(signal, handler)
  end
end