class Emmy::Runner

Constants

BIN_EMMY
RUBY

Attributes

action[RW]
argv[RW]
config[RW]
env[RW]
option_parser[RW]

Public Class Methods

new() click to toggle source
# File lib/emmy/runner.rb, line 18
def initialize
  @argv = ARGV
  @env  = ENV
  @config = EmmyHttp::Configuration.new
  @action = :start_server

  on :bootstrap do
    parse_environment!
  end
  on :bootstrap do
    defaults!
  end

  on :instance do |id|
    instance_defaults!(id)
  end

  on :parse_options do
    option_parser.parse!(argv)
  end
  on :parse_options do
    update_rack_environment!
  end
end

Public Instance Methods

bootstrap!() click to toggle source
# File lib/emmy/runner.rb, line 204
def bootstrap!
  bootstrap.instance_fire(self)
end
configure(&b) click to toggle source
# File lib/emmy/runner.rb, line 196
def configure(&b)
  on :bootstrap, &b
end
daemonize_server() click to toggle source
# File lib/emmy/runner.rb, line 122
def daemonize_server
  each_server do
    Process.fork do
      Process.setsid
      exit if fork

      Fibre.reset
      # Boot instance
      instance!

      scope_pid(Process.pid) do |pid|
        puts pid
        File.umask(0000)
        bind_standard_streams
        start_server
      end
    end
  end
  sleep(1)
end
defaults!() click to toggle source
# File lib/emmy/runner.rb, line 93
def defaults!
  if config.environment == "development"
    config.stdout = "#{config.backend}.stdout"
    config.stderr = config.stdout
  end

  config.pid = "#{config.backend}.pid"
  config.log = "#{config.backend}.log"

  config.adapter ||= EmmyExtends::Thin::Controller rescue nil
end
display_help() click to toggle source
# File lib/emmy/runner.rb, line 183
def display_help
  puts option_parser
end
display_version() click to toggle source
# File lib/emmy/runner.rb, line 187
def display_version
  puts Emmy::VERSION
end
each(&b) click to toggle source
# File lib/emmy/runner.rb, line 200
def each(&b)
  on :instance, &b
end
error(message) click to toggle source
# File lib/emmy/runner.rb, line 191
def error(message)
  puts message
  exit
end
execute_bin_emmy() click to toggle source
# File lib/emmy/runner.rb, line 43
def execute_bin_emmy
  return false unless File.file?(BIN_EMMY)
  exec RUBY, BIN_EMMY, *argv
  true
end
instance!() click to toggle source
# File lib/emmy/runner.rb, line 208
def instance!
  instance.instance_fire(self, config.id)
end
instance_defaults!(id) click to toggle source
# File lib/emmy/runner.rb, line 105
def instance_defaults!(id)
  if config.id
    config.url.port += id if config.servers
    config.pid  = "#{config.backend}#{id}.pid"
    config.log  = "#{config.backend}#{id}.log"
  end
end
parse_environment!() click to toggle source
# File lib/emmy/runner.rb, line 49
def parse_environment!
  config.environment = env['EMMY_ENV'] || env['RACK_ENV'] || 'development'
end
run_action() click to toggle source
# File lib/emmy/runner.rb, line 113
def run_action
  # Bootstrap
  bootstrap! if @action != :display_help
  parse_options!
  # start action
  send(action)
  self
end
show_configuration() click to toggle source
# File lib/emmy/runner.rb, line 175
def show_configuration
  puts "Server configuration:"
  config.attributes.each do |name, value|
    value = "off" if value.nil?
    puts "  #{name}: #{value}"
  end
end
start_console() click to toggle source
# File lib/emmy/runner.rb, line 162
def start_console
  EmmyMachine.run_once do
    if defined?(binding.pry)
      TOPLEVEL_BINDING.pry
    else
      require 'irb'
      require 'irb/completion'

      IRB.start
    end
  end
end
start_server() click to toggle source
# File lib/emmy/runner.rb, line 143
def start_server
  Emmy.run do
    trap("INT")  { Emmy.stop }
    trap("TERM") { Emmy.stop }

    Emmy.fiber_block do
      file = backend_file
      Backend.module_eval(File.read(file), file)
    end
  end
end
stop_server() click to toggle source
# File lib/emmy/runner.rb, line 155
def stop_server
  each_server do
    instance!
    stop_pid(config.pid)
  end
end
update_rack_environment!() click to toggle source
# File lib/emmy/runner.rb, line 53
def update_rack_environment!
  ENV['RACK_ENV'] = config.environment
end

Private Instance Methods

backend_file() click to toggle source
# File lib/emmy/runner.rb, line 226
def backend_file
  thin = (config.backend == 'backend') ? EmmyExtends::Thin::EMMY_BACKEND : nil rescue nil
  backends = [
    "#{Dir.getwd}/#{config.backend}.em",
    "#{Dir.getwd}/config/#{config.backend}.em",
    thin
  ].compact

  backends.each do |file|
    return file if File.readable_real?(file)
  end
  error "Can't find backend in #{backends.inspect} places."
end
bind_standard_streams() click to toggle source
# File lib/emmy/runner.rb, line 269
def bind_standard_streams
  STDIN.reopen("/dev/null")
  STDOUT.reopen(config.stdout, "a") if config.stdout

  if config.stdout == config.stderr
    STDERR.reopen(STDOUT)
  else
    STDERR.reopen(config.stderr, "a") if config.stderr
  end
end
delete_pid() click to toggle source
# File lib/emmy/runner.rb, line 265
def delete_pid
  File.delete(config.pid)
end
each_server() { || ... } click to toggle source
# File lib/emmy/runner.rb, line 214
def each_server
  unless config.servers
    yield
    return
  end

  config.servers.times do |id|
    config.id = id
    yield
  end
end
scope_pid(pid) { |pid| ... } click to toggle source
# File lib/emmy/runner.rb, line 240
def scope_pid(pid)
  FileUtils.mkdir_p(File.dirname(config.pid))
  stop_pid(config.pid)
  File.open(config.pid, 'w') { |f| f.write(pid) }
  if block_given?
    yield pid
    delete_pid
  end
end
stop_pid(pid_file) click to toggle source
# File lib/emmy/runner.rb, line 250
def stop_pid(pid_file)
  return unless File.exists?(pid_file)

  pid = File.read(pid_file).to_i
  unless pid.zero?
    Process.kill("TERM", pid)
    puts "Stopping..."
    while File.exists?(config.pid)
      sleep(0.1)
    end
    #Process.wait(pid)
  end
rescue Errno::ESRCH
end