Class: Puma::Launcher
- Inherits:
-
Object
- Object
- Puma::Launcher
- Defined in:
- lib/puma/launcher.rb,
lib/puma/launcher/bundle_pruner.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.
Defined Under Namespace
Classes: BundlePruner
Instance Attribute Summary collapse
-
#binder ⇒ Object
readonly
Returns the value of attribute binder.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connected_ports ⇒ Object
readonly
Return all tcp ports the launcher may be using, TCP or SSL.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#log_writer ⇒ Object
readonly
Returns the value of attribute log_writer.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
- #restart_args ⇒ Object readonly
-
#restart_dir ⇒ Object
readonly
Returns the value of attribute restart_dir.
- #thread_status ⇒ Object readonly
Instance Method Summary collapse
- #close_binder_listeners ⇒ Object
-
#delete_pidfile ⇒ Object
Delete the configured pidfile.
-
#halt ⇒ Object
Begin async shutdown of the server.
-
#initialize(conf, launcher_args = {}) ⇒ Launcher
constructor
Returns an instance of Launcher.
-
#phased_restart ⇒ Object
Begin a phased restart if supported.
-
#refork ⇒ Object
Begin a refork if supported.
-
#restart ⇒ Object
Begin async restart of the server.
-
#run ⇒ Object
Run the server.
-
#stats ⇒ Object
Return stats about the server.
-
#stop ⇒ Object
Begin async shutdown of the server gracefully.
-
#write_state ⇒ Object
Write a state file that can be used by pumactl to control the server.
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 has a few optional keys.
:log_writer-
Expected to hold an object similar to
Puma::LogWriter.stdio.
This object will be responsible for broadcasting Puma’s internal state to a logging destination.
:events-
Expected to hold an object similar to
Puma::Events.
:argv-
Expected to be an array of strings.
:env-
Expected to hold a hash of environment variables.
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, log_writer: Puma::LogWriter.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 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/puma/launcher.rb', line 44 def initialize(conf, launcher_args={}) ## Minimal initialization before potential early restart (e.g. from bundle pruning) @config = conf # Advertise the CLI Configuration before config files are loaded Puma.cli_config = @config if defined?(Puma.cli_config) @config.clamp = @config. @log_writer = launcher_args[:log_writer] || LogWriter::DEFAULT @log_writer.formatter = LogWriter::PidFormatter.new if clustered? @log_writer.formatter = [:log_formatter] if [:log_formatter] @log_writer.custom_logger = [:custom_logger] if [:custom_logger] [:log_writer] = @log_writer [:logger] = @log_writer if clustered? @events = launcher_args[:events] || Events.new @argv = launcher_args[:argv] || [] @original_argv = @argv.dup ## End minimal initialization generate_restart_data Dir.chdir(@restart_dir) prune_bundler! env = launcher_args.delete(:env) || ENV # Log after prune_bundler! to avoid duplicate logging if a restart occurs log_config if env['PUMA_LOG_CONFIG'] @binder = Binder.new(@log_writer, ) @binder.create_inherited_fds(env).each { |k| env.delete k } @binder.create_activated_fds(env).each { |k| env.delete k } @environment = @config.environment # Load the systemd integration if we detect systemd's NOTIFY_SOCKET. # Skip this on JRuby though, because it is incompatible with the systemd # integration due to https://github.com/jruby/jruby/issues/6504 if ENV["NOTIFY_SOCKET"] && !Puma.jruby? && !ENV["PUMA_SKIP_SYSTEMD"] @config.plugins.create('systemd') end if [:bind_to_activated_sockets] [:binds] = @binder.synthesize_binds_from_activated_fs( [:binds], [:bind_to_activated_sockets] == 'only' ) end if clustered? && !Puma.forkable? unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform" end @environment = [:environment] if [:environment] set_rack_environment if clustered? @runner = Cluster.new(self) else @runner = Single.new(self) end Puma.stats_object = @runner @status = :run end |
Instance Attribute Details
#binder ⇒ Object (readonly)
Returns the value of attribute binder.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def binder @binder end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def config @config end |
#connected_ports ⇒ Object (readonly)
Return all tcp ports the launcher may be using, TCP or SSL
216 217 218 |
# File 'lib/puma/launcher.rb', line 216 def connected_ports @binder.connected_ports end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def events @events end |
#log_writer ⇒ Object (readonly)
Returns the value of attribute log_writer.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def log_writer @log_writer end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def end |
#restart_args ⇒ Object (readonly)
221 222 223 224 225 226 227 228 |
# File 'lib/puma/launcher.rb', line 221 def restart_args cmd = [:restart_cmd] if cmd cmd.split(' ') + @original_argv else @restart_argv end end |
#restart_dir ⇒ Object (readonly)
Returns the value of attribute restart_dir.
115 116 117 |
# File 'lib/puma/launcher.rb', line 115 def restart_dir @restart_dir end |
#thread_status ⇒ Object (readonly)
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/puma/launcher.rb', line 241 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_listeners ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/puma/launcher.rb', line 230 def close_binder_listeners @runner.close_control_listeners @binder.close_listeners unless @status == :restart log "=== puma shutdown: #{Time.now} ===" log "- Goodbye!" end end |
#delete_pidfile ⇒ Object
Delete the configured pidfile
143 144 145 146 147 148 149 |
# File 'lib/puma/launcher.rb', line 143 def delete_pidfile path = [:pidfile] begin File.unlink(path) if path rescue Errno::ENOENT end end |
#halt ⇒ Object
Begin async shutdown of the server
152 153 154 155 |
# File 'lib/puma/launcher.rb', line 152 def halt @status = :halt @runner.halt end |
#phased_restart ⇒ Object
Begin a phased restart if supported
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/puma/launcher.rb', line 170 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 if .[:tag].nil? dir = File.realdirpath(@restart_dir) [:tag] = File.basename(dir) set_process_title end true end |
#refork ⇒ Object
Begin a refork if supported
186 187 188 189 190 191 192 193 194 |
# File 'lib/puma/launcher.rb', line 186 def refork if clustered? && @runner.respond_to?(:fork_worker!) && [:fork_worker] @runner.fork_worker! true else log "* refork called but not available." false end end |
#restart ⇒ Object
Begin async restart of the server
164 165 166 167 |
# File 'lib/puma/launcher.rb', line 164 def restart @status = :restart @runner.restart end |
#run ⇒ Object
Run the server. This blocks until the server is stopped
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/puma/launcher.rb', line 197 def run previous_env = get_env @config.clamp @config.plugins.fire_starts self setup_signals set_process_title # This blocks until the server is stopped @runner.run do_run_finished(previous_env) end |
#stats ⇒ Object
Return stats about the server
118 119 120 |
# File 'lib/puma/launcher.rb', line 118 def stats @runner.stats end |
#stop ⇒ Object
Begin async shutdown of the server gracefully
158 159 160 161 |
# File 'lib/puma/launcher.rb', line 158 def stop @status = :stop @runner.stop end |
#write_state ⇒ Object
Write a state file that can be used by pumactl to control the server
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/puma/launcher.rb', line 124 def write_state write_pid path = [:state] = [:state_permission] return unless path require_relative 'state_file' sf = StateFile.new sf.pid = Process.pid sf.control_url = [:control_url] sf.control_auth_token = [:control_auth_token] sf.running_from = File.('.') sf.save path, end |