2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-09-27 12:23:03 -04:00
|
|
|
require 'optparse'
|
2011-09-27 13:53:45 -04:00
|
|
|
require 'uri'
|
|
|
|
|
Expose top level Puma.stats API
Right now to get stats for a puma process the only way is to go through the control server. The puma-stats-logger does this by reading in a state file then querying the control server manually.
https://github.com/hired/puma-stats-logger/blob/7ad7798e9d06ff44e047ac56d0307c4ff9c73751/lib/puma_stats_logger/middleware.rb#L28
Instead, I’m proposing adding a top level `Puma.stats` method that will allow anyone inside of the same process to get access to the stats.
This could be instrumented by a gem to theoretically export these stats to say a Heroku dashboard where we could list out backlog or thread count.
The format of stats is a hash, and will change depending on if the server is in “single” or “clustered” mode.
Clustered:
```
{ "workers": 2, "phase": 0, "booted_workers": 2, "old_workers": 0, "worker_status": [{ "pid": 19832, "index": 0, "phase": 0, "booted": true, "last_checkin": "2018-03-12T16:03:12Z", "last_status": { "backlog":0, "running":5 } },{ "pid": 19833, "index": 1, "phase": 0, "booted": true, "last_checkin": "2018-03-12T16:03:12Z", "last_status": { "backlog":0, "running":5 } }] }
```
Single:
```
{ "backlog": 0, "running": 2 }
```
Alternatively if we could somehow enable another process to get these stats from Puma via pumactl by default without requiring any additional in app config, that would also work.
2018-03-12 12:41:39 -04:00
|
|
|
require 'puma'
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/configuration'
|
2016-01-14 10:41:55 -05:00
|
|
|
require 'puma/launcher'
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/const'
|
|
|
|
require 'puma/events'
|
2011-11-22 17:15:24 -05:00
|
|
|
|
2011-09-27 12:23:03 -04:00
|
|
|
module Puma
|
2015-03-13 18:07:08 -04:00
|
|
|
class << self
|
|
|
|
# The CLI exports its Puma::Configuration object here to allow
|
|
|
|
# apps to pick it up. An app needs to use it conditionally though
|
|
|
|
# since it is not set if the app is launched via another
|
|
|
|
# mechanism than the CLI class.
|
|
|
|
attr_accessor :cli_config
|
|
|
|
end
|
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Handles invoke a Puma::Server in a command line style.
|
|
|
|
#
|
2011-09-27 12:23:03 -04:00
|
|
|
class CLI
|
2016-02-04 10:49:01 -05:00
|
|
|
KEYS_NOT_TO_PERSIST_IN_STATE = Launcher::KEYS_NOT_TO_PERSIST_IN_STATE
|
2015-12-10 03:01:47 -05:00
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# 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.
|
|
|
|
#
|
2013-07-05 19:54:15 -04:00
|
|
|
def initialize(argv, events=Events.stdio)
|
2012-08-01 13:11:27 -04:00
|
|
|
@debug = false
|
2016-02-06 22:00:29 -05:00
|
|
|
@argv = argv.dup
|
2011-09-27 12:23:03 -04:00
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
@events = events
|
2011-09-27 17:33:17 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@conf = nil
|
|
|
|
|
|
|
|
@stdout = nil
|
|
|
|
@stderr = nil
|
|
|
|
@append = false
|
|
|
|
|
|
|
|
@control_url = nil
|
|
|
|
@control_options = {}
|
2011-12-05 14:15:44 -05:00
|
|
|
|
2011-09-27 12:23:03 -04:00
|
|
|
setup_options
|
2012-04-04 11:38:22 -04:00
|
|
|
|
2016-01-14 10:41:55 -05:00
|
|
|
begin
|
|
|
|
@parser.parse! @argv
|
2016-02-06 22:00:29 -05:00
|
|
|
|
|
|
|
if file = @argv.shift
|
2017-03-03 16:11:59 -05:00
|
|
|
@conf.configure do |user_config, file_config|
|
|
|
|
file_config.rackup file
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
rescue UnsupportedOption
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2017-03-03 16:11:59 -05:00
|
|
|
@conf.configure do |user_config, file_config|
|
2016-02-06 22:00:29 -05:00
|
|
|
if @stdout || @stderr
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.stdout_redirect @stdout, @stderr, @append
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2012-09-10 20:14:35 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if @control_url
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.activate_control_app @control_url, @control_options
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
|
|
|
end
|
2012-09-10 20:14:35 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@launcher = Puma::Launcher.new(@conf, :events => @events, :argv => argv)
|
2012-04-11 14:08:46 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
attr_reader :launcher
|
2013-07-06 00:13:29 -04:00
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Parse the options, load the rackup, start the server and wait
|
|
|
|
# for it to finish.
|
|
|
|
#
|
2011-11-22 00:15:40 -05:00
|
|
|
def run
|
2016-01-14 10:41:55 -05:00
|
|
|
@launcher.run
|
2013-02-05 22:09:40 -05:00
|
|
|
end
|
|
|
|
|
2014-02-19 12:22:11 -05:00
|
|
|
private
|
2015-03-13 21:23:09 -04:00
|
|
|
def unsupported(str)
|
|
|
|
@events.error(str)
|
|
|
|
raise UnsupportedOption
|
|
|
|
end
|
|
|
|
|
2018-05-09 14:38:12 -04:00
|
|
|
def configure_control_url(command_line_arg)
|
|
|
|
if command_line_arg
|
|
|
|
@control_url = command_line_arg
|
|
|
|
elsif Puma.jruby?
|
|
|
|
unsupported "No default url available on JRuby"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-13 19:08:07 -04:00
|
|
|
# Build the OptionParser object to handle the available options.
|
|
|
|
#
|
2015-08-14 19:55:11 -04:00
|
|
|
|
2015-03-13 19:08:07 -04:00
|
|
|
def setup_options
|
2017-03-03 16:11:59 -05:00
|
|
|
@conf = Configuration.new do |user_config, file_config|
|
2016-02-06 22:00:29 -05:00
|
|
|
@parser = OptionParser.new do |o|
|
|
|
|
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.bind arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
file_config.load arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2018-05-09 14:38:12 -04:00
|
|
|
o.on "--control-url URL", "The bind url to use for the control server. Use 'auto' to use temp unix server" do |arg|
|
|
|
|
configure_control_url(arg)
|
|
|
|
end
|
|
|
|
|
|
|
|
# alias --control-url for backwards-compatibility
|
|
|
|
o.on "--control URL", "DEPRECATED alias for --control-url" do |arg|
|
|
|
|
configure_control_url(arg)
|
2015-03-13 19:08:07 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--control-token TOKEN",
|
|
|
|
"The token to use as authentication for the control server" do |arg|
|
|
|
|
@control_options[:auth_token] = arg
|
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-d", "--daemon", "Daemonize the server into the background" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.daemonize
|
|
|
|
user_config.quiet
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--debug", "Log lowlevel debugging information" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.debug
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--dir DIR", "Change to DIR before starting" do |d|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.directory d
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-e", "--environment ENVIRONMENT",
|
|
|
|
"The environment to run the Rack app on (default development)" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.environment arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
|
|
|
|
$LOAD_PATH.unshift(*arg.split(':'))
|
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-p", "--port PORT", "Define the TCP port to bind to",
|
|
|
|
"Use -b for more advanced options" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.bind "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.pidfile arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--preload", "Preload the app. Cluster mode only" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.preload_app!
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--prune-bundler", "Prune out the bundler env if possible" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.prune_bundler
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2019-09-02 12:10:33 -04:00
|
|
|
o.on "--extra-runtime-dependencies GEM1,GEM2", "Defines any extra needed gems when using --prune-bundler" do |arg|
|
|
|
|
c.extra_runtime_dependencies arg.split(',')
|
|
|
|
end
|
|
|
|
|
2016-02-25 16:09:02 -05:00
|
|
|
o.on "-q", "--quiet", "Do not log requests internally (default true)" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.quiet
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-25 16:09:02 -05:00
|
|
|
o.on "-v", "--log-requests", "Log requests as they occur" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.log_requests
|
2016-02-25 16:09:02 -05:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-R", "--restart-cmd CMD",
|
|
|
|
"The puma command to run during a hot restart",
|
|
|
|
"Default: inferred" do |cmd|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.restart_command cmd
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-S", "--state PATH", "Where to store the state details" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.state_path arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
|
|
|
|
min, max = arg.split(":")
|
|
|
|
if max
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.threads min, max
|
2016-02-06 22:00:29 -05:00
|
|
|
else
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.threads min, min
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--tcp-mode", "Run the app in raw TCP mode instead of HTTP mode" do
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.tcp_mode!
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2017-10-04 08:30:16 -04:00
|
|
|
o.on "--early-hints", "Enable early hints support" do
|
|
|
|
user_config.early_hints
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-V", "--version", "Print the version information" do
|
|
|
|
puts "puma version #{Puma::Const::VERSION}"
|
|
|
|
exit 0
|
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "-w", "--workers COUNT",
|
|
|
|
"Activate cluster mode: How many worker processes to create" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.workers arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--tag NAME", "Additional text to display in process listing" do |arg|
|
2017-03-03 16:11:59 -05:00
|
|
|
user_config.tag arg
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--redirect-stdout FILE", "Redirect STDOUT to a specific file" do |arg|
|
|
|
|
@stdout = arg.to_s
|
|
|
|
end
|
2015-10-01 12:23:55 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--redirect-stderr FILE", "Redirect STDERR to a specific file" do |arg|
|
|
|
|
@stderr = arg.to_s
|
|
|
|
end
|
2015-10-01 12:23:55 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on "--[no-]redirect-append", "Append to redirected files" do |val|
|
|
|
|
@append = val
|
|
|
|
end
|
2015-10-01 12:23:55 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.banner = "puma <options> <rackup file>"
|
2015-03-13 19:08:07 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
o.on_tail "-h", "--help", "Show help" do
|
2016-02-07 02:10:48 -05:00
|
|
|
$stdout.puts o
|
2016-02-06 22:00:29 -05:00
|
|
|
exit 0
|
|
|
|
end
|
2015-03-13 19:08:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-09-27 12:23:03 -04:00
|
|
|
end
|
|
|
|
end
|