Class: Sigurd::SignalHandler
- Inherits:
-
Object
- Object
- Sigurd::SignalHandler
- Defined in:
- lib/sigurd/signal_handler.rb
Overview
Class that takes any object with a “start” and “stop” method and catches signals to ask them to stop nicely.
Constant Summary collapse
- SIGNALS =
%i(INT TERM QUIT).freeze
Instance Method Summary collapse
-
#initialize(runner) ⇒ SignalHandler
constructor
Takes any object that responds to the ‘start` and `stop` methods.
-
#run! ⇒ Object
Run the runner.
Constructor Details
#initialize(runner) ⇒ SignalHandler
Takes any object that responds to the ‘start` and `stop` methods.
11 12 13 14 15 |
# File 'lib/sigurd/signal_handler.rb', line 11 def initialize(runner) @signal_queue = [] @reader, @writer = IO.pipe @runner = runner end |
Instance Method Details
#run! ⇒ Object
Run the runner.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sigurd/signal_handler.rb', line 18 def run! setup_signals @runner.start loop do case signal_queue.pop when *SIGNALS @runner.stop break else ready = IO.select([reader, writer]) # drain the self-pipe so it won't be returned again next time reader.read_nonblock(1) if ready[0].include?(reader) end end end |