class Puma::Runner

Generic class that is used by `Puma::Cluster` and `Puma::Single` to serve requests. This class spawns a new instance of `Puma::Server` via a call to `start_server`.

Public Class Methods

new(cli, events) click to toggle source
# File lib/puma/runner.rb, line 11
def initialize(cli, events)
  @launcher = cli
  @events = events
  @options = cli.options
  @app = nil
  @control = nil
end

Public Instance Methods

app() click to toggle source
# File lib/puma/runner.rb, line 156
def app
  @app ||= @launcher.config.app
end
before_restart() click to toggle source
# File lib/puma/runner.rb, line 35
def before_restart
  @control.stop(true) if @control
end
daemon?() click to toggle source
# File lib/puma/runner.rb, line 19
def daemon?
  @options[:daemon]
end
debug(str) click to toggle source
# File lib/puma/runner.rb, line 43
def debug(str)
  @events.log "- #{str}" if @options[:debug]
end
development?() click to toggle source
# File lib/puma/runner.rb, line 23
def development?
  @options[:environment] == "development"
end
error(str) click to toggle source
# File lib/puma/runner.rb, line 39
def error(str)
  @events.error str
end
load_and_bind() click to toggle source
# File lib/puma/runner.rb, line 139
def load_and_bind
  unless @launcher.config.app_configured?
    error "No application configured, nothing to run"
    exit 1
  end

  # Load the app before we daemonize.
  begin
    @app = @launcher.config.app
  rescue Exception => e
    log "! Unable to load application: #{e.class}: #{e.message}"
    raise e
  end

  @launcher.binder.parse @options[:binds], self
end
log(str) click to toggle source
# File lib/puma/runner.rb, line 31
def log(str)
  @events.log str
end
output_header(mode) click to toggle source
# File lib/puma/runner.rb, line 95
def output_header(mode)
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  log "Puma starting in #{mode} mode..."
  log "* Version #{Puma::Const::PUMA_VERSION} (#{ruby_engine}), codename: #{Puma::Const::CODE_NAME}"
  log "* Min threads: #{min_t}, max threads: #{max_t}"
  log "* Environment: #{ENV['RACK_ENV']}"

  if @options[:mode] == :tcp
    log "* Mode: Lopez Express (tcp)"
  end
end
redirect_io() click to toggle source
# File lib/puma/runner.rb, line 113
def redirect_io
  stdout = @options[:redirect_stdout]
  stderr = @options[:redirect_stderr]
  append = @options[:redirect_append]

  if stdout
    unless Dir.exist?(File.dirname(stdout))
      raise "Cannot redirect STDOUT to #{stdout}"
    end

    STDOUT.reopen stdout, (append ? "a" : "w")
    STDOUT.sync = true
    STDOUT.puts "=== puma startup: #{Time.now} ==="
  end

  if stderr
    unless Dir.exist?(File.dirname(stderr))
      raise "Cannot redirect STDERR to #{stderr}"
    end

    STDERR.reopen stderr, (append ? "a" : "w")
    STDERR.sync = true
    STDERR.puts "=== puma startup: #{Time.now} ==="
  end
end
redirected_io?() click to toggle source
# File lib/puma/runner.rb, line 109
def redirected_io?
  @options[:redirect_stdout] || @options[:redirect_stderr]
end
ruby_engine() click to toggle source
# File lib/puma/runner.rb, line 83
def ruby_engine
  if !defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby"
    "ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  else
    if defined?(RUBY_ENGINE_VERSION)
      "#{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} - ruby #{RUBY_VERSION}"
    else
      "#{RUBY_ENGINE} #{RUBY_VERSION}"
    end
  end
end
start_control() click to toggle source
# File lib/puma/runner.rb, line 47
def start_control
  str = @options[:control_url]
  return unless str

  require 'puma/app/status'

  uri = URI.parse str

  app = Puma::App::Status.new @launcher

  if token = @options[:control_auth_token]
    app.auth_token = token unless token.empty? or token == :none
  end

  control = Puma::Server.new app, @launcher.events
  control.min_threads = 0
  control.max_threads = 1

  case uri.scheme
  when "tcp"
    log "* Starting control server on #{str}"
    control.add_tcp_listener uri.host, uri.port
  when "unix"
    log "* Starting control server on #{str}"
    path = "#{uri.host}#{uri.path}"
    mask = @options[:control_url_umask]

    control.add_unix_listener path, mask
  else
    error "Invalid control URI: #{str}"
  end

  control.run
  @control = control
end
start_server() click to toggle source
# File lib/puma/runner.rb, line 160
def start_server
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  server = Puma::Server.new app, @launcher.events, @options
  server.min_threads = min_t
  server.max_threads = max_t
  server.inherit_binder @launcher.binder

  if @options[:mode] == :tcp
    server.tcp_mode!
  end

  if @options[:early_hints]
    server.early_hints = true
  end

  unless development? || test?
    server.leak_stack_on_error = false
  end

  server
end
test?() click to toggle source
# File lib/puma/runner.rb, line 27
def test?
  @options[:environment] == "test"
end