Class: DopCommon::SignalHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dop_common/signal_handler.rb

Constant Summary collapse

DEFAULT_SIGNALS =
[:INT, :QUIT, :TERM]

Instance Method Summary collapse

Constructor Details

#initializeSignalHandler

Returns a new instance of SignalHandler.



15
16
17
18
# File 'lib/dop_common/signal_handler.rb', line 15

def initialize
  @signal_queue = []
  @self_reader, @self_writer = IO.pipe
end

Instance Method Details

#handle_signals(*signals) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dop_common/signal_handler.rb', line 20

def handle_signals(*signals)
  signals = DEFAULT_SIGNALS if signals.empty?
  old_handlers = setup_signal_traps(signals)
  loop do
    begin
      if @signal_queue.any?
        yield(@signal_queue.shift) if block_given?
      else
        IO.select([@self_reader])
        @self_reader.read_nonblock(1)
      end
    rescue
      break
    end
  end
  teardown_signal_traps(old_handlers)
end