Class: Puma::Runner

Inherits:
Object
  • Object
show all
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, Single

Instance Method Summary collapse

Constructor Details

#initialize(cli, events) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
18
19
# File 'lib/puma/runner.rb', line 12

def initialize(cli, events)
  @launcher = cli
  @events = events
  @options = cli.options
  @app = nil
  @control = nil
  @started_at = Time.now
end

Instance Method Details

#appObject



164
165
166
# File 'lib/puma/runner.rb', line 164

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

#before_restartObject



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

def before_restart
  @control.stop(true) if @control
end

#daemon?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/puma/runner.rb', line 21

def daemon?
  @options[:daemon]
end

#debug(str) ⇒ Object



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

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

#development?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/puma/runner.rb', line 25

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

#error(str) ⇒ Object



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

def error(str)
  @events.error str
end

#load_and_bindObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/puma/runner.rb', line 147

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

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

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

#log(str) ⇒ Object



33
34
35
# File 'lib/puma/runner.rb', line 33

def log(str)
  @events.log str
end

#output_header(mode) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puma/runner.rb', line 103

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

  log "Puma starting in #{mode} mode..."
  log "* Version #{Puma::Const::PUMA_VERSION} (#{ruby_engine}), codename: #{Puma::Const::CODE_NAME}"
  log "* Min threads: #{min_t}, max threads: #{max_t}"
  log "* Environment: #{ENV['RACK_ENV']}"

  if @options[:mode] == :tcp
    log "* Mode: Lopez Express (tcp)"
  end
end

#redirect_ioObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puma/runner.rb', line 121

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

  if stdout
    unless Dir.exist?(File.dirname(stdout))
      raise "Cannot redirect STDOUT to #{stdout}"
    end

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

  if stderr
    unless Dir.exist?(File.dirname(stderr))
      raise "Cannot redirect STDERR to #{stderr}"
    end

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

#redirected_io?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/puma/runner.rb', line 117

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

#ruby_engineObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puma/runner.rb', line 91

def ruby_engine
  if !defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby"
    "ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  else
    if defined?(RUBY_ENGINE_VERSION)
      "#{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} - ruby #{RUBY_VERSION}"
    else
      "#{RUBY_ENGINE} #{RUBY_VERSION}"
    end
  end
end

#start_controlObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
89
# File 'lib/puma/runner.rb', line 49

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

  require 'puma/app/status'

  uri = URI.parse str

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

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

  control = Puma::Server.new app, @launcher.events
  control.min_threads = 0
  control.max_threads = 1

  case uri.scheme
  when "ssl"
      log "* Starting control server on #{str}"
      params = Util.parse_query uri.query
      ctx = MiniSSL::ContextBuilder.new(params, @events).context

      control.add_ssl_listener uri.host, uri.port, ctx
  when "tcp"
    log "* Starting control server on #{str}"
    control.add_tcp_listener uri.host, uri.port
  when "unix"
    log "* Starting control server on #{str}"
    path = "#{uri.host}#{uri.path}"
    mask = @options[:control_url_umask]

    control.add_unix_listener path, mask
  else
    error "Invalid control URI: #{str}"
  end

  control.run
  @control = control
end

#start_serverObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/puma/runner.rb', line 168

def start_server
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  server = Puma::Server.new app, @launcher.events, @options
  server.min_threads = min_t
  server.max_threads = max_t
  server.inherit_binder @launcher.binder

  if @options[:mode] == :tcp
    server.tcp_mode!
  end

  if @options[:early_hints]
    server.early_hints = true
  end

  unless development? || test?
    server.leak_stack_on_error = false
  end

  server
end

#test?Boolean

Returns:

  • (Boolean)


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

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