2015-03-13 20:10:39 -04:00
|
|
|
module Puma
|
|
|
|
# The methods that are available for use inside the config file.
|
|
|
|
#
|
|
|
|
class DSL
|
2015-04-11 16:12:27 -04:00
|
|
|
include ConfigDefault
|
|
|
|
|
2016-02-07 01:28:02 -05:00
|
|
|
def self.load(options, cfg, path)
|
2016-02-07 17:51:54 -05:00
|
|
|
d = new(options, cfg)
|
|
|
|
d._load_from(path)
|
2015-03-13 20:10:39 -04:00
|
|
|
|
|
|
|
options
|
2016-02-07 17:51:54 -05:00
|
|
|
ensure
|
|
|
|
d._offer_plugins
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2016-02-07 02:10:48 -05:00
|
|
|
def initialize(options, config)
|
2016-02-07 01:28:02 -05:00
|
|
|
@config = config
|
2015-03-13 20:10:39 -04:00
|
|
|
@options = options
|
2016-02-07 17:51:54 -05:00
|
|
|
|
|
|
|
@plugins = []
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _load_from(path)
|
|
|
|
instance_eval(File.read(path), path, 1) if path
|
2016-02-07 17:51:54 -05:00
|
|
|
ensure
|
|
|
|
_offer_plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
def _offer_plugins
|
|
|
|
@plugins.each do |o|
|
|
|
|
if o.respond_to? :config
|
|
|
|
@options.shift
|
|
|
|
o.config self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@plugins.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def _run(&blk)
|
|
|
|
blk.call self
|
|
|
|
ensure
|
|
|
|
_offer_plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
def inject(&blk)
|
|
|
|
instance_eval(&blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load the named plugin for use by this configuration
|
|
|
|
#
|
|
|
|
def plugin(name)
|
|
|
|
@plugins << @config.load_plugin(name)
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use +obj+ or +block+ as the Rack app. This allows a config file to
|
|
|
|
# be the app itself.
|
|
|
|
#
|
|
|
|
def app(obj=nil, &block)
|
|
|
|
obj ||= block
|
|
|
|
|
|
|
|
raise "Provide either a #call'able or a block" unless obj
|
|
|
|
|
|
|
|
@options[:app] = obj
|
|
|
|
end
|
|
|
|
|
|
|
|
# Start the Puma control rack app on +url+. This app can be communicated
|
|
|
|
# with to control the main server.
|
|
|
|
#
|
2016-02-06 22:00:29 -05:00
|
|
|
def activate_control_app(url="auto", opts={})
|
|
|
|
if url == "auto"
|
|
|
|
path = Configuration.temp_path
|
|
|
|
@options[:control_url] = "unix://#{path}"
|
|
|
|
@options[:control_url_temp] = path
|
|
|
|
else
|
|
|
|
@options[:control_url] = url
|
|
|
|
end
|
2015-03-13 20:10:39 -04:00
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
if opts[:no_token]
|
|
|
|
auth_token = :none
|
|
|
|
else
|
2015-03-13 20:18:15 -04:00
|
|
|
auth_token = opts[:auth_token]
|
2016-02-06 22:00:29 -05:00
|
|
|
auth_token ||= Configuration.random_token
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
2016-02-06 22:00:29 -05:00
|
|
|
|
|
|
|
@options[:control_auth_token] = auth_token
|
|
|
|
@options[:control_url_umask] = opts[:umask] if opts[:umask]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Load additional configuration from a file
|
|
|
|
def load(file)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:config_files) << file
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Bind the server to +url+. tcp:// and unix:// are the only accepted
|
|
|
|
# protocols.
|
|
|
|
#
|
|
|
|
def bind(url)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:binds) << url
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Define the TCP port to bind to. Use +bind+ for more advanced options.
|
|
|
|
#
|
2016-02-06 22:00:29 -05:00
|
|
|
def port(port, host=nil)
|
|
|
|
host ||= Configuration::DefaultTCPHost
|
|
|
|
bind "tcp://#{host}:#{port}"
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Work around leaky apps that leave garbage in Thread locals
|
|
|
|
# across requests
|
|
|
|
#
|
|
|
|
def clean_thread_locals(which=true)
|
|
|
|
@options[:clean_thread_locals] = which
|
|
|
|
end
|
|
|
|
|
|
|
|
# Daemonize the server into the background. Highly suggest that
|
|
|
|
# this be combined with +pidfile+ and +stdout_redirect+.
|
|
|
|
def daemonize(which=true)
|
|
|
|
@options[:daemon] = which
|
|
|
|
end
|
|
|
|
|
|
|
|
# When shutting down, drain the accept socket of pending
|
|
|
|
# connections and proces 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.
|
|
|
|
def drain_on_shutdown(which=true)
|
|
|
|
@options[:drain_on_shutdown] = which
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the environment in which the Rack's app will run.
|
|
|
|
def environment(environment)
|
|
|
|
@options[:environment] = environment
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
def on_restart(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:on_restart) << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
def restart_command(cmd)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:restart_cmd] = cmd.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Store the pid of the server in the file at +path+.
|
|
|
|
def pidfile(path)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:pidfile] = path.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Disable request logging.
|
|
|
|
#
|
|
|
|
def quiet
|
|
|
|
@options[:quiet] = true
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Show debugging info
|
|
|
|
#
|
|
|
|
def debug
|
|
|
|
@options[:debug] = true
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Load +path+ as a rackup file.
|
|
|
|
#
|
|
|
|
def rackup(path)
|
|
|
|
@options[:rackup] = path.to_s
|
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
# Run Puma in TCP mode
|
|
|
|
#
|
|
|
|
def tcp_mode!
|
|
|
|
@options[:mode] = :tcp
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# Redirect STDOUT and STDERR to files specified.
|
|
|
|
def stdout_redirect(stdout=nil, stderr=nil, append=false)
|
|
|
|
@options[:redirect_stdout] = stdout
|
|
|
|
@options[:redirect_stderr] = stderr
|
|
|
|
@options[:redirect_append] = append
|
|
|
|
end
|
|
|
|
|
|
|
|
# Configure +min+ to be the minimum number of threads to use to answer
|
|
|
|
# requests and +max+ the maximum.
|
|
|
|
#
|
|
|
|
def threads(min, max)
|
|
|
|
min = Integer(min)
|
|
|
|
max = Integer(max)
|
|
|
|
if min > max
|
2015-10-13 11:30:42 -04:00
|
|
|
raise "The minimum (#{min}) number of threads must be less than or equal to the max (#{max})"
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2016-01-28 20:00:17 -05:00
|
|
|
if max < 1
|
|
|
|
raise "The maximum number of threads (#{max}) must be greater than 0"
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
@options[:min_threads] = min
|
|
|
|
@options[:max_threads] = max
|
|
|
|
end
|
|
|
|
|
|
|
|
def ssl_bind(host, port, opts)
|
2015-12-01 11:27:27 -05:00
|
|
|
if defined?(JRUBY_VERSION)
|
|
|
|
keystore_additions = "keystore=#{opts[:keystore]}&keystore-pass=#{opts[:keystore_pass]}"
|
2016-02-06 22:00:29 -05:00
|
|
|
bind "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}&#{keystore_additions}"
|
2015-12-01 11:27:27 -05:00
|
|
|
else
|
2016-02-06 22:00:29 -05:00
|
|
|
bind "ssl://#{host}:#{port}?cert=#{opts[:cert]}&key=#{opts[:key]}"
|
2015-12-01 11:27:27 -05:00
|
|
|
end
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use +path+ as the file to store the server info state. This is
|
|
|
|
# used by pumactl to query and control the server.
|
|
|
|
#
|
|
|
|
def state_path(path)
|
|
|
|
@options[:state] = path.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# *Cluster mode only* How many worker processes to run.
|
|
|
|
#
|
|
|
|
def workers(count)
|
|
|
|
@options[:workers] = count.to_i
|
|
|
|
end
|
|
|
|
|
2015-08-05 20:03:36 -04:00
|
|
|
# *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.
|
|
|
|
#
|
|
|
|
def before_fork(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:before_fork) << block
|
2015-08-05 20:03:36 -04:00
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# *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.
|
|
|
|
#
|
|
|
|
def on_worker_shutdown(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:before_worker_shutdown) << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# *Cluster mode only* Code to run when a worker boots to setup
|
|
|
|
# the process before booting the app.
|
|
|
|
#
|
|
|
|
# This can be called multiple times to add hooks.
|
|
|
|
#
|
|
|
|
def on_worker_boot(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:before_worker_boot) << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# *Cluster mode only* Code to run when a master process is
|
|
|
|
# about to create the worker by forking itself.
|
|
|
|
#
|
|
|
|
# This can be called multiple times to add hooks.
|
|
|
|
#
|
|
|
|
def on_worker_fork(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:before_worker_fork) << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# *Cluster mode only* Code to run when a worker boots to setup
|
|
|
|
# the process after booting the app.
|
|
|
|
#
|
|
|
|
# This can be called multiple times to add hooks.
|
|
|
|
#
|
|
|
|
def after_worker_boot(&block)
|
2016-02-07 17:51:54 -05:00
|
|
|
_ary(:after_worker_fork) << block
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# The directory to operate out of.
|
|
|
|
def directory(dir)
|
|
|
|
@options[:directory] = dir.to_s
|
2016-02-06 22:00:29 -05:00
|
|
|
|
|
|
|
worker_directory dir
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the directory for workers to start in
|
|
|
|
def worker_directory(dir)
|
2015-03-13 20:10:39 -04:00
|
|
|
@options[:worker_directory] = dir.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the app as a raw TCP app instead of an HTTP rack app
|
|
|
|
def tcp_mode
|
|
|
|
@options[:mode] = :tcp
|
|
|
|
end
|
|
|
|
|
|
|
|
# *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.
|
|
|
|
#
|
|
|
|
def preload_app!(answer=true)
|
|
|
|
@options[:preload_app] = answer
|
|
|
|
end
|
|
|
|
|
|
|
|
# Use +obj+ or +block+ as the low level error handler. This allows a config file to
|
|
|
|
# change the default error on the server.
|
|
|
|
#
|
|
|
|
def lowlevel_error_handler(obj=nil, &block)
|
|
|
|
obj ||= block
|
|
|
|
raise "Provide either a #call'able or a block" unless obj
|
|
|
|
@options[:lowlevel_error_handler] = obj
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def prune_bundler(answer=true)
|
|
|
|
@options[:prune_bundler] = answer
|
|
|
|
end
|
|
|
|
|
|
|
|
# Additional text to display in process listing
|
|
|
|
def tag(string)
|
2016-02-06 22:00:29 -05:00
|
|
|
@options[:tag] = string.to_s
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
|
2015-11-03 10:33:49 -05:00
|
|
|
# *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.
|
2015-03-13 20:10:39 -04:00
|
|
|
def worker_timeout(timeout)
|
|
|
|
@options[:worker_timeout] = timeout
|
|
|
|
end
|
|
|
|
|
2015-11-06 11:46:48 -05:00
|
|
|
# *Cluster mode only* Set the timeout for workers to boot
|
|
|
|
def worker_boot_timeout(timeout)
|
|
|
|
@options[:worker_boot_timeout] = timeout
|
|
|
|
end
|
|
|
|
|
2015-03-13 20:10:39 -04:00
|
|
|
# *Cluster mode only* Set the timeout for worker shutdown
|
|
|
|
def worker_shutdown_timeout(timeout)
|
|
|
|
@options[:worker_shutdown_timeout] = timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def queue_requests(answer=true)
|
|
|
|
@options[:queue_requests] = answer
|
|
|
|
end
|
2015-04-11 01:52:38 -04:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
def shutdown_debug(val=true)
|
|
|
|
@options[:shutdown_debug] = val
|
|
|
|
end
|
2016-01-06 13:12:09 -05:00
|
|
|
|
|
|
|
# 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:
|
2016-01-06 13:33:50 -05:00
|
|
|
# `set_remote_address header: "X-Real-IP"`.
|
2016-01-06 13:39:31 -05:00
|
|
|
# Only the first word (as separated by spaces or comma)
|
2016-01-06 13:33:50 -05:00
|
|
|
# is used, allowing headers such as X-Forwarded-For
|
|
|
|
# to be used as well.
|
2016-01-06 13:12:09 -05:00
|
|
|
# * 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.
|
|
|
|
def set_remote_address(val=:socket)
|
|
|
|
case val
|
|
|
|
when :socket
|
|
|
|
@options[:remote_address] = val
|
|
|
|
when :localhost
|
|
|
|
@options[:remote_address] = :value
|
|
|
|
@options[:remote_address_value] = "127.0.0.1".freeze
|
|
|
|
when String
|
|
|
|
@options[:remote_address] = :value
|
|
|
|
@options[:remote_address_value] = val
|
|
|
|
when Hash
|
|
|
|
if hdr = val[:header]
|
|
|
|
@options[:remote_address] = :header
|
|
|
|
@options[:remote_address_header] = "HTTP_" + hdr.upcase.gsub("-", "_")
|
|
|
|
else
|
|
|
|
raise "Invalid value for set_remote_address - #{val.inspect}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Invalid value for set_remote_address - #{val}"
|
|
|
|
end
|
|
|
|
end
|
2016-02-07 01:28:02 -05:00
|
|
|
|
2016-02-07 17:51:54 -05:00
|
|
|
private
|
2016-02-07 02:10:48 -05:00
|
|
|
|
2016-02-07 17:51:54 -05:00
|
|
|
def _ary(key)
|
|
|
|
(@options.cur[key] ||= [])
|
2016-02-07 01:28:02 -05:00
|
|
|
end
|
2015-03-13 20:10:39 -04:00
|
|
|
end
|
|
|
|
end
|