Class: Puma::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/launcher.rb

Overview

Puma::Launcher is the single entry point for starting a Puma server based on user 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.

Constant Summary collapse

KEYS_NOT_TO_PERSIST_IN_STATE =
Deprecated.

6.0.0

[
  :logger, :lowlevel_error_handler,
  :before_worker_shutdown, :before_worker_boot, :before_worker_fork,
  :after_worker_boot, :before_fork, :on_restart
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf, launcher_args = {}) ⇒ Launcher

Returns an instance of Launcher

conf A Puma::Configuration object indicating how to run the server.

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.

Examples:

conf = Puma::Configuration.new do |user_config|
  user_config.threads 1, 10
  user_config.app do |env|
    [200, {}, ["hello world"]]
  end
end
Puma::Launcher.new(conf, events: Puma::Events.stdio).run


44
45
46
47
48
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
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/puma/launcher.rb', line 44

def initialize(conf, launcher_args={})
  @runner        = nil
  @events        = launcher_args[:events] || Events::DEFAULT
  @argv          = launcher_args[:argv] || []
  @original_argv = @argv.dup
  @config        = conf

  @binder        = Binder.new(@events, conf)
  @binder.create_inherited_fds(ENV).each { |k| ENV.delete k }
  @binder.create_activated_fds(ENV).each { |k| ENV.delete k }

  @environment = conf.environment

  # Advertise the Configuration
  Puma.cli_config = @config if defined?(Puma.cli_config)

  @config.load

  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

  @options = @config.options
  @config.clamp

  @events.formatter = Events::PidFormatter.new if clustered?
  @events.formatter = options[:log_formatter] if @options[:log_formatter]

  generate_restart_data

  if clustered? && !Puma.forkable?
    unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform"
  end

  Dir.chdir(@restart_dir)

  prune_bundler if prune_bundler?

  @environment = @options[:environment] if @options[:environment]
  set_rack_environment

  if clustered?
    @options[:logger] = @events

    @runner = Cluster.new(self, @events)
  else
    @runner = Single.new(self, @events)
  end
  Puma.stats_object = @runner

  @status = :run

  log_config if ENV['PUMA_LOG_CONFIG']
end

Instance Attribute Details

#binderObject (readonly)

Returns the value of attribute binder.



102
103
104
# File 'lib/puma/launcher.rb', line 102

def binder
  @binder
end

#configObject (readonly)

Returns the value of attribute config.



102
103
104
# File 'lib/puma/launcher.rb', line 102

def config
  @config
end

#connected_portsObject (readonly)

Return all tcp ports the launcher may be using, TCP or SSL

Version:

  • 5.0.0



215
216
217
# File 'lib/puma/launcher.rb', line 215

def connected_ports
  @binder.connected_ports
end

#eventsObject (readonly)

Returns the value of attribute events.



102
103
104
# File 'lib/puma/launcher.rb', line 102

def events
  @events
end

#optionsObject (readonly)

Returns the value of attribute options.



102
103
104
# File 'lib/puma/launcher.rb', line 102

def options
  @options
end

#restart_argsObject (readonly)



220
221
222
223
224
225
226
227
# File 'lib/puma/launcher.rb', line 220

def restart_args
  cmd = @options[:restart_cmd]
  if cmd
    cmd.split(' ') + @original_argv
  else
    @restart_argv
  end
end

#restart_dirObject (readonly)

Returns the value of attribute restart_dir.



102
103
104
# File 'lib/puma/launcher.rb', line 102

def restart_dir
  @restart_dir
end

#thread_statusObject (readonly)

Version:

  • 5.0.0



240
241
242
243
244
245
246
247
248
249
# File 'lib/puma/launcher.rb', line 240

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
  end
end

Instance Method Details

#close_binder_listenersObject



229
230
231
232
233
234
235
236
# File 'lib/puma/launcher.rb', line 229

def close_binder_listeners
  @runner.close_control_listeners
  @binder.close_listeners
  unless @status == :restart
    log "=== puma shutdown: #{Time.now} ==="
    log "- Goodbye!"
  end
end

#delete_pidfileObject

Delete the configured pidfile



130
131
132
133
# File 'lib/puma/launcher.rb', line 130

def delete_pidfile
  path = @options[:pidfile]
  File.unlink(path) if path && File.exist?(path)
end

#haltObject

Begin async shutdown of the server



136
137
138
139
# File 'lib/puma/launcher.rb', line 136

def halt
  @status = :halt
  @runner.halt
end

#phased_restartObject

Begin a phased restart if supported



154
155
156
157
158
159
160
# File 'lib/puma/launcher.rb', line 154

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

#reforkObject

Begin a refork if supported



163
164
165
166
167
168
169
170
171
# File 'lib/puma/launcher.rb', line 163

def refork
  if clustered? && @runner.respond_to?(:fork_worker!) && @options[:fork_worker]
    @runner.fork_worker!
    true
  else
    log "* refork called but not available."
    false
  end
end

#restartObject

Begin async restart of the server



148
149
150
151
# File 'lib/puma/launcher.rb', line 148

def restart
  @status = :restart
  @runner.restart
end

#runObject

Run the server. This blocks until the server is stopped



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puma/launcher.rb', line 174

def run
  previous_env =
    if defined?(Bundler)
      env = Bundler::ORIGINAL_ENV.dup
      # add -rbundler/setup so we load from Gemfile when restarting
      bundle = "-rbundler/setup"
      env["RUBYOPT"] = [env["RUBYOPT"], bundle].join(" ").lstrip unless env["RUBYOPT"].to_s.include?(bundle)
      env
    else
      ENV.to_h
    end

  @config.clamp

  @config.plugins.fire_starts self

  setup_signals
  set_process_title
  integrate_with_systemd
  @runner.run

  case @status
  when :halt
    log "* Stopping immediately!"
    @runner.stop_control
  when :run, :stop
    graceful_stop
  when :restart
    log "* Restarting..."
    ENV.replace(previous_env)
    @runner.stop_control
    restart!
  when :exit
    # nothing
  end
  close_binder_listeners unless @status == :restart
end

#statsObject

Return stats about the server



105
106
107
# File 'lib/puma/launcher.rb', line 105

def stats
  @runner.stats
end

#stopObject

Begin async shutdown of the server gracefully



142
143
144
145
# File 'lib/puma/launcher.rb', line 142

def stop
  @status = :stop
  @runner.stop
end

#write_stateObject

Write a state file that can be used by pumactl to control the server



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/puma/launcher.rb', line 111

def write_state
  write_pid

  path = @options[:state]
  permission = @options[:state_permission]
  return unless path

  require 'puma/state_file'

  sf = StateFile.new
  sf.pid = Process.pid
  sf.control_url = @options[:control_url]
  sf.control_auth_token = @options[:control_auth_token]
  sf.running_from = File.expand_path('.')

  sf.save path, permission
end