2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/events'
|
2016-02-04 17:11:34 -05:00
|
|
|
require 'puma/detect'
|
|
|
|
require 'puma/cluster'
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/single'
|
|
|
|
require 'puma/const'
|
|
|
|
require 'puma/binder'
|
2016-02-03 15:12:17 -05:00
|
|
|
|
2016-01-14 10:41:55 -05:00
|
|
|
module Puma
|
2016-04-07 14:22:15 -04:00
|
|
|
# Puma::Launcher is the single entry point for starting a Puma server based on user
|
2016-02-03 18:21:25 -05:00
|
|
|
# configuration. It is responsible for taking user supplied arguments and resolving them
|
|
|
|
# with configuration in `config/puma.rb` or `config/puma/<env>.rb`.
|
|
|
|
#
|
|
|
|
# It is responsible for either launching a cluster of Puma workers or a single
|
|
|
|
# puma server.
|
2016-01-14 10:41:55 -05:00
|
|
|
class Launcher
|
2016-02-04 10:49:01 -05:00
|
|
|
KEYS_NOT_TO_PERSIST_IN_STATE = [
|
|
|
|
:logger, :lowlevel_error_handler,
|
|
|
|
:before_worker_shutdown, :before_worker_boot, :before_worker_fork,
|
|
|
|
:after_worker_boot, :before_fork, :on_restart
|
|
|
|
]
|
2016-02-03 18:21:25 -05:00
|
|
|
# Returns an instance of Launcher
|
|
|
|
#
|
2016-02-06 22:00:29 -05:00
|
|
|
# +conf+ A Puma::Configuration object indicating how to run the server.
|
2016-02-03 18:21:25 -05:00
|
|
|
#
|
2016-02-06 22:00:29 -05:00
|
|
|
# +launcher_args+ A Hash that currently has one required key `:events`,
|
|
|
|
# this is expected to hold an object similar to an `Puma::Events.stdio`,
|
|
|
|
# this object will be responsible for broadcasting Puma's internal state
|
|
|
|
# to a logging destination. An optional key `:argv` can be supplied,
|
|
|
|
# this should be an array of strings, these arguments are re-used when
|
|
|
|
# restarting the puma server.
|
2016-02-03 18:21:25 -05:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2017-03-03 16:11:59 -05:00
|
|
|
# conf = Puma::Configuration.new do |user_config|
|
|
|
|
# user_config.threads 1, 10
|
|
|
|
# user_config.app do |env|
|
2016-02-06 22:00:29 -05:00
|
|
|
# [200, {}, ["hello world"]]
|
|
|
|
# end
|
|
|
|
# end
|
2017-10-09 12:19:22 -04:00
|
|
|
# Puma::Launcher.new(conf, events: Puma::Events.stdio).run
|
2016-02-06 22:00:29 -05:00
|
|
|
def initialize(conf, launcher_args={})
|
2016-02-03 15:15:15 -05:00
|
|
|
@runner = nil
|
2016-02-06 22:00:29 -05:00
|
|
|
@events = launcher_args[:events] || Events::DEFAULT
|
2016-02-03 18:22:40 -05:00
|
|
|
@argv = launcher_args[:argv] || []
|
2016-02-03 15:15:15 -05:00
|
|
|
@original_argv = @argv.dup
|
2016-02-06 22:00:29 -05:00
|
|
|
@config = conf
|
2016-02-03 15:15:15 -05:00
|
|
|
|
2020-06-01 03:17:46 -04:00
|
|
|
@binder = Binder.new(@events, conf)
|
2020-03-11 14:50:44 -04:00
|
|
|
@binder.create_inherited_fds(ENV).each { |k| ENV.delete k }
|
|
|
|
@binder.create_activated_fds(ENV).each { |k| ENV.delete k }
|
2016-02-03 15:37:16 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@environment = conf.environment
|
2016-02-03 15:37:16 -05:00
|
|
|
|
|
|
|
# Advertise the Configuration
|
2016-02-04 17:11:34 -05:00
|
|
|
Puma.cli_config = @config if defined?(Puma.cli_config)
|
2016-02-03 15:37:16 -05:00
|
|
|
|
|
|
|
@config.load
|
|
|
|
|
2020-11-10 12:24:39 -05:00
|
|
|
if @config.options[:bind_to_activated_sockets]
|
|
|
|
@config.options[:binds] = @binder.synthesize_binds_from_activated_fs(
|
|
|
|
@config.options[:binds],
|
|
|
|
@config.options[:bind_to_activated_sockets] == 'only'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-02-03 15:37:16 -05:00
|
|
|
@options = @config.options
|
2017-03-03 15:41:42 -05:00
|
|
|
@config.clamp
|
2016-02-03 15:37:16 -05:00
|
|
|
|
2019-08-01 15:21:23 -04:00
|
|
|
@events.formatter = Events::PidFormatter.new if clustered?
|
|
|
|
@events.formatter = options[:log_formatter] if @options[:log_formatter]
|
|
|
|
|
2016-03-20 17:09:30 -04:00
|
|
|
generate_restart_data
|
|
|
|
|
2018-08-09 04:46:03 -04:00
|
|
|
if clustered? && !Process.respond_to?(:fork)
|
|
|
|
unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
|
2016-02-03 15:37:16 -05:00
|
|
|
end
|
|
|
|
|
2016-03-20 17:09:30 -04:00
|
|
|
Dir.chdir(@restart_dir)
|
2016-02-03 15:37:16 -05:00
|
|
|
|
|
|
|
prune_bundler if prune_bundler?
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@environment = @options[:environment] if @options[:environment]
|
2016-02-03 15:37:16 -05:00
|
|
|
set_rack_environment
|
|
|
|
|
|
|
|
if clustered?
|
|
|
|
@options[:logger] = @events
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@runner = Cluster.new(self, @events)
|
2016-02-03 15:37:16 -05:00
|
|
|
else
|
2016-02-06 22:00:29 -05:00
|
|
|
@runner = Single.new(self, @events)
|
2016-02-03 15:37:16 -05:00
|
|
|
end
|
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
|
|
|
Puma.stats_object = @runner
|
2016-02-03 15:37:16 -05:00
|
|
|
|
|
|
|
@status = :run
|
2020-11-02 10:32:37 -05:00
|
|
|
|
|
|
|
log_config if ENV['PUMA_LOG_CONFIG']
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2016-03-20 17:09:30 -04:00
|
|
|
attr_reader :binder, :events, :config, :options, :restart_dir
|
2016-02-03 15:12:17 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Return stats about the server
|
2016-01-14 10:41:55 -05:00
|
|
|
def stats
|
|
|
|
@runner.stats
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Write a state file that can be used by pumactl to control
|
|
|
|
# the server
|
2016-01-14 10:41:55 -05:00
|
|
|
def write_state
|
|
|
|
write_pid
|
|
|
|
|
|
|
|
path = @options[:state]
|
2020-05-06 23:13:35 -04:00
|
|
|
permission = @options[:state_permission]
|
2016-01-14 10:41:55 -05:00
|
|
|
return unless path
|
|
|
|
|
2016-09-08 11:46:24 -04:00
|
|
|
require 'puma/state_file'
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
sf = StateFile.new
|
|
|
|
sf.pid = Process.pid
|
|
|
|
sf.control_url = @options[:control_url]
|
|
|
|
sf.control_auth_token = @options[:control_auth_token]
|
2020-05-23 13:27:51 -04:00
|
|
|
sf.running_from = File.expand_path('.')
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2020-05-06 23:13:35 -04:00
|
|
|
sf.save path, permission
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Delete the configured pidfile
|
2016-01-14 10:41:55 -05:00
|
|
|
def delete_pidfile
|
|
|
|
path = @options[:pidfile]
|
|
|
|
File.unlink(path) if path && File.exist?(path)
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Begin async shutdown of the server
|
|
|
|
def halt
|
|
|
|
@status = :halt
|
|
|
|
@runner.halt
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Begin async shutdown of the server gracefully
|
2016-01-14 10:41:55 -05:00
|
|
|
def stop
|
|
|
|
@status = :stop
|
|
|
|
@runner.stop
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Begin async restart of the server
|
2016-01-14 10:41:55 -05:00
|
|
|
def restart
|
|
|
|
@status = :restart
|
|
|
|
@runner.restart
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Begin a phased restart if supported
|
|
|
|
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
|
|
|
|
|
|
|
|
# Run the server. This blocks until the server is stopped
|
2016-01-14 10:41:55 -05:00
|
|
|
def run
|
2017-06-09 08:20:03 -04:00
|
|
|
previous_env =
|
|
|
|
if defined?(Bundler)
|
2017-08-16 11:20:11 -04:00
|
|
|
env = Bundler::ORIGINAL_ENV.dup
|
2017-06-09 08:20:03 -04:00
|
|
|
# add -rbundler/setup so we load from Gemfile when restarting
|
|
|
|
bundle = "-rbundler/setup"
|
2017-11-16 16:26:31 -05:00
|
|
|
env["RUBYOPT"] = [env["RUBYOPT"], bundle].join(" ").lstrip unless env["RUBYOPT"].to_s.include?(bundle)
|
2017-06-09 08:20:03 -04:00
|
|
|
env
|
|
|
|
else
|
|
|
|
ENV.to_h
|
|
|
|
end
|
2017-04-01 14:12:54 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@config.clamp
|
|
|
|
|
2016-02-07 01:28:02 -05:00
|
|
|
@config.plugins.fire_starts self
|
|
|
|
|
2016-01-14 10:41:55 -05:00
|
|
|
setup_signals
|
|
|
|
set_process_title
|
2020-10-26 18:02:31 -04:00
|
|
|
integrate_with_systemd
|
2016-01-14 10:41:55 -05:00
|
|
|
@runner.run
|
|
|
|
|
|
|
|
case @status
|
|
|
|
when :halt
|
|
|
|
log "* Stopping immediately!"
|
2020-05-19 14:32:02 -04:00
|
|
|
@runner.stop_control
|
2016-01-14 10:41:55 -05:00
|
|
|
when :run, :stop
|
|
|
|
graceful_stop
|
|
|
|
when :restart
|
|
|
|
log "* Restarting..."
|
2017-05-03 13:18:54 -04:00
|
|
|
ENV.replace(previous_env)
|
2020-05-19 14:32:02 -04:00
|
|
|
@runner.stop_control
|
2016-01-14 10:41:55 -05:00
|
|
|
restart!
|
|
|
|
when :exit
|
|
|
|
# nothing
|
|
|
|
end
|
2020-03-03 15:09:08 -05:00
|
|
|
close_binder_listeners unless @status == :restart
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2019-11-19 13:36:15 -05:00
|
|
|
# Return all tcp ports the launcher may be using, TCP or SSL
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] connected_ports
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2019-11-19 13:36:15 -05:00
|
|
|
def connected_ports
|
|
|
|
@binder.connected_ports
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] restart_args
|
2016-03-05 19:28:39 -05:00
|
|
|
def restart_args
|
|
|
|
cmd = @options[:restart_cmd]
|
|
|
|
if cmd
|
|
|
|
cmd.split(' ') + @original_argv
|
|
|
|
else
|
|
|
|
@restart_argv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-07 15:10:20 -04:00
|
|
|
def close_binder_listeners
|
2020-03-03 15:09:08 -05:00
|
|
|
@runner.close_control_listeners
|
2019-09-11 06:49:35 -04:00
|
|
|
@binder.close_listeners
|
2021-01-24 16:31:41 -05:00
|
|
|
unless @status == :restart
|
|
|
|
log "=== puma shutdown: #{Time.now} ==="
|
|
|
|
log "- Goodbye!"
|
|
|
|
end
|
2019-06-07 15:10:20 -04:00
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] thread_status
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2019-11-11 00:08:41 -05:00
|
|
|
def thread_status
|
|
|
|
Thread.list.each do |thread|
|
|
|
|
name = "Thread: TID-#{thread.object_id.to_s(36)}"
|
|
|
|
name += " #{thread['label']}" if thread['label']
|
|
|
|
name += " #{thread.name}" if thread.respond_to?(:name) && thread.name
|
|
|
|
backtrace = thread.backtrace || ["<no backtrace available>"]
|
|
|
|
|
|
|
|
yield name, backtrace
|
2019-06-07 15:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
private
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2019-09-20 05:51:57 -04:00
|
|
|
# If configured, write the pid of the current process out
|
|
|
|
# to a file.
|
|
|
|
def write_pid
|
|
|
|
path = @options[:pidfile]
|
|
|
|
return unless path
|
2020-11-27 10:47:33 -05:00
|
|
|
cur_pid = Process.pid
|
|
|
|
File.write path, cur_pid, mode: 'wb:UTF-8'
|
2019-09-20 05:51:57 -04:00
|
|
|
at_exit do
|
2020-11-27 10:47:33 -05:00
|
|
|
delete_pidfile if cur_pid == Process.pid
|
2019-09-20 05:51:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def reload_worker_directory
|
|
|
|
@runner.reload_worker_directory if @runner.respond_to?(:reload_worker_directory)
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def restart!
|
2020-10-26 18:02:31 -04:00
|
|
|
@events.fire_on_restart!
|
2020-03-10 13:41:17 -04:00
|
|
|
@config.run_hooks :on_restart, self, @events
|
2016-02-06 22:00:29 -05:00
|
|
|
|
|
|
|
if Puma.jruby?
|
|
|
|
close_binder_listeners
|
|
|
|
|
|
|
|
require 'puma/jruby_restart'
|
|
|
|
JRubyRestart.chdir_exec(@restart_dir, restart_args)
|
|
|
|
elsif Puma.windows?
|
|
|
|
close_binder_listeners
|
|
|
|
|
|
|
|
argv = restart_args
|
|
|
|
Dir.chdir(@restart_dir)
|
|
|
|
Kernel.exec(*argv)
|
|
|
|
else
|
|
|
|
argv = restart_args
|
|
|
|
Dir.chdir(@restart_dir)
|
2020-03-08 12:06:42 -04:00
|
|
|
ENV.update(@binder.redirects_for_restart_env)
|
2019-09-11 06:49:35 -04:00
|
|
|
argv += [@binder.redirects_for_restart]
|
2016-02-06 22:00:29 -05:00
|
|
|
Kernel.exec(*argv)
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2020-10-20 09:31:23 -04:00
|
|
|
# @!attribute [r] files_to_require_after_prune
|
|
|
|
def files_to_require_after_prune
|
2019-09-02 12:10:33 -04:00
|
|
|
puma = spec_for_gem("puma")
|
|
|
|
|
2020-10-20 09:31:23 -04:00
|
|
|
require_paths_for_gem(puma) + extra_runtime_deps_directories
|
2019-09-02 12:10:33 -04:00
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] extra_runtime_deps_directories
|
2019-09-02 12:10:33 -04:00
|
|
|
def extra_runtime_deps_directories
|
|
|
|
Array(@options[:extra_runtime_dependencies]).map do |d_name|
|
|
|
|
if (spec = spec_for_gem(d_name))
|
|
|
|
require_paths_for_gem(spec)
|
|
|
|
else
|
|
|
|
log "* Could not load extra dependency: #{d_name}"
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end.flatten.compact
|
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] puma_wild_location
|
2019-09-02 12:10:33 -04:00
|
|
|
def puma_wild_location
|
|
|
|
puma = spec_for_gem("puma")
|
|
|
|
dirs = require_paths_for_gem(puma)
|
2016-01-14 10:41:55 -05:00
|
|
|
puma_lib_dir = dirs.detect { |x| File.exist? File.join(x, '../bin/puma-wild') }
|
2019-09-02 12:10:33 -04:00
|
|
|
File.expand_path(File.join(puma_lib_dir, "../bin/puma-wild"))
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2019-09-02 12:10:33 -04:00
|
|
|
def prune_bundler
|
2020-07-27 08:02:11 -04:00
|
|
|
return if ENV['PUMA_BUNDLER_PRUNED']
|
2019-09-02 12:10:33 -04:00
|
|
|
return unless defined?(Bundler)
|
|
|
|
require_rubygems_min_version!(Gem::Version.new("2.2"), "prune_bundler")
|
|
|
|
unless puma_wild_location
|
2016-01-14 10:41:55 -05:00
|
|
|
log "! Unable to prune Bundler environment, continuing"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-10-20 09:31:23 -04:00
|
|
|
dirs = files_to_require_after_prune
|
2016-01-14 10:41:55 -05:00
|
|
|
|
2020-02-13 17:55:35 -05:00
|
|
|
log '* Pruning Bundler environment'
|
2016-01-14 10:41:55 -05:00
|
|
|
home = ENV['GEM_HOME']
|
2020-03-10 13:46:20 -04:00
|
|
|
bundle_gemfile = Bundler.original_env['BUNDLE_GEMFILE']
|
2021-09-03 13:52:35 -04:00
|
|
|
bundle_app_config = Bundler.original_env['BUNDLE_APP_CONFIG']
|
2020-03-06 20:30:26 -05:00
|
|
|
with_unbundled_env do
|
2016-01-14 10:41:55 -05:00
|
|
|
ENV['GEM_HOME'] = home
|
2020-02-13 17:47:12 -05:00
|
|
|
ENV['BUNDLE_GEMFILE'] = bundle_gemfile
|
2016-01-14 10:41:55 -05:00
|
|
|
ENV['PUMA_BUNDLER_PRUNED'] = '1'
|
2021-09-03 13:52:35 -04:00
|
|
|
ENV["BUNDLE_APP_CONFIG"] = bundle_app_config
|
2020-10-20 09:31:23 -04:00
|
|
|
args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':')] + @original_argv
|
2016-12-13 15:51:41 -05:00
|
|
|
# Ruby 2.0+ defaults to true which breaks socket activation
|
2019-06-27 18:32:01 -04:00
|
|
|
args += [{:close_others => false}]
|
2016-01-14 10:41:55 -05:00
|
|
|
Kernel.exec(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-26 18:02:31 -04:00
|
|
|
#
|
|
|
|
# Puma's systemd integration allows Puma to inform systemd:
|
|
|
|
# 1. when it has successfully started
|
|
|
|
# 2. when it is starting shutdown
|
|
|
|
# 3. periodically for a liveness check with a watchdog thread
|
|
|
|
#
|
|
|
|
|
|
|
|
def integrate_with_systemd
|
|
|
|
return unless ENV["NOTIFY_SOCKET"]
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'puma/systemd'
|
|
|
|
rescue LoadError
|
|
|
|
log "Systemd integration failed. It looks like you're trying to use systemd notify but don't have sd_notify gem installed"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
log "* Enabling systemd notification integration"
|
|
|
|
|
|
|
|
systemd = Systemd.new(@events)
|
|
|
|
systemd.hook_events
|
|
|
|
systemd.start_watchdog
|
|
|
|
end
|
|
|
|
|
2019-09-02 12:10:33 -04:00
|
|
|
def spec_for_gem(gem_name)
|
|
|
|
Bundler.rubygems.loaded_specs(gem_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_paths_for_gem(gem_spec)
|
|
|
|
gem_spec.full_require_paths
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def log(str)
|
|
|
|
@events.log str
|
2016-02-03 15:06:00 -05:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def clustered?
|
|
|
|
(@options[:workers] || 0) > 0
|
2016-02-03 15:06:00 -05:00
|
|
|
end
|
|
|
|
|
2016-02-03 15:37:16 -05:00
|
|
|
def unsupported(str)
|
|
|
|
@events.error(str)
|
|
|
|
raise UnsupportedOption
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def graceful_stop
|
2020-10-26 18:02:31 -04:00
|
|
|
@events.fire_on_stopped!
|
2016-01-14 10:41:55 -05:00
|
|
|
@runner.stop_blocked
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_process_title
|
|
|
|
Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title
|
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] title
|
2016-01-14 10:41:55 -05:00
|
|
|
def title
|
2017-08-02 21:02:40 -04:00
|
|
|
buffer = "puma #{Puma::Const::VERSION} (#{@options[:binds].join(',')})"
|
|
|
|
buffer += " [#{@options[:tag]}]" if @options[:tag] && !@options[:tag].empty?
|
2016-01-14 10:41:55 -05:00
|
|
|
buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_rack_environment
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:environment] = environment
|
|
|
|
ENV['RACK_ENV'] = environment
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
2020-09-25 14:50:57 -04:00
|
|
|
# @!attribute [r] environment
|
2016-02-06 22:00:29 -05:00
|
|
|
def environment
|
|
|
|
@environment
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def prune_bundler?
|
|
|
|
@options[:prune_bundler] && clustered? && !@options[:preload_app]
|
|
|
|
end
|
|
|
|
|
2016-02-03 15:06:00 -05:00
|
|
|
def generate_restart_data
|
2016-03-20 17:09:30 -04:00
|
|
|
if dir = @options[:directory]
|
|
|
|
@restart_dir = dir
|
|
|
|
|
2016-07-24 00:36:05 -04:00
|
|
|
elsif Puma.windows?
|
|
|
|
# I guess the value of PWD is garbage on windows so don't bother
|
|
|
|
# using it.
|
|
|
|
@restart_dir = Dir.pwd
|
|
|
|
|
2016-03-20 17:09:30 -04:00
|
|
|
# Use the same trick as unicorn, namely favor PWD because
|
|
|
|
# it will contain an unresolved symlink, useful for when
|
|
|
|
# the pwd is /data/releases/current.
|
|
|
|
elsif dir = ENV['PWD']
|
2016-02-03 15:06:00 -05:00
|
|
|
s_env = File.stat(dir)
|
|
|
|
s_pwd = File.stat(Dir.pwd)
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if s_env.ino == s_pwd.ino and (Puma.jruby? or s_env.dev == s_pwd.dev)
|
2016-02-03 15:06:00 -05:00
|
|
|
@restart_dir = dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@restart_dir ||= Dir.pwd
|
|
|
|
|
|
|
|
# if $0 is a file in the current directory, then restart
|
|
|
|
# it the same, otherwise add -S on there because it was
|
|
|
|
# picked up in PATH.
|
|
|
|
#
|
|
|
|
if File.exist?($0)
|
|
|
|
arg0 = [Gem.ruby, $0]
|
|
|
|
else
|
|
|
|
arg0 = [Gem.ruby, "-S", $0]
|
|
|
|
end
|
|
|
|
|
2017-06-02 16:24:01 -04:00
|
|
|
# Detect and reinject -Ilib from the command line, used for testing without bundler
|
|
|
|
# cruby has an expanded path, jruby has just "lib"
|
2016-02-03 15:06:00 -05:00
|
|
|
lib = File.expand_path "lib"
|
2017-06-02 16:24:01 -04:00
|
|
|
arg0[1,0] = ["-I", lib] if [lib, "lib"].include?($LOAD_PATH[0])
|
2016-02-03 15:06:00 -05:00
|
|
|
|
|
|
|
if defined? Puma::WILD_ARGS
|
|
|
|
@restart_argv = arg0 + Puma::WILD_ARGS + @original_argv
|
|
|
|
else
|
|
|
|
@restart_argv = arg0 + @original_argv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 10:41:55 -05:00
|
|
|
def setup_signals
|
|
|
|
begin
|
|
|
|
Signal.trap "SIGUSR2" do
|
|
|
|
restart
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
log "*** SIGUSR2 not implemented, signal based restart unavailable!"
|
|
|
|
end
|
|
|
|
|
2016-03-06 00:44:37 -05:00
|
|
|
unless Puma.jruby?
|
|
|
|
begin
|
|
|
|
Signal.trap "SIGUSR1" do
|
|
|
|
phased_restart
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
log "*** SIGUSR1 not implemented, signal based restart unavailable!"
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Signal.trap "SIGTERM" do
|
2017-08-16 11:23:51 -04:00
|
|
|
graceful_stop
|
|
|
|
|
2019-02-21 14:12:24 -05:00
|
|
|
raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!"
|
|
|
|
end
|
|
|
|
|
2017-08-16 11:22:38 -04:00
|
|
|
begin
|
|
|
|
Signal.trap "SIGINT" do
|
|
|
|
stop
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
log "*** SIGINT not implemented, signal based gracefully stopping unavailable!"
|
|
|
|
end
|
|
|
|
|
2016-01-14 10:41:55 -05:00
|
|
|
begin
|
|
|
|
Signal.trap "SIGHUP" do
|
2016-07-24 18:24:42 -04:00
|
|
|
if @runner.redirected_io?
|
|
|
|
@runner.redirect_io
|
|
|
|
else
|
|
|
|
stop
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
log "*** SIGHUP not implemented, signal based logs reopening unavailable!"
|
|
|
|
end
|
2019-09-12 05:59:54 -04:00
|
|
|
|
|
|
|
begin
|
2020-03-14 15:29:05 -04:00
|
|
|
unless Puma.jruby? # INFO in use by JVM already
|
|
|
|
Signal.trap "SIGINFO" do
|
2020-04-01 20:28:44 -04:00
|
|
|
thread_status do |name, backtrace|
|
|
|
|
@events.log name
|
|
|
|
@events.log backtrace.map { |bt| " #{bt}" }
|
|
|
|
end
|
2019-11-11 00:08:41 -05:00
|
|
|
end
|
2019-09-12 05:59:54 -04:00
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
# Not going to log this one, as SIGINFO is *BSD only and would be pretty annoying
|
|
|
|
# to see this constantly on Linux.
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
2019-09-02 12:10:33 -04:00
|
|
|
|
|
|
|
def require_rubygems_min_version!(min_version, feature)
|
|
|
|
return if min_version <= Gem::Version.new(Gem::VERSION)
|
|
|
|
|
|
|
|
raise "#{feature} is not supported on your version of RubyGems. " \
|
|
|
|
"You must have RubyGems #{min_version}+ to use this feature."
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
2020-03-06 20:30:26 -05:00
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2020-03-06 20:30:26 -05:00
|
|
|
def with_unbundled_env
|
|
|
|
bundler_ver = Gem::Version.new(Bundler::VERSION)
|
|
|
|
if bundler_ver < Gem::Version.new('2.1.0')
|
|
|
|
Bundler.with_clean_env { yield }
|
|
|
|
else
|
|
|
|
Bundler.with_unbundled_env { yield }
|
|
|
|
end
|
|
|
|
end
|
2020-11-02 10:32:37 -05:00
|
|
|
|
|
|
|
def log_config
|
|
|
|
log "Configuration:"
|
|
|
|
|
|
|
|
@config.final_options
|
|
|
|
.each { |config_key, value| log "- #{config_key}: #{value}" }
|
|
|
|
|
|
|
|
log "\n"
|
|
|
|
end
|
2016-01-14 10:41:55 -05:00
|
|
|
end
|
2016-02-06 22:00:29 -05:00
|
|
|
end
|