2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/server'
|
|
|
|
require 'puma/const'
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
module Puma
|
2018-05-01 16:44:08 -04:00
|
|
|
# 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`.
|
2013-07-05 19:08:13 -04:00
|
|
|
class Runner
|
2016-02-06 22:00:29 -05:00
|
|
|
def initialize(cli, events)
|
|
|
|
@launcher = cli
|
|
|
|
@events = events
|
2013-07-05 19:08:13 -04:00
|
|
|
@options = cli.options
|
|
|
|
@app = nil
|
2013-07-09 01:36:43 -04:00
|
|
|
@control = nil
|
2019-08-01 15:25:15 -04:00
|
|
|
@started_at = Time.now
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def development?
|
|
|
|
@options[:environment] == "development"
|
|
|
|
end
|
|
|
|
|
2018-05-09 13:45:41 -04:00
|
|
|
def test?
|
|
|
|
@options[:environment] == "test"
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
def log(str)
|
2016-02-06 22:00:29 -05:00
|
|
|
@events.log str
|
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2020-05-19 14:32:02 -04:00
|
|
|
def stop_control
|
2016-02-06 22:00:29 -05:00
|
|
|
@control.stop(true) if @control
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error(str)
|
2016-02-06 22:00:29 -05:00
|
|
|
@events.error str
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
def debug(str)
|
|
|
|
@events.log "- #{str}" if @options[:debug]
|
2013-07-09 01:36:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_control
|
|
|
|
str = @options[:control_url]
|
|
|
|
return unless str
|
|
|
|
|
|
|
|
require 'puma/app/status'
|
|
|
|
|
|
|
|
if token = @options[:control_auth_token]
|
2019-09-20 06:45:56 -04:00
|
|
|
token = nil if token.empty? || token == 'none'
|
2013-07-09 01:36:43 -04:00
|
|
|
end
|
|
|
|
|
2019-09-20 06:45:56 -04:00
|
|
|
app = Puma::App::Status.new @launcher, token
|
|
|
|
|
2020-09-29 09:12:47 -04:00
|
|
|
control = Puma::Server.new app, @launcher.events,
|
|
|
|
{ min_threads: 0, max_threads: 1 }
|
2013-07-09 01:36:43 -04:00
|
|
|
|
2020-02-20 07:40:17 -05:00
|
|
|
control.binder.parse [str], self, 'Starting control server'
|
2013-07-09 01:36:43 -04:00
|
|
|
|
2020-11-29 10:35:50 -05:00
|
|
|
control.run thread_name: 'control'
|
2013-07-09 01:36:43 -04:00
|
|
|
@control = control
|
|
|
|
end
|
|
|
|
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2020-03-03 15:09:08 -05:00
|
|
|
def close_control_listeners
|
|
|
|
@control.binder.close_listeners if @control
|
|
|
|
end
|
|
|
|
|
2020-09-29 09:12:47 -04:00
|
|
|
# @!attribute [r] ruby_engine
|
2014-01-25 17:26:59 -05:00
|
|
|
def ruby_engine
|
|
|
|
if !defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby"
|
|
|
|
"ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
|
|
|
|
else
|
2016-03-06 00:48:44 -05:00
|
|
|
if defined?(RUBY_ENGINE_VERSION)
|
|
|
|
"#{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} - ruby #{RUBY_VERSION}"
|
|
|
|
else
|
|
|
|
"#{RUBY_ENGINE} #{RUBY_VERSION}"
|
|
|
|
end
|
2014-01-25 17:26:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-05 20:09:18 -04:00
|
|
|
def output_header(mode)
|
2013-07-05 19:08:13 -04:00
|
|
|
min_t = @options[:min_threads]
|
|
|
|
max_t = @options[:max_threads]
|
|
|
|
|
2020-11-30 11:26:08 -05:00
|
|
|
log "Puma starting in #{mode} mode..."
|
|
|
|
log "* Puma version: #{Puma::Const::PUMA_VERSION} (#{ruby_engine}) (\"#{Puma::Const::CODE_NAME}\")"
|
|
|
|
log "* Min threads: #{min_t}"
|
|
|
|
log "* Max threads: #{max_t}"
|
|
|
|
log "* Environment: #{ENV['RACK_ENV']}"
|
|
|
|
|
|
|
|
if mode == "cluster"
|
|
|
|
log "* Master PID: #{Process.pid}"
|
|
|
|
else
|
|
|
|
log "* PID: #{Process.pid}"
|
|
|
|
end
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2016-07-24 18:24:42 -04:00
|
|
|
def redirected_io?
|
|
|
|
@options[:redirect_stdout] || @options[:redirect_stderr]
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
def redirect_io
|
|
|
|
stdout = @options[:redirect_stdout]
|
|
|
|
stderr = @options[:redirect_stderr]
|
|
|
|
append = @options[:redirect_append]
|
|
|
|
|
|
|
|
if stdout
|
2017-05-15 08:40:29 -04:00
|
|
|
unless Dir.exist?(File.dirname(stdout))
|
2016-12-14 11:45:10 -05:00
|
|
|
raise "Cannot redirect STDOUT to #{stdout}"
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
STDOUT.reopen stdout, (append ? "a" : "w")
|
|
|
|
STDOUT.sync = true
|
|
|
|
STDOUT.puts "=== puma startup: #{Time.now} ==="
|
|
|
|
end
|
|
|
|
|
|
|
|
if stderr
|
2017-05-15 08:40:29 -04:00
|
|
|
unless Dir.exist?(File.dirname(stderr))
|
2016-12-14 11:45:10 -05:00
|
|
|
raise "Cannot redirect STDERR to #{stderr}"
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
STDERR.reopen stderr, (append ? "a" : "w")
|
|
|
|
STDERR.sync = true
|
|
|
|
STDERR.puts "=== puma startup: #{Time.now} ==="
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_and_bind
|
2016-02-06 22:00:29 -05:00
|
|
|
unless @launcher.config.app_configured?
|
2013-07-05 19:08:13 -04:00
|
|
|
error "No application configured, nothing to run"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2016-02-06 22:00:29 -05:00
|
|
|
@app = @launcher.config.app
|
2013-07-05 19:08:13 -04:00
|
|
|
rescue Exception => e
|
2014-09-16 09:29:31 -04:00
|
|
|
log "! Unable to load application: #{e.class}: #{e.message}"
|
2013-07-05 19:08:13 -04:00
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@launcher.binder.parse @options[:binds], self
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2020-09-29 09:12:47 -04:00
|
|
|
# @!attribute [r] app
|
2013-07-05 19:08:13 -04:00
|
|
|
def app
|
2016-02-06 22:00:29 -05:00
|
|
|
@app ||= @launcher.config.app
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
2013-07-06 00:13:29 -04:00
|
|
|
|
|
|
|
def start_server
|
2016-02-06 22:00:29 -05:00
|
|
|
server = Puma::Server.new app, @launcher.events, @options
|
|
|
|
server.inherit_binder @launcher.binder
|
2013-07-06 00:13:29 -04:00
|
|
|
server
|
|
|
|
end
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
end
|