class Garlando::Server

Constants

COMMANDS
OPTIONS

Public Class Methods

new(options={}) click to toggle source
# File lib/garlando.rb, line 18
def initialize(options={})
  @options = OPTIONS.merge options
end

Public Instance Methods

call(command) click to toggle source
# File lib/garlando.rb, line 22
def call(command)
  raise ArgumentError unless COMMANDS.include? command
  method(command).call
end
restart() click to toggle source
# File lib/garlando.rb, line 53
def restart
  stop if running?
  start
end
start() click to toggle source
# File lib/garlando.rb, line 27
def start
  abort 'server is already running?' if running?
  abort "File could not found (#{env_path})" unless File.exist?(env_path)

  Process.daemon nochdir = true

  File.write pid_path, Process.pid.to_s
  at_exit { File.unlink pid_path }

  prepare

  server.run application, Host: @options[:host], Port: @options[:port]
end
status() click to toggle source
# File lib/garlando.rb, line 58
def status
  if running?
    puts "server is running (#{File.read pid_path})"
  else
    puts 'server is not running'
  end
end
stop() click to toggle source
# File lib/garlando.rb, line 41
def stop
  return abort 'server is not running' unless running?

  pid = File.read(pid_path).to_i
  Timeout.timeout(10) do
    begin
      kill pid while running?
    rescue Errno::ESRCH
    end
  end
end

Private Instance Methods

application() click to toggle source
# File lib/garlando.rb, line 98
def application
  Rack::Logger.new(Rack::CommonLogger.new(Rack::URLMap.new(rails.config.assets[:prefix] => rails.assets)))
end
env_path() click to toggle source
# File lib/garlando.rb, line 81
def env_path
  File.join @options[:pwd], 'config/environment.rb'
end
kill(pid, sig=:INT) click to toggle source
# File lib/garlando.rb, line 68
def kill(pid, sig=:INT)
  Process.kill sig, pid
  sleep 0.5
end
log_path() click to toggle source
# File lib/garlando.rb, line 85
def log_path
  File.join @options[:pwd], @options[:log]
end
logger() click to toggle source
# File lib/garlando.rb, line 106
def logger
  Logger.new log_path
end
pid_path() click to toggle source
# File lib/garlando.rb, line 77
def pid_path
  File.join @options[:pwd], @options[:pid]
end
prepare() click to toggle source
# File lib/garlando.rb, line 135
def prepare
  reopen

  ENV['RACK_ENV'] = @options[:env]
  require env_path

  relogging

  spinup
end
rails() click to toggle source
# File lib/garlando.rb, line 102
def rails
  Rails.application
end
relogging() click to toggle source
# File lib/garlando.rb, line 110
def relogging
  rails.assets.logger = logger
end
reopen() click to toggle source
# File lib/garlando.rb, line 89
def reopen
  file = File.open log_path, 'a'
  [STDOUT, STDERR].each { |e| e.reopen file }
end
running?() click to toggle source
# File lib/garlando.rb, line 73
def running?
  File.exists? pid_path
end
server() click to toggle source
# File lib/garlando.rb, line 94
def server
  Rack::Handler.default
end
spinup() click to toggle source
# File lib/garlando.rb, line 114
def spinup
  check = lambda do |path|
    begin
      Net::HTTP.start(@options[:host], @options[:port]) do |http|
        http.open_timeout = http.read_timeout = nil
        http.get path
        throw :finish
      end
    rescue Errno::ECONNREFUSED
    end
  end
  [
    "#{rails.config.assets[:prefix]}/application.js",
    "#{rails.config.assets[:prefix]}/application.css",
  ].each do |_path|
    Thread.new(_path) do |path|
      catch(:finish) { loop { check.call path } }
    end
  end
end