Class: Messaging::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/cli.rb

Overview

Starts up the consumer supervisor and handles signals

Constant Summary collapse

SIGNALS =
%i[INT TERM QUIT].freeze

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



11
12
13
14
15
# File 'lib/messaging/cli.rb', line 11

def initialize
  @signal_queue = []
  @reader, @writer = IO.pipe
  @consumer_supervisor = Messaging::ConsumerSupervisor.new
end

Instance Method Details

#runObject



17
18
19
20
21
# File 'lib/messaging/cli.rb', line 17

def run
  setup_signals
  consumer_supervisor.start
  wait_for_signal
end

#wait_for_signalObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/messaging/cli.rb', line 24

def wait_for_signal
  loop do
    case signal_queue.pop
    when *SIGNALS
      consumer_supervisor.stop
      break
    when "USR1"
      consumer_supervisor.status
    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