class MachineGun

An automatic reloading Rack development web server for Ruby.

Public Class Methods

new() click to toggle source
# File lib/machinegun.rb, line 13
def initialize
  @running = false
end
run(*args) click to toggle source

Helper method to instantiate a new object and run the server. @see run

# File lib/machinegun.rb, line 9
def self.run *args
  new.run *args
end

Public Instance Methods

run(opts = {}) click to toggle source

Run the automatically-reloading web server. This method blocks. @option opts [Numeric] :interval (0.5) Interval in seconds to scan the

filesystem for changes.
# File lib/machinegun.rb, line 21
def run opts = {}
  @running = true
  interval = (opts[:interval] || 0.5).to_f # FileWatcher interval must be numeric.
   
  @pid = start_server
   
  @watcher = FileWatcher.new("./**/*.rb")
  
  @watcher.watch interval do
    stop_server
    @pid = start_server
  end
end
running?() click to toggle source

@return true if the server is running.

# File lib/machinegun.rb, line 45
def running?
  @running
end
stop() click to toggle source

Stop watching for file changes and shutdown the web server.

# File lib/machinegun.rb, line 36
def stop
  return unless @running
  
  @watcher.stop
  stop_server
  @running = false
end

Private Instance Methods

start_server() click to toggle source

Start the web server in a forked process. @return process id

# File lib/machinegun.rb, line 53
def start_server
  fork do
    $0 = "rack"
    Rack::Server.start
  end
end
stop_server() click to toggle source

Gracefully stop the web server’s forked process.

# File lib/machinegun.rb, line 61
def stop_server
  Process.kill "INT", @pid
  Process.wait @pid
end