class Guard::Shotgun
Constants
- STARTUP_TIMEOUT
- VALID_ARGS
Attributes
pid[RW]
Public Class Methods
new(options={})
click to toggle source
Calls superclass method
# File lib/guard/shotgun.rb, line 16 def initialize(options={}) super @options = { host: 'localhost', port: 9292, server: "WEBrick" }.update(options) { |key, oldval, newval| (newval.nil? || newval.empty?) ? oldval : newval } @reloaded = false end
Public Instance Methods
reload()
click to toggle source
Call with Ctrl-Z signal
# File lib/guard/shotgun.rb, line 67 def reload @reloaded = true restart end
run_on_change(paths = {})
click to toggle source
Call on file(s) modifications
# File lib/guard/shotgun.rb, line 73 def run_on_change(paths = {}) @reloaded = true restart_without_waiting end
start()
click to toggle source
Call once when guard starts
# File lib/guard/shotgun.rb, line 31 def start UI.info "Starting up Rack..." if running? UI.error "Another instance of Rack is running." false else @pid = Spoon.spawnp 'rackup', *(options_array << (config_file if config_file)).reject(&:nil?) end wait_for_port if running? Notifier.notify(@reloaded ? 'reloaded' : 'up') @reloaded = false else UI.info "Rack failed to start." Notifier.notify('failed') end end
stop()
click to toggle source
Call with Ctrl-C signal (when Guard
quit)
# File lib/guard/shotgun.rb, line 50 def stop UI.info "Shutting down Rack..." Process.kill("TERM", @pid) Process.wait(@pid) @pid = nil true end
stop_without_waiting()
click to toggle source
# File lib/guard/shotgun.rb, line 58 def stop_without_waiting UI.info "Shutting down Rack without waiting..." Process.kill("KILL", @pid) Process.wait(@pid) @pid = nil true end
Private Instance Methods
config_file()
click to toggle source
# File lib/guard/shotgun.rb, line 80 def config_file @options.fetch :config, 'config.ru' end
options_array()
click to toggle source
# File lib/guard/shotgun.rb, line 84 def options_array @options.each_with_object([]) do |(key, val), array| key = key.to_s.downcase if VALID_ARGS.include? key array << "--#{key}" << val.to_s end end end
port_open?(addr, port)
click to toggle source
thanks to: bit.ly/bVN5AQ
# File lib/guard/shotgun.rb, line 127 def port_open?(addr, port) begin Timeout::timeout(1) do begin s = TCPSocket.new(addr, port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error end return false end
restart()
click to toggle source
# File lib/guard/shotgun.rb, line 99 def restart UI.info "Restarting Rack..." stop start end
restart_without_waiting()
click to toggle source
# File lib/guard/shotgun.rb, line 93 def restart_without_waiting UI.info "Restarting Rack without waiting..." stop_without_waiting start end
running?()
click to toggle source
# File lib/guard/shotgun.rb, line 105 def running? begin if @pid Process.getpgid @pid true else false end rescue Errno::ESRCH false end end
wait_for_port()
click to toggle source
# File lib/guard/shotgun.rb, line 118 def wait_for_port timeout_time = Time.now + STARTUP_TIMEOUT while Time.now < timeout_time do sleep 0.2 break if port_open?(@options[:host], @options[:port]) end end