class Guard::Restarter

Constants

VERSION

Public Class Methods

new(watchers = [], options = {}) click to toggle source
Calls superclass method
# File lib/guard/restarter.rb, line 9
def initialize(watchers = [], options = {})
  super
  @pid = nil
  @command = options[:command]
  @spawn = options[:spawn]
  raise "Must provide option :command or :spawn" unless @command || @spawn
end

Public Instance Methods

run_on_changes(paths) click to toggle source
# File lib/guard/restarter.rb, line 22
def run_on_changes(paths)
  run_info
  stop_server
  start_server
end
start() click to toggle source
# File lib/guard/restarter.rb, line 17
def start()
  start_info
  start_server
end
stop() click to toggle source
# File lib/guard/restarter.rb, line 28
def stop() stop_server end

Private Instance Methods

run_info() click to toggle source
# File lib/guard/restarter.rb, line 72
def run_info
  if @pid
    UI.info "Process started, pid #{@pid}"
  else
    UI.info "Error starting process."
  end
end
server_is_running() click to toggle source
# File lib/guard/restarter.rb, line 60
def server_is_running()
  !!Process.kill(0, @pid) rescue false
end
start_info() click to toggle source
# File lib/guard/restarter.rb, line 64
def start_info
  if @command
    UI.info "Running command '#{@command}'..."
  else
    UI.info "Running spawn: #{@spawn.inspect}..."
  end
end
start_server() click to toggle source
# File lib/guard/restarter.rb, line 32
def start_server
  custom_spawn_args = { chdir: Dir.pwd, pgroup: true }
  if @command
    args = [@command, custom_spawn_args]
  elsif @spawn.last.is_a? Hash
    args = @spawn[0..-2] << @spawn.last.merge(custom_spawn_args)
  else
    args = @spawn << custom_spawn_args
  end
  @pid = spawn(*args)
  Process.detach(@pid)
end
stop_server() click to toggle source
# File lib/guard/restarter.rb, line 45
def stop_server
  unless @pid && @pid > 0 && server_is_running
    UI.info "Error: server not started."
    return
  end
  UI.info "Stopping server..."
  Process.kill "INT", -@pid
  20.times do
    return unless server_is_running
    sleep 0.05
  end
  UI.info "Killing server forcibly."
  Process.kill "KILL", -@pid
end