puma 2.0.0.b5 → 5.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/History.md +1598 -0
- data/LICENSE +23 -20
- data/README.md +222 -62
- data/bin/puma-wild +31 -0
- data/bin/pumactl +1 -1
- data/docs/architecture.md +37 -0
- data/docs/deployment.md +113 -0
- data/docs/fork_worker.md +31 -0
- data/docs/images/puma-connection-flow-no-reactor.png +0 -0
- data/docs/images/puma-connection-flow.png +0 -0
- data/docs/images/puma-general-arch.png +0 -0
- data/docs/jungle/README.md +13 -0
- data/docs/jungle/rc.d/README.md +74 -0
- data/docs/jungle/rc.d/puma +61 -0
- data/docs/jungle/rc.d/puma.conf +10 -0
- data/docs/jungle/upstart/README.md +61 -0
- data/docs/jungle/upstart/puma-manager.conf +31 -0
- data/docs/jungle/upstart/puma.conf +69 -0
- data/docs/nginx.md +5 -10
- data/docs/plugins.md +38 -0
- data/docs/restart.md +41 -0
- data/docs/signals.md +97 -0
- data/docs/systemd.md +228 -0
- data/ext/puma_http11/PumaHttp11Service.java +2 -2
- data/ext/puma_http11/extconf.rb +23 -2
- data/ext/puma_http11/http11_parser.c +301 -482
- data/ext/puma_http11/http11_parser.h +13 -11
- data/ext/puma_http11/http11_parser.java.rl +26 -42
- data/ext/puma_http11/http11_parser.rl +22 -21
- data/ext/puma_http11/http11_parser_common.rl +5 -5
- data/ext/puma_http11/mini_ssl.c +377 -18
- data/ext/puma_http11/org/jruby/puma/Http11.java +108 -107
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +137 -170
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +265 -191
- data/ext/puma_http11/puma_http11.c +57 -81
- data/lib/puma.rb +25 -4
- data/lib/puma/accept_nonblock.rb +7 -1
- data/lib/puma/app/status.rb +61 -24
- data/lib/puma/binder.rb +212 -78
- data/lib/puma/cli.rb +149 -644
- data/lib/puma/client.rb +316 -65
- data/lib/puma/cluster.rb +659 -0
- data/lib/puma/commonlogger.rb +108 -0
- data/lib/puma/configuration.rb +279 -180
- data/lib/puma/const.rb +126 -39
- data/lib/puma/control_cli.rb +183 -96
- data/lib/puma/detect.rb +20 -1
- data/lib/puma/dsl.rb +776 -0
- data/lib/puma/events.rb +91 -23
- data/lib/puma/io_buffer.rb +9 -5
- data/lib/puma/jruby_restart.rb +9 -5
- data/lib/puma/launcher.rb +487 -0
- data/lib/puma/minissl.rb +239 -93
- data/lib/puma/minissl/context_builder.rb +76 -0
- data/lib/puma/null_io.rb +22 -12
- data/lib/puma/plugin.rb +111 -0
- data/lib/puma/plugin/tmp_restart.rb +36 -0
- data/lib/puma/rack/builder.rb +297 -0
- data/lib/puma/rack/urlmap.rb +93 -0
- data/lib/puma/rack_default.rb +9 -0
- data/lib/puma/reactor.rb +290 -43
- data/lib/puma/runner.rb +163 -0
- data/lib/puma/server.rb +493 -126
- data/lib/puma/single.rb +66 -0
- data/lib/puma/state_file.rb +34 -0
- data/lib/puma/thread_pool.rb +228 -47
- data/lib/puma/util.rb +115 -0
- data/lib/rack/handler/puma.rb +78 -31
- data/tools/Dockerfile +16 -0
- data/tools/trickletest.rb +44 -0
- metadata +60 -155
- data/COPYING +0 -55
- data/Gemfile +0 -8
- data/History.txt +0 -196
- data/Manifest.txt +0 -56
- data/Rakefile +0 -121
- data/TODO +0 -5
- data/docs/config.md +0 -0
- data/ext/puma_http11/io_buffer.c +0 -154
- data/lib/puma/capistrano.rb +0 -26
- data/lib/puma/compat.rb +0 -11
- data/lib/puma/daemon_ext.rb +0 -20
- data/lib/puma/delegation.rb +0 -11
- data/lib/puma/java_io_buffer.rb +0 -45
- data/lib/puma/rack_patch.rb +0 -25
- data/puma.gemspec +0 -45
- data/test/test_app_status.rb +0 -88
- data/test/test_cli.rb +0 -171
- data/test/test_config.rb +0 -16
- data/test/test_http10.rb +0 -27
- data/test/test_http11.rb +0 -126
- data/test/test_integration.rb +0 -150
- data/test/test_iobuffer.rb +0 -38
- data/test/test_minissl.rb +0 -22
- data/test/test_null_io.rb +0 -31
- data/test/test_persistent.rb +0 -238
- data/test/test_puma_server.rb +0 -128
- data/test/test_rack_handler.rb +0 -10
- data/test/test_rack_server.rb +0 -141
- data/test/test_thread_pool.rb +0 -146
- data/test/test_unix_socket.rb +0 -39
- data/test/test_ws.rb +0 -89
- data/tools/jungle/README.md +0 -54
- data/tools/jungle/puma +0 -332
- data/tools/jungle/run-puma +0 -3
@@ -0,0 +1,69 @@
|
|
1
|
+
# /etc/init/puma.conf - Puma config
|
2
|
+
|
3
|
+
# This example config should work with Ubuntu 12.04+. It
|
4
|
+
# allows you to manage multiple Puma instances with
|
5
|
+
# Upstart, Ubuntu's native service management tool.
|
6
|
+
#
|
7
|
+
# See puma-manager.conf for how to manage all Puma instances at once.
|
8
|
+
#
|
9
|
+
# Save this config as /etc/init/puma.conf then manage puma with:
|
10
|
+
# sudo start puma app=PATH_TO_APP
|
11
|
+
# sudo stop puma app=PATH_TO_APP
|
12
|
+
# sudo status puma app=PATH_TO_APP
|
13
|
+
#
|
14
|
+
# or use the service command:
|
15
|
+
# sudo service puma {start,stop,restart,status}
|
16
|
+
#
|
17
|
+
|
18
|
+
description "Puma Background Worker"
|
19
|
+
|
20
|
+
# no "start on", we don't want to automatically start
|
21
|
+
stop on (stopping puma-manager or runlevel [06])
|
22
|
+
|
23
|
+
# change apps to match your deployment user if you want to use this as a less privileged user (recommended!)
|
24
|
+
setuid apps
|
25
|
+
setgid apps
|
26
|
+
|
27
|
+
respawn
|
28
|
+
respawn limit 3 30
|
29
|
+
|
30
|
+
instance ${app}
|
31
|
+
|
32
|
+
script
|
33
|
+
# this script runs in /bin/sh by default
|
34
|
+
# respawn as bash so we can source in rbenv/rvm
|
35
|
+
# quoted heredoc to tell /bin/sh not to interpret
|
36
|
+
# variables
|
37
|
+
|
38
|
+
# source ENV variables manually as Upstart doesn't, eg:
|
39
|
+
#. /etc/environment
|
40
|
+
|
41
|
+
exec /bin/bash <<'EOT'
|
42
|
+
# set HOME to the setuid user's home, there doesn't seem to be a better, portable way
|
43
|
+
export HOME="$(eval echo ~$(id -un))"
|
44
|
+
|
45
|
+
if [ -d "/usr/local/rbenv/bin" ]; then
|
46
|
+
export PATH="/usr/local/rbenv/bin:/usr/local/rbenv/shims:$PATH"
|
47
|
+
elif [ -d "$HOME/.rbenv/bin" ]; then
|
48
|
+
export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
|
49
|
+
elif [ -f /etc/profile.d/rvm.sh ]; then
|
50
|
+
source /etc/profile.d/rvm.sh
|
51
|
+
elif [ -f /usr/local/rvm/scripts/rvm ]; then
|
52
|
+
source /etc/profile.d/rvm.sh
|
53
|
+
elif [ -f "$HOME/.rvm/scripts/rvm" ]; then
|
54
|
+
source "$HOME/.rvm/scripts/rvm"
|
55
|
+
elif [ -f /usr/local/share/chruby/chruby.sh ]; then
|
56
|
+
source /usr/local/share/chruby/chruby.sh
|
57
|
+
if [ -f /usr/local/share/chruby/auto.sh ]; then
|
58
|
+
source /usr/local/share/chruby/auto.sh
|
59
|
+
fi
|
60
|
+
# if you aren't using auto, set your version here
|
61
|
+
# chruby 2.0.0
|
62
|
+
fi
|
63
|
+
|
64
|
+
cd $app
|
65
|
+
logger -t puma "Starting server: $app"
|
66
|
+
|
67
|
+
exec bundle exec puma -C config/puma.rb
|
68
|
+
EOT
|
69
|
+
end script
|
data/docs/nginx.md
CHANGED
@@ -34,13 +34,13 @@ server {
|
|
34
34
|
proxy_set_header Host $http_host;
|
35
35
|
|
36
36
|
# If the file exists as a static file serve it directly without
|
37
|
-
# running all the other
|
37
|
+
# running all the other rewrite tests on it
|
38
38
|
if (-f $request_filename) {
|
39
39
|
break;
|
40
40
|
}
|
41
41
|
|
42
42
|
# check for index.html for directory index
|
43
|
-
# if
|
43
|
+
# if it's there on the filesystem then rewrite
|
44
44
|
# the url to add /index.html to the end of it
|
45
45
|
# and then break to send it to the next config rules.
|
46
46
|
if (-f $request_filename/index.html) {
|
@@ -50,10 +50,10 @@ server {
|
|
50
50
|
# this is the meat of the rack page caching config
|
51
51
|
# it adds .html to the end of the url and then checks
|
52
52
|
# the filesystem for that file. If it exists, then we
|
53
|
-
#
|
53
|
+
# rewrite the url to have explicit .html on the end
|
54
54
|
# and then send it on its way to the next config rule.
|
55
55
|
# if there is no file on the fs then it sets all the
|
56
|
-
# necessary headers and proxies to our upstream
|
56
|
+
# necessary headers and proxies to our upstream pumas
|
57
57
|
if (-f $request_filename.html) {
|
58
58
|
rewrite (.*) $1.html break;
|
59
59
|
}
|
@@ -66,12 +66,7 @@ server {
|
|
66
66
|
|
67
67
|
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
|
68
68
|
# BUT there's a chance it could break the ajax calls.
|
69
|
-
location ~* \.(ico|css|gif|jpe?g|png)(\?[0-9]+)?$ {
|
70
|
-
expires max;
|
71
|
-
break;
|
72
|
-
}
|
73
|
-
|
74
|
-
location ~ ^/javascripts/.*\.js(\?[0-9]+)?$ {
|
69
|
+
location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
|
75
70
|
expires max;
|
76
71
|
break;
|
77
72
|
}
|
data/docs/plugins.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
## Plugins
|
2
|
+
|
3
|
+
Puma 3.0 added support for plugins that can augment configuration and service
|
4
|
+
operations.
|
5
|
+
|
6
|
+
2 canonical plugins to look to aid in development of further plugins:
|
7
|
+
|
8
|
+
* [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb):
|
9
|
+
Restarts the server if the file `tmp/restart.txt` is touched
|
10
|
+
* [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb):
|
11
|
+
Packages up the default configuration used by puma on Heroku
|
12
|
+
|
13
|
+
Plugins are activated in a puma configuration file (such as `config/puma.rb'`)
|
14
|
+
by adding `plugin "name"`, such as `plugin "heroku"`.
|
15
|
+
|
16
|
+
Plugins are activated based simply on path requirements so, activating the
|
17
|
+
`heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This
|
18
|
+
allows gems to provide multiple plugins (as well as unrelated gems to provide
|
19
|
+
puma plugins).
|
20
|
+
|
21
|
+
The `tmp_restart` plugin is bundled with puma, so it can always be used.
|
22
|
+
|
23
|
+
To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
|
24
|
+
|
25
|
+
### API
|
26
|
+
|
27
|
+
## Server-wide hooks
|
28
|
+
|
29
|
+
Plugins can use a couple of hooks at server level: `start` and `config`.
|
30
|
+
|
31
|
+
`start` runs when the server has started and allows the plugin to start other
|
32
|
+
functionality to augment puma.
|
33
|
+
|
34
|
+
`config` runs when the server is being configured and is passed a `Puma::DSL`
|
35
|
+
object that can be used to add additional configuration.
|
36
|
+
|
37
|
+
Any public methods in `Puma::Plugin` are the public API that any plugin may
|
38
|
+
use.
|
data/docs/restart.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Restarts
|
2
|
+
|
3
|
+
To perform a restart, there are 3 builtin mechanisms:
|
4
|
+
|
5
|
+
* Send the `puma` process the `SIGUSR2` signal (normal restart)
|
6
|
+
* Send the `puma` process the `SIGUSR1` signal (restart in phases (a "rolling restart"), cluster mode only)
|
7
|
+
* Use the status server and issue `/restart`
|
8
|
+
|
9
|
+
No code is shared between the current and restarted process, so it should be safe to issue a restart any place where you would manually stop Puma and start it again.
|
10
|
+
|
11
|
+
If the new process is unable to load, it will simply exit. You should therefore run Puma under a process monitor (see below) when using it in production.
|
12
|
+
|
13
|
+
### Normal vs Hot vs Phased Restart
|
14
|
+
|
15
|
+
A hot restart means that no requests will be lost while deploying your new code, since the server socket is kept open between restarts.
|
16
|
+
|
17
|
+
But beware, hot restart does not mean that the incoming requests won’t hang for multiple seconds while your new code has not fully deployed. If you need a zero downtime and zero hanging requests deploy, you must use phased restart.
|
18
|
+
|
19
|
+
When you run pumactl phased-restart, Puma kills workers one-by-one, meaning that at least another worker is still available to serve requests, which lead to zero hanging requests (yay!).
|
20
|
+
|
21
|
+
But again beware, upgrading an application sometimes involves upgrading the database schema. With phased restart, there may be a moment during the deployment where processes belonging to the previous version and processes belonging to the new version both exist at the same time. Any database schema upgrades you perform must therefore be backwards-compatible with the old application version.
|
22
|
+
|
23
|
+
If you perform a lot of database migrations, you probably should not use phased restart and use a normal/hot restart instead (`pumactl restart`). That way, no code is shared while deploying (in that case, `preload_app!` might help for quicker deployment, see ["Clustered Mode" in the README](../README.md#clustered-mode)).
|
24
|
+
|
25
|
+
**Note**: Hot and phased restarts are only available on MRI, not on JRuby. They are also unavailable on Windows servers.
|
26
|
+
|
27
|
+
### Release Directory
|
28
|
+
|
29
|
+
If your symlink releases into a common working directory (i.e., `/current` from Capistrano), Puma won't pick up your new changes when running phased restarts without additional configuration. You should set your working directory within Puma's config to specify the directory it should use. This is a change from earlier versions of Puma (< 2.15) that would infer the directory for you.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# config/puma.rb
|
33
|
+
|
34
|
+
directory '/var/www/current'
|
35
|
+
```
|
36
|
+
|
37
|
+
### Cleanup Code
|
38
|
+
|
39
|
+
Puma isn't able to understand all the resources that your app may use, so it provides a hook in the configuration file you pass to `-C` called `on_restart`. The block passed to `on_restart` will be called, unsurprisingly, just before Puma restarts itself.
|
40
|
+
|
41
|
+
You should place code to close global log files, redis connections, etc. in this block so that their file descriptors don't leak into the restarted process. Failure to do so will result in slowly running out of descriptors and eventually obscure crashes as the server is restarted many times.
|
data/docs/signals.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
The [unix signal](http://en.wikipedia.org/wiki/Unix_signal) is a method of sending messages between [processes](http://en.wikipedia.org/wiki/Process_(computing)). When a signal is sent, the operating system interrupts the target process's normal flow of execution. There are standard signals that are used to stop a process but there are also custom signals that can be used for other purposes. This document is an attempt to list all supported signals that Puma will respond to. In general, signals need only be sent to the master process of a cluster.
|
2
|
+
|
3
|
+
## Sending Signals
|
4
|
+
|
5
|
+
If you are new to signals it can be useful to see how they can be used. When a process is created in a *nix like operating system it will have a [PID - or process identifier](http://en.wikipedia.org/wiki/Process_identifier) that can be used to send signals to the process. For demonstration we will create an infinitely running process by tailing a file:
|
6
|
+
|
7
|
+
```sh
|
8
|
+
$ echo "foo" >> my.log
|
9
|
+
$ irb
|
10
|
+
> pid = Process.spawn 'tail -f my.log'
|
11
|
+
```
|
12
|
+
|
13
|
+
From here we can see that the tail process is running by using the `ps` command:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ ps aux | grep tail
|
17
|
+
schneems 87152 0.0 0.0 2432772 492 s032 S+ 12:46PM 0:00.00 tail -f my.log
|
18
|
+
```
|
19
|
+
|
20
|
+
You can send a signal in Ruby using the [Process module](http://www.ruby-doc.org/core-2.1.1/Process.html#kill-method):
|
21
|
+
|
22
|
+
```
|
23
|
+
$ irb
|
24
|
+
> puts pid
|
25
|
+
=> 87152
|
26
|
+
Process.detach(pid) # http://ruby-doc.org/core-2.1.1/Process.html#method-c-detach
|
27
|
+
Process.kill("TERM", pid)
|
28
|
+
```
|
29
|
+
|
30
|
+
Now you will see via `ps` that there is no more `tail` process. Sometimes when referring to signals the `SIG` prefix will be used for instance `SIGTERM` is equivalent to sending `TERM` via `Process.kill`.
|
31
|
+
|
32
|
+
## Puma Signals
|
33
|
+
|
34
|
+
Puma cluster responds to these signals:
|
35
|
+
|
36
|
+
- `TTIN` increment the worker count by 1
|
37
|
+
- `TTOU` decrement the worker count by 1
|
38
|
+
- `TERM` send `TERM` to worker. Worker will attempt to finish then exit.
|
39
|
+
- `USR2` restart workers. This also reloads puma configuration file, if there is one.
|
40
|
+
- `USR1` restart workers in phases, a rolling restart. This will not reload configuration file.
|
41
|
+
- `HUP` reopen log files defined in stdout_redirect configuration parameter. If there is no stdout_redirect option provided it will behave like `INT`
|
42
|
+
- `INT` equivalent of sending Ctrl-C to cluster. Will attempt to finish then exit.
|
43
|
+
- `CHLD`
|
44
|
+
- `URG` refork workers in phases from worker 0, if `fork_workers` option is enabled.
|
45
|
+
|
46
|
+
## Callbacks order in case of different signals
|
47
|
+
|
48
|
+
### Start application
|
49
|
+
|
50
|
+
```
|
51
|
+
puma configuration file reloaded, if there is one
|
52
|
+
* Pruning Bundler environment
|
53
|
+
puma configuration file reloaded, if there is one
|
54
|
+
|
55
|
+
before_fork
|
56
|
+
on_worker_fork
|
57
|
+
after_worker_fork
|
58
|
+
|
59
|
+
Gemfile in context
|
60
|
+
|
61
|
+
on_worker_boot
|
62
|
+
|
63
|
+
Code of the app is loaded and running
|
64
|
+
```
|
65
|
+
|
66
|
+
### Send USR2
|
67
|
+
|
68
|
+
```
|
69
|
+
on_worker_shutdown
|
70
|
+
on_restart
|
71
|
+
|
72
|
+
puma configuration file reloaded, if there is one
|
73
|
+
|
74
|
+
before_fork
|
75
|
+
on_worker_fork
|
76
|
+
after_worker_fork
|
77
|
+
|
78
|
+
Gemfile in context
|
79
|
+
|
80
|
+
on_worker_boot
|
81
|
+
|
82
|
+
Code of the app is loaded and running
|
83
|
+
```
|
84
|
+
|
85
|
+
### Send USR1
|
86
|
+
|
87
|
+
```
|
88
|
+
on_worker_shutdown
|
89
|
+
on_worker_fork
|
90
|
+
after_worker_fork
|
91
|
+
|
92
|
+
Gemfile in context
|
93
|
+
|
94
|
+
on_worker_boot
|
95
|
+
|
96
|
+
Code of the app is loaded and running
|
97
|
+
```
|
data/docs/systemd.md
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
# systemd
|
2
|
+
|
3
|
+
[systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a
|
4
|
+
commonly available init system (PID 1) on many Linux distributions. It
|
5
|
+
offers process monitoring (including automatic restarts) and other
|
6
|
+
useful features for running Puma in production.
|
7
|
+
|
8
|
+
## Service Configuration
|
9
|
+
|
10
|
+
Below is a sample puma.service configuration file for systemd, which
|
11
|
+
can be copied or symlinked to /etc/systemd/system/puma.service, or if
|
12
|
+
desired, using an application or instance specific name.
|
13
|
+
|
14
|
+
Note that this uses the systemd preferred "simple" type where the
|
15
|
+
start command remains running in the foreground (does not fork and
|
16
|
+
exit).
|
17
|
+
|
18
|
+
~~~~ ini
|
19
|
+
[Unit]
|
20
|
+
Description=Puma HTTP Server
|
21
|
+
After=network.target
|
22
|
+
|
23
|
+
# Uncomment for socket activation (see below)
|
24
|
+
# Requires=puma.socket
|
25
|
+
|
26
|
+
[Service]
|
27
|
+
# Foreground process (do not use --daemon in ExecStart or config.rb)
|
28
|
+
Type=simple
|
29
|
+
|
30
|
+
# Preferably configure a non-privileged user
|
31
|
+
# User=
|
32
|
+
|
33
|
+
# The path to the your application code root directory.
|
34
|
+
# Also replace the "<YOUR_APP_PATH>" place holders below with this path.
|
35
|
+
# Example /home/username/myapp
|
36
|
+
WorkingDirectory=<YOUR_APP_PATH>
|
37
|
+
|
38
|
+
# Helpful for debugging socket activation, etc.
|
39
|
+
# Environment=PUMA_DEBUG=1
|
40
|
+
|
41
|
+
# SystemD will not run puma even if it is in your path. You must specify
|
42
|
+
# an absolute URL to puma. For example /usr/local/bin/puma
|
43
|
+
# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
|
44
|
+
ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
|
45
|
+
|
46
|
+
# Variant: Rails start.
|
47
|
+
# ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
|
48
|
+
|
49
|
+
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
|
50
|
+
# Variant: Specify directives inline.
|
51
|
+
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
|
52
|
+
|
53
|
+
|
54
|
+
Restart=always
|
55
|
+
|
56
|
+
[Install]
|
57
|
+
WantedBy=multi-user.target
|
58
|
+
~~~~
|
59
|
+
|
60
|
+
See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
|
61
|
+
for additional details.
|
62
|
+
|
63
|
+
## Socket Activation
|
64
|
+
|
65
|
+
systemd and puma also support socket activation, where systemd opens
|
66
|
+
the listening socket(s) in advance and provides them to the puma
|
67
|
+
master process on startup. Among other advantages, this keeps
|
68
|
+
listening sockets open across puma restarts and achieves graceful
|
69
|
+
restarts, including when upgraded puma, and is compatible with both
|
70
|
+
clustered mode and application preload.
|
71
|
+
|
72
|
+
**Note:** Any wrapper scripts which `exec`, or other indirections in
|
73
|
+
`ExecStart`, may result in activated socket file descriptors being closed
|
74
|
+
before they reach the puma master process. For example, if using `bundle exec`,
|
75
|
+
pass the `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a
|
76
|
+
`puma` executable generated by `bundle binstubs puma`. This is tracked in
|
77
|
+
[#1499].
|
78
|
+
|
79
|
+
**Note:** Socket activation doesn't currently work on jruby. This is
|
80
|
+
tracked in [#1367].
|
81
|
+
|
82
|
+
To use socket activation, configure one or more `ListenStream` sockets
|
83
|
+
in a companion `*.socket` unit file. Also uncomment the associated
|
84
|
+
`Requires` directive for the socket unit in the service file (see
|
85
|
+
above.) Here is a sample puma.socket, matching the ports used in the
|
86
|
+
above puma.service:
|
87
|
+
|
88
|
+
~~~~ ini
|
89
|
+
[Unit]
|
90
|
+
Description=Puma HTTP Server Accept Sockets
|
91
|
+
|
92
|
+
[Socket]
|
93
|
+
ListenStream=0.0.0.0:9292
|
94
|
+
ListenStream=0.0.0.0:9293
|
95
|
+
|
96
|
+
# AF_UNIX domain socket
|
97
|
+
# SocketUser, SocketGroup, etc. may be needed for Unix domain sockets
|
98
|
+
# ListenStream=/run/puma.sock
|
99
|
+
|
100
|
+
# Socket options matching Puma defaults
|
101
|
+
NoDelay=true
|
102
|
+
ReusePort=true
|
103
|
+
Backlog=1024
|
104
|
+
|
105
|
+
[Install]
|
106
|
+
WantedBy=sockets.target
|
107
|
+
~~~~
|
108
|
+
|
109
|
+
See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
|
110
|
+
for additional configuration details.
|
111
|
+
|
112
|
+
Note that the above configurations will work with Puma in either
|
113
|
+
single process or cluster mode.
|
114
|
+
|
115
|
+
### Sockets and symlinks
|
116
|
+
|
117
|
+
When using releases folders, you should set the socket path using the
|
118
|
+
shared folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the
|
119
|
+
release folder path (`/srv/projet/releases/1234/tmp/puma.sock`).
|
120
|
+
|
121
|
+
Puma will detect the release path socket as different than the one provided by
|
122
|
+
systemd and attempt to bind it again, resulting in the exception
|
123
|
+
`There is already a server bound to:`.
|
124
|
+
|
125
|
+
## Usage
|
126
|
+
|
127
|
+
Without socket activation, use `systemctl` as root (e.g. via `sudo`) as
|
128
|
+
with other system services:
|
129
|
+
|
130
|
+
~~~~ sh
|
131
|
+
# After installing or making changes to puma.service
|
132
|
+
systemctl daemon-reload
|
133
|
+
|
134
|
+
# Enable so it starts on boot
|
135
|
+
systemctl enable puma.service
|
136
|
+
|
137
|
+
# Initial start up.
|
138
|
+
systemctl start puma.service
|
139
|
+
|
140
|
+
# Check status
|
141
|
+
systemctl status puma.service
|
142
|
+
|
143
|
+
# A normal restart. Warning: listeners sockets will be closed
|
144
|
+
# while a new puma process initializes.
|
145
|
+
systemctl restart puma.service
|
146
|
+
~~~~
|
147
|
+
|
148
|
+
With socket activation, several but not all of these commands should
|
149
|
+
be run for both socket and service:
|
150
|
+
|
151
|
+
~~~~ sh
|
152
|
+
# After installing or making changes to either puma.socket or
|
153
|
+
# puma.service.
|
154
|
+
systemctl daemon-reload
|
155
|
+
|
156
|
+
# Enable both socket and service so they start on boot. Alternatively
|
157
|
+
# you could leave puma.service disabled and systemd will start it on
|
158
|
+
# first use (with startup lag on first request)
|
159
|
+
systemctl enable puma.socket puma.service
|
160
|
+
|
161
|
+
# Initial start up. The Requires directive (see above) ensures the
|
162
|
+
# socket is started before the service.
|
163
|
+
systemctl start puma.socket puma.service
|
164
|
+
|
165
|
+
# Check status of both socket and service.
|
166
|
+
systemctl status puma.socket puma.service
|
167
|
+
|
168
|
+
# A "hot" restart, with systemd keeping puma.socket listening and
|
169
|
+
# providing to the new puma (master) instance.
|
170
|
+
systemctl restart puma.service
|
171
|
+
|
172
|
+
# A normal restart, needed to handle changes to
|
173
|
+
# puma.socket, such as changing the ListenStream ports. Note
|
174
|
+
# daemon-reload (above) should be run first.
|
175
|
+
systemctl restart puma.socket puma.service
|
176
|
+
~~~~
|
177
|
+
|
178
|
+
Here is sample output from `systemctl status` with both service and
|
179
|
+
socket running:
|
180
|
+
|
181
|
+
~~~~
|
182
|
+
● puma.socket - Puma HTTP Server Accept Sockets
|
183
|
+
Loaded: loaded (/etc/systemd/system/puma.socket; enabled; vendor preset: enabled)
|
184
|
+
Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
|
185
|
+
Listen: 0.0.0.0:9233 (Stream)
|
186
|
+
0.0.0.0:9234 (Stream)
|
187
|
+
|
188
|
+
Apr 07 08:40:19 hx systemd[874]: Listening on Puma HTTP Server Accept Sockets.
|
189
|
+
|
190
|
+
● puma.service - Puma HTTP Server
|
191
|
+
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
|
192
|
+
Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
|
193
|
+
Main PID: 28320 (ruby)
|
194
|
+
CGroup: /system.slice/puma.service
|
195
|
+
├─28320 puma 3.3.0 (tcp://0.0.0.0:9233,ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem) [app]
|
196
|
+
├─28323 puma: cluster worker 0: 28320 [app]
|
197
|
+
└─28327 puma: cluster worker 1: 28320 [app]
|
198
|
+
|
199
|
+
Apr 07 08:40:19 hx puma[28320]: Puma starting in cluster mode...
|
200
|
+
Apr 07 08:40:19 hx puma[28320]: * Version 3.3.0 (ruby 2.2.4-p230), codename: Jovial Platypus
|
201
|
+
Apr 07 08:40:19 hx puma[28320]: * Min threads: 0, max threads: 16
|
202
|
+
Apr 07 08:40:19 hx puma[28320]: * Environment: production
|
203
|
+
Apr 07 08:40:19 hx puma[28320]: * Process workers: 2
|
204
|
+
Apr 07 08:40:19 hx puma[28320]: * Phased restart available
|
205
|
+
Apr 07 08:40:19 hx puma[28320]: * Activated tcp://0.0.0.0:9233
|
206
|
+
Apr 07 08:40:19 hx puma[28320]: * Activated ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem
|
207
|
+
Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
|
208
|
+
~~~~
|
209
|
+
|
210
|
+
### capistrano3-puma
|
211
|
+
|
212
|
+
By default,
|
213
|
+
[capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
|
214
|
+
`pumactl` for deployment restarts, outside of systemd. To learn the
|
215
|
+
exact commands that this tool would use for `ExecStart` and
|
216
|
+
`ExecStop`, use the following `cap` commands in dry-run mode, and
|
217
|
+
update from the above forking service configuration accordingly. Note
|
218
|
+
also that the configured `User` should likely be the same as the
|
219
|
+
capistrano3-puma `:puma_user` option.
|
220
|
+
|
221
|
+
~~~~ sh
|
222
|
+
stage=production # or different stage, as needed
|
223
|
+
cap $stage puma:start --dry-run
|
224
|
+
cap $stage puma:stop --dry-run
|
225
|
+
~~~~
|
226
|
+
|
227
|
+
[Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
|
228
|
+
[#1367]: https://github.com/puma/puma/issues/1367
|