Class: Procodile::SignalHandler
- Inherits:
-
Object
- Object
- Procodile::SignalHandler
- Defined in:
- lib/procodile/signal_handler.rb
Instance Attribute Summary collapse
-
#pipe ⇒ Object
readonly
Returns the value of attribute pipe.
Class Method Summary collapse
Instance Method Summary collapse
- #handle ⇒ Object
-
#initialize(*signals) ⇒ SignalHandler
constructor
A new instance of SignalHandler.
- #notice ⇒ Object
- #register(name, &block) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(*signals) ⇒ SignalHandler
Returns a new instance of SignalHandler.
10 11 12 13 14 15 16 17 |
# File 'lib/procodile/signal_handler.rb', line 10 def initialize(*signals) @handlers = {} reader, writer = IO.pipe @pipe = {:reader => reader, :writer => writer} signals.each do |sig| Signal.trap(sig, proc { SignalHandler.queue << sig ; notice }) end end |
Instance Attribute Details
#pipe ⇒ Object (readonly)
Returns the value of attribute pipe.
4 5 6 |
# File 'lib/procodile/signal_handler.rb', line 4 def pipe @pipe end |
Class Method Details
.queue ⇒ Object
6 7 8 |
# File 'lib/procodile/signal_handler.rb', line 6 def self.queue Thread.main[:signal_queue] ||= [] end |
Instance Method Details
#handle ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/procodile/signal_handler.rb', line 37 def handle if signal = self.class.queue.shift Procodile.log nil, 'system', "Supervisor received #{signal} signal" if @handlers[signal] @handlers[signal].each(&:call) end end end |
#notice ⇒ Object
33 34 35 |
# File 'lib/procodile/signal_handler.rb', line 33 def notice @pipe[:writer].write_nonblock('.') end |
#register(name, &block) ⇒ Object
28 29 30 31 |
# File 'lib/procodile/signal_handler.rb', line 28 def register(name, &block) @handlers[name] ||= [] @handlers[name] << block end |
#start ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/procodile/signal_handler.rb', line 19 def start Thread.new do loop do handle sleep 1 end end end |