Class: EventBus::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/event_bus/daemon.rb

Constant Summary collapse

INTERRUPTION_SIGNALS =
%w(TERM INT)

Class Method Summary collapse

Class Method Details

.bind_signalsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/event_bus/daemon.rb', line 17

def self.bind_signals
  read, write = IO.pipe

  INTERRUPTION_SIGNALS.each do |signal|
    Signal.trap(signal) { write.puts(signal) }
  end

  begin
    while io = IO.select([read])
      signal = io.first[0].gets.strip
      raise Interrupt if INTERRUPTION_SIGNALS.include?(signal)
    end
  rescue Interrupt
    stop
  end
end

.startObject



5
6
7
8
9
# File 'lib/event_bus/daemon.rb', line 5

def self.start
  Listeners::Manager.bind_all_listeners

  bind_signals
end

.stopObject



11
12
13
14
15
# File 'lib/event_bus/daemon.rb', line 11

def self.stop
  EventBus::Config.broker.close_connection

  exit
end