Class: Puma::Runner

Inherits:
Object
  • Object
show all
Includes:
Const::PipeRequest
Defined in:
lib/puma/runner.rb

Overview

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`.

Direct Known Subclasses

Cluster, Cluster::Worker, Single

Constant Summary

Constants included from Const::PipeRequest

Const::PipeRequest::PIPE_BOOT, Const::PipeRequest::PIPE_EXTERNAL_TERM, Const::PipeRequest::PIPE_FORK, Const::PipeRequest::PIPE_IDLE, Const::PipeRequest::PIPE_PING, Const::PipeRequest::PIPE_TERM, Const::PipeRequest::PIPE_WAKEUP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(launcher) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puma/runner.rb', line 14

def initialize(launcher)
  @launcher = launcher
  @log_writer = launcher.log_writer
  @events = launcher.events
  @config = launcher.config
  @options = launcher.options
  @app = nil
  @control = nil
  @started_at = Time.now
  @wakeup = nil
end

Instance Attribute Details

#appObject (readonly)



170
171
172
# File 'lib/puma/runner.rb', line 170

def app
  @app ||= @config.app
end

#optionsPuma::UserFileDefaultOptions (readonly)

Returns the hash of configuration options.



28
29
30
# File 'lib/puma/runner.rb', line 28

def options
  @options
end

Instance Method Details

#close_control_listenersObject

Version:

  • 5.0.0



91
92
93
# File 'lib/puma/runner.rb', line 91

def close_control_listeners
  @control.binder.close_listeners if @control
end

#debug(str) ⇒ Object



59
60
61
# File 'lib/puma/runner.rb', line 59

def debug(str)
  @log_writer.log "- #{str}" if @options[:debug]
end

#development?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/puma/runner.rb', line 38

def development?
  @options[:environment] == "development"
end

#error(str) ⇒ Object



55
56
57
# File 'lib/puma/runner.rb', line 55

def error(str)
  @log_writer.error str
end

#load_and_bindObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puma/runner.rb', line 153

def load_and_bind
  unless @config.app_configured?
    error "No application configured, nothing to run"
    exit 1
  end

  begin
    @app = @config.app
  rescue Exception => e
    log "! Unable to load application: #{e.class}: #{e.message}"
    raise e
  end

  @launcher.binder.parse @options[:binds]
end

#log(str) ⇒ Object



46
47
48
# File 'lib/puma/runner.rb', line 46

def log(str)
  @log_writer.log str
end

#output_header(mode) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/puma/runner.rb', line 95

def output_header(mode)
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]
  environment = @options[:environment]

  log "Puma starting in #{mode} mode..."
  log "* Puma version: #{Puma::Const::PUMA_VERSION} (\"#{Puma::Const::CODE_NAME}\")"
  log "* Ruby version: #{RUBY_DESCRIPTION}"
  log "*  Min threads: #{min_t}"
  log "*  Max threads: #{max_t}"
  log "*  Environment: #{environment}"

  if mode == "cluster"
    log "*   Master PID: #{Process.pid}"
  else
    log "*          PID: #{Process.pid}"
  end
end

#redirect_ioObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puma/runner.rb', line 126

def redirect_io
  stdout = @options[:redirect_stdout]
  stderr = @options[:redirect_stderr]
  append = @options[:redirect_append]

  if stdout
    ensure_output_directory_exists(stdout, 'STDOUT')

    STDOUT.reopen stdout, (append ? "a" : "w")
    STDOUT.puts "=== puma startup: #{Time.now} ==="
    STDOUT.flush unless STDOUT.sync
  end

  if stderr
    ensure_output_directory_exists(stderr, 'STDERR')

    STDERR.reopen stderr, (append ? "a" : "w")
    STDERR.puts "=== puma startup: #{Time.now} ==="
    STDERR.flush unless STDERR.sync
  end

  if @options[:mutate_stdout_and_stderr_to_sync_on_write]
    STDOUT.sync = true
    STDERR.sync = true
  end
end

#redirected_io?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/puma/runner.rb', line 122

def redirected_io?
  @options[:redirect_stdout] || @options[:redirect_stderr]
end

#start_controlObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puma/runner.rb', line 63

def start_control
  str = @options[:control_url]
  return unless str

  require_relative 'app/status'

  if token = @options[:control_auth_token]
    token = nil if token.empty? || token == 'none'
  end

  app = Puma::App::Status.new @launcher, token

  # A Reactor is not created and nio4r is not loaded when 'queue_requests: false'
  # Use `nil` for events, no hooks in control server
  control = Puma::Server.new app, nil,
    { min_threads: 0, max_threads: 1, queue_requests: false, log_writer: @log_writer }

  begin
    control.binder.parse [str], nil, 'Starting control server'
  rescue Errno::EADDRINUSE, Errno::EACCES => e
    raise e, "Error: Control server address '#{str}' is already in use. Original error: #{e.message}"
  end

  control.run thread_name: 'ctl'
  @control = control
end

#start_serverObject



174
175
176
177
178
# File 'lib/puma/runner.rb', line 174

def start_server
  server = Puma::Server.new(app, @events, @options)
  server.inherit_binder(@launcher.binder)
  server
end

#stop_controlObject

Version:

  • 5.0.0



51
52
53
# File 'lib/puma/runner.rb', line 51

def stop_control
  @control&.stop true
end

#test?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/puma/runner.rb', line 42

def test?
  @options[:environment] == "test"
end

#wakeup!Object



30
31
32
33
34
35
36
# File 'lib/puma/runner.rb', line 30

def wakeup!
  return unless @wakeup

  @wakeup.write PIPE_WAKEUP unless @wakeup.closed?

rescue SystemCallError, IOError
end

#warn_ruby_mn_threadsObject



114
115
116
117
118
119
120
# File 'lib/puma/runner.rb', line 114

def warn_ruby_mn_threads
  return if !ENV.key?('RUBY_MN_THREADS')

  log "! WARNING: Detected `RUBY_MN_THREADS=#{ENV['RUBY_MN_THREADS']}`"
  log "! This setting is known to cause performance regressions with Puma."
  log "! Consider disabling this environment variable: https://github.com/puma/puma/issues/3720"
end