Class: Procodile::SignalHandler

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*signals) ⇒ SignalHandler



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/procodile/signal_handler.rb', line 8

def initialize(*signals)
  @handlers = {}
  Thread.new do
    loop do
      if signal = self.class.queue.shift
        if @handlers[signal]
          @handlers[signal].each(&:call)
        end
      end
      sleep 1
    end
  end

  signals.each do |sig|
    Signal.trap(sig, proc { SignalHandler.queue << sig })
  end
end

Class Method Details

.queueObject



4
5
6
# File 'lib/procodile/signal_handler.rb', line 4

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

Instance Method Details

#register(name, &block) ⇒ Object



26
27
28
29
# File 'lib/procodile/signal_handler.rb', line 26

def register(name, &block)
  @handlers[name] ||= []
  @handlers[name] << block
end