Class: Puma::CLI

### Home Classes Methods

### In Files

- lib/puma/cli.rb

### Parent

Object

### Methods

- ::new
- #clustered?
- #debug
- #delete_pidfile
- #env
- #error
- #graceful_stop
- #halt
- #jruby?
- #jruby_daemon_start
- #log
- #phased_restart
- #prune_bundler?
- #redirect_io
- #reload_worker_directory
- #restart
- #restart!
- #run
- #stats
- #stop
- #windows?
- #write_pid
- #write_state

### Files

- Manifest.txt

### Class/Module Index

Quicksearch

- Puma
- Puma::App
- Puma::App::Status
- Puma::Binder
- Puma::CLI
- Puma::Client
- Puma::Cluster
- Puma::Cluster::Worker
- Puma::CommonLogger
- Puma::ConfigDefault
- Puma::Configuration
- Puma::Configuration::ConfigMiddleware
- Puma::ConnectionError
- Puma::Const
- Puma::ControlCLI
- Puma::DSL
- Puma::Delegation
- Puma::Events
- Puma::Events::DefaultFormatter
- Puma::Events::PidFormatter
- Puma::IOBuffer
- Puma::JRubyRestart
- Puma::JavaIOBuffer
- Puma::MiniSSL
- Puma::MiniSSL::Context
- Puma::MiniSSL::SSLError
- Puma::MiniSSL::Server
- Puma::MiniSSL::Socket
- Puma::NullIO
- Puma::Rack
- Puma::Rack::Builder
- Puma::Rack::Options
- Puma::Rack::URLMap
- Puma::Reactor
- Puma::Runner
- Puma::Server
- Puma::Single
- Puma::TCPLogger
- Puma::ThreadPool
- Puma::ThreadPool::AutoTrim
- Puma::ThreadPool::Reaper
- Puma::UnsupportedOption
- Puma::Util
- Puma::Util::HeaderHash
- OpenSSL
- OpenSSL::SSL
- OpenSSL::SSL::SSLServer
- Rack
- Rack::Handler
- Rack::Handler::Puma
- IO
- IO::WaitReadable
- Object
- Process
- String

No matching classes.

# 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 34
    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

clustered?() click to toggle source

    # File lib/puma/cli.rb, line 82
    def clustered?
      @options[:workers] > 0
    end

debug(str) click to toggle source

    # File lib/puma/cli.rb, line 78
    def debug(str)
      @events.log "- #{str}" if @options[:debug]
    end

delete_pidfile() click to toggle source

    # File lib/puma/cli.rb, line 137
    def delete_pidfile
      path = @options[:pidfile]
      File.unlink(path) if path && File.exist?(path)
    end

env() click to toggle source

    # File lib/puma/cli.rb, line 98
    def env
      @options[:environment] || @cli_options[:environment] || ENV['RACK_ENV'] || 'development'
    end

error(str) click to toggle source

Delegate `error` to +@events+

    # File lib/puma/cli.rb, line 74
    def error(str)
      @events.error str
    end

graceful_stop() click to toggle source

    # File lib/puma/cli.rb, line 142
    def graceful_stop
      @runner.stop_blocked
      log "=== puma shutdown: #{Time.now} ==="
      log "- Goodbye!"
    end

halt() click to toggle source

    # File lib/puma/cli.rb, line 261
    def halt
      @status = :halt
      @runner.halt
    end

jruby?() click to toggle source

    # File lib/puma/cli.rb, line 90
    def jruby?
      IS_JRUBY
    end

jruby_daemon_start() click to toggle source

    # File lib/puma/cli.rb, line 148
    def jruby_daemon_start
      require 'puma/jruby_restart'
      JRubyRestart.daemon_start(@restart_dir, restart_args)
    end

log(str) click to toggle source

Delegate `log` to +@events+

    # File lib/puma/cli.rb, line 68
    def log(str)
      @events.log str
    end

phased_restart() click to toggle source

    # File lib/puma/cli.rb, line 245
    def phased_restart
      unless @runner.respond_to?(:phased_restart) and @runner.phased_restart
        log "* phased-restart called but not available, restarting normally."
        return restart
      end
      true
    end

prune_bundler?() click to toggle source

    # File lib/puma/cli.rb, line 86
    def prune_bundler?
      @options[:prune_bundler] && clustered? && !@options[:preload_app]
    end

redirect_io() click to toggle source

    # File lib/puma/cli.rb, line 253
    def redirect_io
      @runner.redirect_io
    end

reload_worker_directory() click to toggle source

    # File lib/puma/cli.rb, line 241
    def reload_worker_directory
      @runner.reload_worker_directory if @runner.respond_to?(:reload_worker_directory)
    end

restart() click to toggle source

    # File lib/puma/cli.rb, line 236
    def restart
      @status = :restart
      @runner.restart
    end

restart!() click to toggle source

    # File lib/puma/cli.rb, line 153
    def restart!
      @options[:on_restart].each do |block|
        block.call self
      end

      if jruby?
        close_binder_listeners

        require 'puma/jruby_restart'
        JRubyRestart.chdir_exec(@restart_dir, restart_args)
      elsif windows?
        close_binder_listeners

        argv = restart_args
        Dir.chdir(@restart_dir)
        argv += [redirects] if RUBY_VERSION >= '1.9'
        Kernel.exec(*argv)
      else
        redirects = {:close_others => true}
        @binder.listeners.each_with_index do |(l, io), i|
          ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
          redirects[io.to_i] = io.to_i
        end

        argv = restart_args
        Dir.chdir(@restart_dir)
        argv += [redirects] if RUBY_VERSION >= '1.9'
        Kernel.exec(*argv)
      end
    end

run() click to toggle source

Parse the options, load the rackup, start the server and wait for it to finish.

    # File lib/puma/cli.rb, line 187
    def run
      begin
        parse_options
      rescue UnsupportedOption
        exit 1
      end

      dir = @options[:directory]
      Dir.chdir(dir) if dir

      prune_bundler if prune_bundler?

      set_rack_environment

      if clustered?
        @events.formatter = Events::PidFormatter.new
        @options[:logger] = @events

        @runner = Cluster.new(self)
      else
        @runner = Single.new(self)
      end

      setup_signals
      set_process_title

      @status = :run

      @runner.run

      case @status
      when :halt
        log "* Stopping immediately!"
      when :run, :stop
        graceful_stop
      when :restart
        log "* Restarting..."
        @runner.before_restart
        restart!
      when :exit
        # nothing
      end
    end

stats() click to toggle source

    # File lib/puma/cli.rb, line 257
    def stats
      @runner.stats
    end

stop() click to toggle source

    # File lib/puma/cli.rb, line 231
    def stop
      @status = :stop
      @runner.stop
    end

windows?() click to toggle source

    # File lib/puma/cli.rb, line 94
    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 126
    def write_pid
      path = @options[:pidfile]
      return unless path

      File.open(path, 'w') { |f| f.puts Process.pid }
      cur = Process.pid
      at_exit do
        delete_pidfile if cur == Process.pid
      end
    end

write_state() click to toggle source

    # File lib/puma/cli.rb, line 102
    def write_state
      write_pid

      path = @options[:state]
      return unless path

      state = { 'pid' => Process.pid }
      cfg = @config.dup

      [
        :logger,
        :before_worker_shutdown, :before_worker_boot, :before_worker_fork,
        :after_worker_boot,
        :on_restart, :lowlevel_error_handler
      ].each { |k| cfg.options.delete(k) }
      state['config'] = cfg

      require 'yaml'
      File.open(path, 'w') { |f| f.write state.to_yaml }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.