In Files

Parent

Puma::CLI

Handles invoke a Puma::Server in a command line style.

Attributes

binder[R]

The Binder object containing the sockets bound to.

config[R]

The Configuration object used.

events[R]

The Events object used to output information.

options[R]

The Hash of options used to configure puma.

Public Class Methods

new(argv, events=Events.stdio) click to toggle source

Create a new CLI object using argv as the command line arguments.

stdout and stderr can be set to IO-like objects which this object will report status on.

# File lib/puma/cli.rb, line 27
def initialize(argv, events=Events.stdio)
  @debug = false
  @argv = argv

  @events = events

  @status = nil
  @runner = nil

  @config = nil

  ENV['NEWRELIC_DISPATCHER'] ||= "puma"

  setup_options
  generate_restart_data

  @binder = Binder.new(@events)
  @binder.import_from_env
end

Public Instance Methods

debug(str) click to toggle source
# File lib/puma/cli.rb, line 71
def debug(str)
  if @options[:debug]
    @events.log "- #{str}"
  end
end
delete_pidfile() click to toggle source
# File lib/puma/cli.rb, line 272
def delete_pidfile
  if path = @options[:pidfile]
    File.unlink path if File.exist? path
  end
end
error(str) click to toggle source

Delegate error to +@events+

# File lib/puma/cli.rb, line 67
def error(str)
  @events.error str
end
find_config() click to toggle source
# File lib/puma/cli.rb, line 278
def find_config
  if cfg = @options[:config_file]
    # Allow - to disable config finding
    if cfg == "-"
      @options[:config_file] = nil
      return
    end

    return
  end

  pos = []

  if env = (@options[:environment] || ENV['RACK_ENV'])
    pos << "config/puma/#{env}.rb"
  end

  pos << "config/puma.rb"
  @options[:config_file] = pos.find { |f| File.exist? f }
end
jruby?() click to toggle source
# File lib/puma/cli.rb, line 77
def jruby?
  IS_JRUBY
end
log(str) click to toggle source

Delegate log to +@events+

# File lib/puma/cli.rb, line 61
def log(str)
  @events.log str
end
set_rack_environment() click to toggle source
# File lib/puma/cli.rb, line 260
def set_rack_environment
  # Try the user option first, then the environment variable,
  # finally default to development

  env = @options[:environment] ||
               ENV['RACK_ENV'] ||
                 'development'

  @options[:environment] = env
  ENV['RACK_ENV'] = env
end
setup_options() click to toggle source

Build the OptionParser object to handle the available options.

# File lib/puma/cli.rb, line 93
def setup_options
  @options = {
    :min_threads => 0,
    :max_threads => 16,
    :quiet => false,
    :debug => false,
    :binds => [],
    :workers => 0,
    :daemon => false,
    :before_worker_boot => [],
    :after_worker_boot => []
  }

  @parser = OptionParser.new do |o|
    o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
      @options[:binds] << arg
    end

    o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
      @options[:config_file] = arg
    end

    o.on "--control URL", "The bind url to use for the control server",
                          "Use 'auto' to use temp unix server" do |arg|
      if arg
        @options[:control_url] = arg
      elsif jruby?
        unsupported "No default url available on JRuby"
      end
    end

    o.on "--control-token TOKEN",
         "The token to use as authentication for the control server" do |arg|
      @options[:control_auth_token] = arg
    end

    o.on "-d", "--daemon", "Daemonize the server into the background" do
      @options[:daemon] = true
      @options[:quiet] = true
    end

    o.on "--debug", "Log lowlevel debugging information" do
      @options[:debug] = true
    end

    o.on "--dir DIR", "Change to DIR before starting" do |d|
      @options[:directory] = d.to_s
      @options[:worker_directory] = d.to_s
    end

    o.on "-e", "--environment ENVIRONMENT",
         "The environment to run the Rack app on (default development)" do |arg|
      @options[:environment] = arg
    end

    o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
      $LOAD_PATH.unshift(*arg.split(':'))
    end

    o.on "-p", "--port PORT", "Define the TCP port to bind to",
                              "Use -b for more advanced options" do |arg|
      @options[:binds] << "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
    end

    o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
      @options[:pidfile] = arg
    end

    o.on "--preload", "Preload the app. Cluster mode only" do
      @options[:preload_app] = true
    end

    o.on "--prune-bundler", "Prune out the bundler env if possible" do
      @options[:prune_bundler] = true
    end

    o.on "-q", "--quiet", "Quiet down the output" do
      @options[:quiet] = true
    end

    o.on "-R", "--restart-cmd CMD",
         "The puma command to run during a hot restart",
         "Default: inferred" do |cmd|
      @options[:restart_cmd] = cmd
    end

    o.on "-S", "--state PATH", "Where to store the state details" do |arg|
      @options[:state] = arg
    end

    o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
      min, max = arg.split(":")
      if max
        @options[:min_threads] = min
        @options[:max_threads] = max
      else
        @options[:min_threads] = 0
        @options[:max_threads] = arg
      end
    end

    o.on "--tcp-mode", "Run the app in raw TCP mode instead of HTTP mode" do
      @options[:mode] = :tcp
    end

    o.on "-V", "--version", "Print the version information" do
      puts "puma version #{Puma::Const::VERSION}"
      exit 1
    end

    o.on "-w", "--workers COUNT",
               "Activate cluster mode: How many worker processes to create" do |arg|
      @options[:workers] = arg.to_i
    end

    o.on "--tag NAME", "Additional text to display in process listing" do |arg|
      @options[:tag] = arg
    end
  end

  @parser.banner = "puma <options> <rackup file>"

  @parser.on_tail "-h", "--help", "Show help" do
    log @parser
    exit 1
  end
end
unsupported(str, cond=true) click to toggle source
# File lib/puma/cli.rb, line 85
def unsupported(str, cond=true)
  return unless cond
  @events.error str
  raise UnsupportedOption
end
windows?() click to toggle source
# File lib/puma/cli.rb, line 81
def windows?
  RUBY_PLATFORM =~ /mswin32|ming32/
end
write_pid() click to toggle source

If configured, write the pid of the current process out to a file.

# File lib/puma/cli.rb, line 244
def write_pid
  if path = @options[:pidfile]
    File.open(path, "w") do |f|
      f.puts Process.pid
    end

    cur = Process.pid

    at_exit do
      if cur == Process.pid
        delete_pidfile
      end
    end
  end
end
write_state() click to toggle source
# File lib/puma/cli.rb, line 221
def write_state
  write_pid

  require 'yaml'

  if path = @options[:state]
    state = { "pid" => Process.pid }

    cfg = @config.dup

    [ :logger, :before_worker_boot, :after_worker_boot, :on_restart ].each { |o| cfg.options.delete o }

    state["config"] = cfg

    File.open(path, "w") do |f|
      f.write state.to_yaml
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.