Class: Puma::DSL
Overview
The methods that are available for use inside the config file. These same methods are used in Puma cli and the rack handler internally.
Used manually (via CLI class):
config = Configuration.new({}) do |user_config|
user_config.port 3001
end
config.load
puts config.[:binds]
"tcp://127.0.0.1:3001"
Used to load file:
$ cat puma_config.rb
port 3002
config = Configuration.new(config_file: "puma_config.rb")
config.load
puts config.[:binds]
# => "tcp://127.0.0.1:3002"
Detailed docs can be found in ‘examples/config.rb`
Constant Summary
Constants included from ConfigDefault
ConfigDefault::DefaultRackup, ConfigDefault::DefaultTCPHost, ConfigDefault::DefaultTCPPort, ConfigDefault::DefaultWorkerShutdownTimeout, ConfigDefault::DefaultWorkerTimeout
Instance Method Summary collapse
- #_load_from(path) ⇒ Object
- #_offer_plugins ⇒ Object
-
#activate_control_app(url = "auto", opts = {}) ⇒ Object
Start the Puma control rack app on
url. -
#after_worker_fork(&block) ⇒ Object
(also: #after_worker_boot)
*Cluster mode only* Code to run in the master after it starts a worker.
-
#app(obj = nil, &block) ⇒ Object
Use
objorblockas the Rack app. -
#before_fork(&block) ⇒ Object
*Cluster mode only* Code to run immediately before master process forks workers (once on boot).
-
#bind(url) ⇒ Object
Adds a binding for the server to
url. -
#clean_thread_locals(which = true) ⇒ Object
Work around leaky apps that leave garbage in Thread locals across requests.
- #clear_binds! ⇒ Object
-
#daemonize(which = true) ⇒ Object
Daemonize the server into the background.
-
#debug ⇒ Object
Show debugging info.
- #default_host ⇒ Object
-
#directory(dir) ⇒ Object
The directory to operate out of.
-
#drain_on_shutdown(which = true) ⇒ Object
When shutting down, drain the accept socket of pending connections and process them.
- #early_hints(answer = true) ⇒ Object
-
#environment(environment) ⇒ Object
Set the environment in which the Rack’s app will run.
-
#first_data_timeout(seconds) ⇒ Object
Define how long the tcp socket stays open, if no data has been received.
-
#force_shutdown_after(val = :forever) ⇒ Object
How long to wait for threads to stop when shutting them down.
- #get(key, default = nil) ⇒ Object
-
#initialize(options, config) ⇒ DSL
constructor
A new instance of DSL.
- #inject(&blk) ⇒ Object
-
#load(file) ⇒ Object
Load additional configuration from a file Files get loaded later via Configuration#load.
-
#log_requests(which = true) ⇒ Object
Enable request logging.
-
#lowlevel_error_handler(obj = nil, &block) ⇒ Object
Use
objorblockas the low level error handler. -
#on_restart(&block) ⇒ Object
Code to run before doing a restart.
-
#on_worker_boot(&block) ⇒ Object
*Cluster mode only* Code to run in a worker when it boots to setup the process before booting the app.
-
#on_worker_fork(&block) ⇒ Object
*Cluster mode only* Code to run in the master when it is about to create the worker by forking itself.
-
#on_worker_shutdown(&block) ⇒ Object
*Cluster mode only* Code to run immediately before a worker shuts down (after it has finished processing HTTP requests).
-
#persistent_timeout(seconds) ⇒ Object
Define how long persistent connections can be idle before puma closes them.
-
#pidfile(path) ⇒ Object
Store the pid of the server in the file at
path. -
#plugin(name) ⇒ Object
Load the named plugin for use by this configuration.
-
#port(port, host = nil) ⇒ Object
Define the TCP port to bind to.
-
#preload_app!(answer = true) ⇒ Object
*Cluster mode only* Preload the application before starting the workers and setting up the listen ports.
-
#prune_bundler(answer = true) ⇒ Object
This option is used to allow your app and its gems to be properly reloaded when not using preload.
-
#queue_requests(answer = true) ⇒ Object
When set to true (the default), workers accept all requests and queue them before passing them to the handlers.
-
#quiet(which = true) ⇒ Object
Disable request logging.
-
#rackup(path) ⇒ Object
Load
pathas a rackup file. -
#restart_command(cmd) ⇒ Object
Command to use to restart puma.
- #set_default_host(host) ⇒ Object
-
#set_remote_address(val = :socket) ⇒ Object
Control how the remote address of the connection is set.
-
#shutdown_debug(val = true) ⇒ Object
When a shutdown is requested, the backtraces of all the threads will be written to $stdout.
- #ssl_bind(host, port, opts) ⇒ Object
-
#state_path(path) ⇒ Object
Use
pathas the file to store the server info state. -
#stdout_redirect(stdout = nil, stderr = nil, append = false) ⇒ Object
Redirect STDOUT and STDERR to files specified.
-
#tag(string) ⇒ Object
Additional text to display in process listing.
-
#tcp_mode ⇒ Object
Run the app as a raw TCP app instead of an HTTP rack app.
-
#tcp_mode! ⇒ Object
Run Puma in TCP mode.
-
#threads(min, max) ⇒ Object
Configure
minto be the minimum number of threads to use to answer requests andmaxthe maximum. -
#worker_boot_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for workers to boot.
-
#worker_directory(dir) ⇒ Object
DEPRECATED: The directory to operate out of.
-
#worker_shutdown_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for worker shutdown.
-
#worker_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for workers in seconds When set the master process will terminate any workers that have not checked in within the given
timeout. -
#workers(count) ⇒ Object
*Cluster mode only* How many worker processes to run.
Constructor Details
#initialize(options, config) ⇒ DSL
Returns a new instance of DSL.
33 34 35 36 37 38 |
# File 'lib/puma/dsl.rb', line 33 def initialize(, config) @config = config = @plugins = [] end |
Instance Method Details
#_load_from(path) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/puma/dsl.rb', line 40 def _load_from(path) if path @path = path instance_eval(File.read(path), path, 1) end ensure _offer_plugins end |
#_offer_plugins ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puma/dsl.rb', line 49 def _offer_plugins @plugins.each do |o| if o.respond_to? :config .shift o.config self end end @plugins.clear end |
#activate_control_app(url = "auto", opts = {}) ⇒ Object
Start the Puma control rack app on url. This app can be communicated with to control the main server.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/puma/dsl.rb', line 96 def activate_control_app(url="auto", opts={}) if url == "auto" path = Configuration.temp_path [:control_url] = "unix://#{path}" [:control_url_temp] = path else [:control_url] = url end if opts[:no_token] auth_token = :none else auth_token = opts[:auth_token] auth_token ||= Configuration.random_token end [:control_auth_token] = auth_token [:control_url_umask] = opts[:umask] if opts[:umask] end |
#after_worker_fork(&block) ⇒ Object Also known as: after_worker_boot
*Cluster mode only* Code to run in the master after it starts a worker.
This can be called multiple times to add hooks.
373 374 375 376 |
# File 'lib/puma/dsl.rb', line 373 def after_worker_fork(&block) [:after_worker_fork] ||= [] [:after_worker_fork] = block end |
#app(obj = nil, &block) ⇒ Object
Use obj or block as the Rack app. This allows a config file to be the app itself.
85 86 87 88 89 90 91 |
# File 'lib/puma/dsl.rb', line 85 def app(obj=nil, &block) obj ||= block raise "Provide either a #call'able or a block" unless obj [:app] = obj end |
#before_fork(&block) ⇒ Object
*Cluster mode only* Code to run immediately before master process forks workers (once on boot). These hooks can block if necessary to wait for background operations unknown to puma to finish before the process terminates. This can be used to close any connections to remote servers (database, redis, …) that were opened when preloading the code
This can be called multiple times to add hooks.
331 332 333 334 |
# File 'lib/puma/dsl.rb', line 331 def before_fork(&block) [:before_fork] ||= [] [:before_fork] << block end |
#bind(url) ⇒ Object
multiple urls can be bound to, calling ‘bind` does not overwrite previous bindings.
Adds a binding for the server to url. tcp://, unix://, and ssl:// are the only accepted protocols. Use query parameters within the url to specify options.
139 140 141 142 |
# File 'lib/puma/dsl.rb', line 139 def bind(url) [:binds] ||= [] [:binds] << url end |
#clean_thread_locals(which = true) ⇒ Object
Work around leaky apps that leave garbage in Thread locals across requests
171 172 173 |
# File 'lib/puma/dsl.rb', line 171 def clean_thread_locals(which=true) [:clean_thread_locals] = which end |
#clear_binds! ⇒ Object
144 145 146 |
# File 'lib/puma/dsl.rb', line 144 def clear_binds! [:binds] = [] end |
#daemonize(which = true) ⇒ Object
Daemonize the server into the background. Highly suggest that this be combined with pidfile and stdout_redirect.
177 178 179 |
# File 'lib/puma/dsl.rb', line 177 def daemonize(which=true) [:daemon] = which end |
#debug ⇒ Object
Show debugging info
251 252 253 |
# File 'lib/puma/dsl.rb', line 251 def debug [:debug] = true end |
#default_host ⇒ Object
64 65 66 |
# File 'lib/puma/dsl.rb', line 64 def default_host [:default_host] || Configuration::DefaultTCPHost end |
#directory(dir) ⇒ Object
The directory to operate out of.
381 382 383 |
# File 'lib/puma/dsl.rb', line 381 def directory(dir) [:directory] = dir.to_s end |
#drain_on_shutdown(which = true) ⇒ Object
When shutting down, drain the accept socket of pending connections and process them. This loops over the accept socket until there are no more read events and then stops looking and waits for the requests to finish.
185 186 187 |
# File 'lib/puma/dsl.rb', line 185 def drain_on_shutdown(which=true) [:drain_on_shutdown] = which end |
#early_hints(answer = true) ⇒ Object
267 268 269 |
# File 'lib/puma/dsl.rb', line 267 def early_hints(answer=true) [:early_hints] = answer end |
#environment(environment) ⇒ Object
Set the environment in which the Rack’s app will run.
190 191 192 |
# File 'lib/puma/dsl.rb', line 190 def environment(environment) [:environment] = environment end |
#first_data_timeout(seconds) ⇒ Object
Define how long the tcp socket stays open, if no data has been received
164 165 166 |
# File 'lib/puma/dsl.rb', line 164 def first_data_timeout(seconds) [:first_data_timeout] = Integer(seconds) end |
#force_shutdown_after(val = :forever) ⇒ Object
How long to wait for threads to stop when shutting them down. Defaults to :forever. Specifying :immediately will cause Puma to kill the threads immediately. Otherwise the value is the number of seconds to wait.
Puma always waits a few seconds after killing a thread for it to try to finish up it’s work, even in :immediately mode.
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/puma/dsl.rb', line 201 def force_shutdown_after(val=:forever) i = case val when :forever -1 when :immediately 0 else Integer(val) end [:force_shutdown_after] = i end |
#get(key, default = nil) ⇒ Object
72 73 74 |
# File 'lib/puma/dsl.rb', line 72 def get(key,default=nil) [key.to_sym] || default end |
#inject(&blk) ⇒ Object
68 69 70 |
# File 'lib/puma/dsl.rb', line 68 def inject(&blk) instance_eval(&blk) end |
#load(file) ⇒ Object
Load additional configuration from a file Files get loaded later via Configuration#load
118 119 120 121 |
# File 'lib/puma/dsl.rb', line 118 def load(file) [:config_files] ||= [] [:config_files] << file end |
#log_requests(which = true) ⇒ Object
Enable request logging
245 246 247 |
# File 'lib/puma/dsl.rb', line 245 def log_requests(which=true) [:log_requests] = which end |
#lowlevel_error_handler(obj = nil, &block) ⇒ Object
Use obj or block as the low level error handler. This allows a config file to change the default error on the server.
407 408 409 410 411 |
# File 'lib/puma/dsl.rb', line 407 def lowlevel_error_handler(obj=nil, &block) obj ||= block raise "Provide either a #call'able or a block" unless obj [:lowlevel_error_handler] = obj end |
#on_restart(&block) ⇒ Object
Code to run before doing a restart. This code should close logfiles, database connections, etc.
This can be called multiple times to add code each time.
219 220 221 222 |
# File 'lib/puma/dsl.rb', line 219 def on_restart(&block) [:on_restart] ||= [] [:on_restart] << block end |
#on_worker_boot(&block) ⇒ Object
*Cluster mode only* Code to run in a worker when it boots to setup the process before booting the app.
This can be called multiple times to add hooks.
341 342 343 344 |
# File 'lib/puma/dsl.rb', line 341 def on_worker_boot(&block) [:before_worker_boot] ||= [] [:before_worker_boot] << block end |
#on_worker_fork(&block) ⇒ Object
*Cluster mode only* Code to run in the master when it is about to create the worker by forking itself.
This can be called multiple times to add hooks.
363 364 365 366 |
# File 'lib/puma/dsl.rb', line 363 def on_worker_fork(&block) [:before_worker_fork] ||= [] [:before_worker_fork] << block end |
#on_worker_shutdown(&block) ⇒ Object
*Cluster mode only* Code to run immediately before a worker shuts down (after it has finished processing HTTP requests). These hooks can block if necessary to wait for background operations unknown to puma to finish before the process terminates.
This can be called multiple times to add hooks.
353 354 355 356 |
# File 'lib/puma/dsl.rb', line 353 def on_worker_shutdown(&block) [:before_worker_shutdown] ||= [] [:before_worker_shutdown] << block end |
#persistent_timeout(seconds) ⇒ Object
Define how long persistent connections can be idle before puma closes them
158 159 160 |
# File 'lib/puma/dsl.rb', line 158 def persistent_timeout(seconds) [:persistent_timeout] = Integer(seconds) end |
#pidfile(path) ⇒ Object
Store the pid of the server in the file at path.
233 234 235 |
# File 'lib/puma/dsl.rb', line 233 def pidfile(path) [:pidfile] = path.to_s end |
#plugin(name) ⇒ Object
Load the named plugin for use by this configuration
78 79 80 |
# File 'lib/puma/dsl.rb', line 78 def plugin(name) @plugins << @config.load_plugin(name) end |
#port(port, host = nil) ⇒ Object
Define the TCP port to bind to. Use bind for more advanced options.
150 151 152 153 |
# File 'lib/puma/dsl.rb', line 150 def port(port, host=nil) host ||= default_host bind "tcp://#{host}:#{port}" end |
#preload_app!(answer = true) ⇒ Object
*Cluster mode only* Preload the application before starting the workers and setting up the listen ports. This conflicts with using the phased restart feature, you can’t use both.
400 401 402 |
# File 'lib/puma/dsl.rb', line 400 def preload_app!(answer=true) [:preload_app] = answer end |
#prune_bundler(answer = true) ⇒ Object
This option is used to allow your app and its gems to be properly reloaded when not using preload.
When set, if puma detects that it’s been invoked in the context of Bundler, it will cleanup the environment and re-run itself outside the Bundler environment, but directly using the files that Bundler has setup.
This means that puma is now decoupled from your Bundler context and when each worker loads, it will be loading a new Bundler context and thus can float around as the release dictates.
425 426 427 |
# File 'lib/puma/dsl.rb', line 425 def prune_bundler(answer=true) [:prune_bundler] = answer end |
#queue_requests(answer = true) ⇒ Object
When set to true (the default), workers accept all requests and queue them before passing them to the handlers. When set to false, each worker process accepts exactly as many requests as it is configured to simultaneously handle.
Queueing requests generally improves performance. In some cases, such as a single threaded application, it may be better to ensure requests get balanced across workers.
Note that setting this to false disables HTTP keepalive and slow clients will occupy a handler thread while the request is being sent. A reverse proxy, such as nginx, can handle slow clients and queue requests before they reach puma.
465 466 467 |
# File 'lib/puma/dsl.rb', line 465 def queue_requests(answer=true) [:queue_requests] = answer end |
#quiet(which = true) ⇒ Object
Disable request logging.
239 240 241 |
# File 'lib/puma/dsl.rb', line 239 def quiet(which=true) [:log_requests] = !which end |
#rackup(path) ⇒ Object
Load path as a rackup file.
257 258 259 |
# File 'lib/puma/dsl.rb', line 257 def rackup(path) [:rackup] = path.to_s end |
#restart_command(cmd) ⇒ Object
Command to use to restart puma. This should be just how to load puma itself (ie. ‘ruby -Ilib bin/puma’), not the arguments to puma, as those are the same as the original process.
228 229 230 |
# File 'lib/puma/dsl.rb', line 228 def restart_command(cmd) [:restart_cmd] = cmd.to_s end |
#set_default_host(host) ⇒ Object
60 61 62 |
# File 'lib/puma/dsl.rb', line 60 def set_default_host(host) [:default_host] = host end |
#set_remote_address(val = :socket) ⇒ Object
Control how the remote address of the connection is set. This is configurable because to calculate the true socket peer address a kernel syscall is required which for very fast rack handlers slows down the handling significantly.
There are 4 possible values:
-
:socket (the default) - read the peername from the socket using the
syscall. This is the normal behavior. -
:localhost - set the remote address to “127.0.0.1”
-
header: http_header - set the remote address to the value of the
provided http header. For instance: `set_remote_address header: "X-Real-IP"`. Only the first word (as separated by spaces or comma) is used, allowing headers such as X-Forwarded-For to be used as well. -
Any string - this allows you to hardcode remote address to any value
you wish. Because puma never uses this field anyway, it's format is entirely in your hands.
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/puma/dsl.rb', line 495 def set_remote_address(val=:socket) case val when :socket [:remote_address] = val when :localhost [:remote_address] = :value [:remote_address_value] = "127.0.0.1".freeze when String [:remote_address] = :value [:remote_address_value] = val when Hash if hdr = val[:header] [:remote_address] = :header [:remote_address_header] = "HTTP_" + hdr.upcase.tr("-", "_") else raise "Invalid value for set_remote_address - #{val.inspect}" end else raise "Invalid value for set_remote_address - #{val}" end end |
#shutdown_debug(val = true) ⇒ Object
When a shutdown is requested, the backtraces of all the threads will be written to $stdout. This can help figure out why shutdown is hanging.
472 473 474 |
# File 'lib/puma/dsl.rb', line 472 def shutdown_debug(val=true) [:shutdown_debug] = val end |
#ssl_bind(host, port, opts) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/puma/dsl.rb', line 296 def ssl_bind(host, port, opts) verify = opts.fetch(:verify_mode, 'none') additions = +'' if defined?(JRUBY_VERSION) additions << "keystore=#{opts[:keystore]}&keystore-pass=#{opts[:keystore_pass]}" else Array(opts[:verification_flags]).each { |flag| additions << "&verification_flags=#{flag}" } end bind "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}&verify_mode=#{verify}#{additions}" end |
#state_path(path) ⇒ Object
Use path as the file to store the server info state. This is used by pumactl to query and control the server.
312 313 314 |
# File 'lib/puma/dsl.rb', line 312 def state_path(path) [:state] = path.to_s end |
#stdout_redirect(stdout = nil, stderr = nil, append = false) ⇒ Object
Redirect STDOUT and STDERR to files specified.
272 273 274 275 276 |
# File 'lib/puma/dsl.rb', line 272 def stdout_redirect(stdout=nil, stderr=nil, append=false) [:redirect_stdout] = stdout [:redirect_stderr] = stderr [:redirect_append] = append end |
#tag(string) ⇒ Object
Additional text to display in process listing
430 431 432 |
# File 'lib/puma/dsl.rb', line 430 def tag(string) [:tag] = string.to_s end |
#tcp_mode ⇒ Object
Run the app as a raw TCP app instead of an HTTP rack app
392 393 394 |
# File 'lib/puma/dsl.rb', line 392 def tcp_mode [:mode] = :tcp end |
#tcp_mode! ⇒ Object
Run Puma in TCP mode
263 264 265 |
# File 'lib/puma/dsl.rb', line 263 def tcp_mode! [:mode] = :tcp end |
#threads(min, max) ⇒ Object
Configure min to be the minimum number of threads to use to answer requests and max the maximum.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/puma/dsl.rb', line 281 def threads(min, max) min = Integer(min) max = Integer(max) if min > max raise "The minimum (#{min}) number of threads must be less than or equal to the max (#{max})" end if max < 1 raise "The maximum number of threads (#{max}) must be greater than 0" end [:min_threads] = min [:max_threads] = max end |
#worker_boot_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for workers to boot
443 444 445 |
# File 'lib/puma/dsl.rb', line 443 def worker_boot_timeout(timeout) [:worker_boot_timeout] = Integer(timeout) end |
#worker_directory(dir) ⇒ Object
DEPRECATED: The directory to operate out of.
386 387 388 389 |
# File 'lib/puma/dsl.rb', line 386 def worker_directory(dir) $stderr.puts "worker_directory is deprecated. Please use `directory`" directory dir end |
#worker_shutdown_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for worker shutdown
448 449 450 |
# File 'lib/puma/dsl.rb', line 448 def worker_shutdown_timeout(timeout) [:worker_shutdown_timeout] = Integer(timeout) end |
#worker_timeout(timeout) ⇒ Object
*Cluster mode only* Set the timeout for workers in seconds When set the master process will terminate any workers that have not checked in within the given timeout. This mitigates hung processes. Default value is 60 seconds.
438 439 440 |
# File 'lib/puma/dsl.rb', line 438 def worker_timeout(timeout) [:worker_timeout] = Integer(timeout) end |
#workers(count) ⇒ Object
*Cluster mode only* How many worker processes to run.
318 319 320 |
# File 'lib/puma/dsl.rb', line 318 def workers(count) [:workers] = count.to_i end |