class PumpAction::Runner

Constants

FILETYPES

Attributes

child_pid[R]
paths[R]

Public Class Methods

new(config) click to toggle source
# File lib/pump_action.rb, line 12
def initialize(config)
  @rack_file = config[:rack_file]
  paths = config[:listen_to] || []
  @port = config[:port] || 9292
  set_traps!
  @listener = Listen.to(*paths, only: FILETYPES) do |modified, added, removed|
    restart_child!
  end
end

Public Instance Methods

run() click to toggle source
# File lib/pump_action.rb, line 22
def run
  @listener.start
  start_child!
  sleep(99999) while true
end
stop() click to toggle source
# File lib/pump_action.rb, line 28
def stop
  exit_child!
end

Private Instance Methods

exit_child!() click to toggle source
# File lib/pump_action.rb, line 34
def exit_child!
  if @child_pid
    puts "Killing child with PID #{@child_pid}"
    Process.kill 9, @child_pid
    Process.wait @child_pid
    puts "Child with PID #{@child_pid} is dead"
    @child_pid = nil
  end
end
restart_child!() click to toggle source
# File lib/pump_action.rb, line 52
def restart_child!
  exit_child!
  start_child!
end
set_traps!() click to toggle source
# File lib/pump_action.rb, line 57
def set_traps!
  ["INT", "TERM", "QUIT"].each do |signal|
    trap(signal) do
      exit_child!
      exit!
    end
  end
end
start_child!() click to toggle source
# File lib/pump_action.rb, line 44
def start_child!
  @child_pid = fork do
    app = Rack::Builder.parse_file(@rack_file).first
    Rack::Handler::WEBrick.run(app, Port: @port)
  end
  puts "Restarted child with PID #{@child_pid}"
end