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 =
[
  :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 |c|
  c.threads 1, 10
  c.app do |env|
    [200, {}, ["hello world"]]
  end
end
Puma::Launcher.new(conf, argv: 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
# 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)
  @binder.import_from_env

  @environment = conf.environment

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

  @config.load

  @options = @config.options

  generate_restart_data

  if clustered? && (Puma.jruby? || Puma.windows?)
    unsupported 'worker mode not supported on JRuby or Windows'
  end

  if @options[:daemon] && Puma.windows?
    unsupported 'daemon mode not supported on Windows'
  end

  Dir.chdir(@restart_dir)

  prune_bundler if prune_bundler?

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

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

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

  @status = :run
end

Instance Attribute Details

#binderObject (readonly)

Returns the value of attribute binder.



92
93
94
# File 'lib/puma/launcher.rb', line 92

def binder
  @binder
end

#configObject (readonly)

Returns the value of attribute config.



92
93
94
# File 'lib/puma/launcher.rb', line 92

def config
  @config
end

#eventsObject (readonly)

Returns the value of attribute events.



92
93
94
# File 'lib/puma/launcher.rb', line 92

def events
  @events
end

#optionsObject (readonly)

Returns the value of attribute options.



92
93
94
# File 'lib/puma/launcher.rb', line 92

def options
  @options
end

#restart_dirObject (readonly)

Returns the value of attribute restart_dir.



92
93
94
# File 'lib/puma/launcher.rb', line 92

def restart_dir
  @restart_dir
end

Instance Method Details

#connected_portObject

Return which tcp port the launcher is using, if it’s using TCP



188
189
190
# File 'lib/puma/launcher.rb', line 188

def connected_port
  @binder.connected_port
end

#delete_pidfileObject

Delete the configured pidfile



118
119
120
121
# File 'lib/puma/launcher.rb', line 118

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

#haltObject

Begin async shutdown of the server



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

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

#phased_restartObject

Begin a phased restart if supported



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

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

#restartObject

Begin async restart of the server



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

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

#restart_argsObject



192
193
194
195
196
197
198
199
# File 'lib/puma/launcher.rb', line 192

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

#runObject

Run the server. This blocks until the server is stopped



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/puma/launcher.rb', line 164

def run
  @config.clamp

  @config.plugins.fire_starts self

  setup_signals
  set_process_title
  @runner.run

  case @status
  when :halt
    log "* Stopping immediately!"
  when :run, :stop
    graceful_stop
  when :restart
    log "* Restarting..."
    @runner.before_restart
    restart!
  when :exit
    # nothing
  end
end

#statsObject

Return stats about the server



95
96
97
# File 'lib/puma/launcher.rb', line 95

def stats
  @runner.stats
end

#stopObject

Begin async shutdown of the server gracefully



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

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

#write_pidObject

If configured, write the pid of the current process out to a file.



125
126
127
128
129
130
131
132
133
134
# File 'lib/puma/launcher.rb', line 125

def write_pid
  path = @options[:pidfile]
  return unless path

  File.open(path, 'w') { |f| f.puts Process.pid }
  cur = Process.pid
  at_exit do
    delete_pidfile if cur == Process.pid
  end
end

#write_stateObject

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



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

def write_state
  write_pid

  path = @options[:state]
  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.save path
end