class Phobos::CLI::Runner

Constants

SIGNALS

Attributes

executor[R]
reader[R]
signal_queue[R]
writer[R]

Public Class Methods

new() click to toggle source
# File lib/phobos/cli/runner.rb, line 8
def initialize
  @signal_queue = []
  @reader, @writer = IO.pipe
  @executor = Phobos::Executor.new
end

Public Instance Methods

run!() click to toggle source
# File lib/phobos/cli/runner.rb, line 14
def run!
  setup_signals
  executor.start

  loop do
    case signal_queue.pop
    when *SIGNALS
      executor.stop
      break
    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

Private Instance Methods

setup_signals() click to toggle source
# File lib/phobos/cli/runner.rb, line 36
def setup_signals
  SIGNALS.each do |signal|
    Signal.trap(signal) { unblock(signal) }
  end
end
unblock(signal) click to toggle source
# File lib/phobos/cli/runner.rb, line 42
def unblock(signal)
  writer.write_nonblock('.')
  signal_queue << signal
end