Class: Procodile::SignalHandler

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pipeObject (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

.queueObject



6
7
8
# File 'lib/procodile/signal_handler.rb', line 6

def self.queue
  Thread.main[:signal_queue] ||= []
end

Instance Method Details

#handleObject



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

#noticeObject



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

#startObject



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