gitlab-puma 4.3.1.gitlab.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.md +1537 -0
- data/LICENSE +26 -0
- data/README.md +291 -0
- data/bin/puma +10 -0
- data/bin/puma-wild +31 -0
- data/bin/pumactl +12 -0
- data/docs/architecture.md +37 -0
- data/docs/deployment.md +111 -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/nginx.md +80 -0
- data/docs/plugins.md +38 -0
- data/docs/restart.md +41 -0
- data/docs/signals.md +96 -0
- data/docs/systemd.md +290 -0
- data/docs/tcp_mode.md +96 -0
- data/ext/puma_http11/PumaHttp11Service.java +19 -0
- data/ext/puma_http11/ext_help.h +15 -0
- data/ext/puma_http11/extconf.rb +28 -0
- data/ext/puma_http11/http11_parser.c +1044 -0
- data/ext/puma_http11/http11_parser.h +65 -0
- data/ext/puma_http11/http11_parser.java.rl +145 -0
- data/ext/puma_http11/http11_parser.rl +147 -0
- data/ext/puma_http11/http11_parser_common.rl +54 -0
- data/ext/puma_http11/io_buffer.c +155 -0
- data/ext/puma_http11/mini_ssl.c +553 -0
- data/ext/puma_http11/org/jruby/puma/Http11.java +226 -0
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +455 -0
- data/ext/puma_http11/org/jruby/puma/IOBuffer.java +72 -0
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +363 -0
- data/ext/puma_http11/puma_http11.c +502 -0
- data/lib/puma.rb +31 -0
- data/lib/puma/accept_nonblock.rb +29 -0
- data/lib/puma/app/status.rb +80 -0
- data/lib/puma/binder.rb +385 -0
- data/lib/puma/cli.rb +239 -0
- data/lib/puma/client.rb +494 -0
- data/lib/puma/cluster.rb +554 -0
- data/lib/puma/commonlogger.rb +108 -0
- data/lib/puma/configuration.rb +362 -0
- data/lib/puma/const.rb +242 -0
- data/lib/puma/control_cli.rb +289 -0
- data/lib/puma/detect.rb +15 -0
- data/lib/puma/dsl.rb +740 -0
- data/lib/puma/events.rb +156 -0
- data/lib/puma/io_buffer.rb +4 -0
- data/lib/puma/jruby_restart.rb +84 -0
- data/lib/puma/launcher.rb +475 -0
- data/lib/puma/minissl.rb +278 -0
- data/lib/puma/minissl/context_builder.rb +76 -0
- data/lib/puma/null_io.rb +44 -0
- data/lib/puma/plugin.rb +120 -0
- data/lib/puma/plugin/tmp_restart.rb +36 -0
- data/lib/puma/rack/builder.rb +301 -0
- data/lib/puma/rack/urlmap.rb +93 -0
- data/lib/puma/rack_default.rb +9 -0
- data/lib/puma/reactor.rb +400 -0
- data/lib/puma/runner.rb +192 -0
- data/lib/puma/server.rb +1053 -0
- data/lib/puma/single.rb +123 -0
- data/lib/puma/state_file.rb +31 -0
- data/lib/puma/tcp_logger.rb +41 -0
- data/lib/puma/thread_pool.rb +348 -0
- data/lib/puma/util.rb +124 -0
- data/lib/rack/handler/puma.rb +115 -0
- data/tools/docker/Dockerfile +16 -0
- data/tools/jungle/README.md +19 -0
- data/tools/jungle/init.d/README.md +61 -0
- data/tools/jungle/init.d/puma +421 -0
- data/tools/jungle/init.d/run-puma +18 -0
- data/tools/jungle/rc.d/README.md +74 -0
- data/tools/jungle/rc.d/puma +61 -0
- data/tools/jungle/rc.d/puma.conf +10 -0
- data/tools/jungle/upstart/README.md +61 -0
- data/tools/jungle/upstart/puma-manager.conf +31 -0
- data/tools/jungle/upstart/puma.conf +69 -0
- data/tools/trickletest.rb +44 -0
- metadata +147 -0
Binary file
|
Binary file
|
Binary file
|
data/docs/nginx.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Nginx configuration example file
|
2
|
+
|
3
|
+
This is a very common setup using an upstream. It was adapted from some Capistrano recipe I found on the Internet a while ago.
|
4
|
+
|
5
|
+
```
|
6
|
+
upstream myapp {
|
7
|
+
server unix:///myapp/tmp/puma.sock;
|
8
|
+
}
|
9
|
+
|
10
|
+
server {
|
11
|
+
listen 80;
|
12
|
+
server_name myapp.com;
|
13
|
+
|
14
|
+
# ~2 seconds is often enough for most folks to parse HTML/CSS and
|
15
|
+
# retrieve needed images/icons/frames, connections are cheap in
|
16
|
+
# nginx so increasing this is generally safe...
|
17
|
+
keepalive_timeout 5;
|
18
|
+
|
19
|
+
# path for static files
|
20
|
+
root /myapp/public;
|
21
|
+
access_log /myapp/log/nginx.access.log;
|
22
|
+
error_log /myapp/log/nginx.error.log info;
|
23
|
+
|
24
|
+
# this rewrites all the requests to the maintenance.html
|
25
|
+
# page if it exists in the doc root. This is for capistrano's
|
26
|
+
# disable web task
|
27
|
+
if (-f $document_root/maintenance.html) {
|
28
|
+
rewrite ^(.*)$ /maintenance.html last;
|
29
|
+
break;
|
30
|
+
}
|
31
|
+
|
32
|
+
location / {
|
33
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
34
|
+
proxy_set_header Host $http_host;
|
35
|
+
|
36
|
+
# If the file exists as a static file serve it directly without
|
37
|
+
# running all the other rewrite tests on it
|
38
|
+
if (-f $request_filename) {
|
39
|
+
break;
|
40
|
+
}
|
41
|
+
|
42
|
+
# check for index.html for directory index
|
43
|
+
# if it's there on the filesystem then rewrite
|
44
|
+
# the url to add /index.html to the end of it
|
45
|
+
# and then break to send it to the next config rules.
|
46
|
+
if (-f $request_filename/index.html) {
|
47
|
+
rewrite (.*) $1/index.html break;
|
48
|
+
}
|
49
|
+
|
50
|
+
# this is the meat of the rack page caching config
|
51
|
+
# it adds .html to the end of the url and then checks
|
52
|
+
# the filesystem for that file. If it exists, then we
|
53
|
+
# rewrite the url to have explicit .html on the end
|
54
|
+
# and then send it on its way to the next config rule.
|
55
|
+
# if there is no file on the fs then it sets all the
|
56
|
+
# necessary headers and proxies to our upstream pumas
|
57
|
+
if (-f $request_filename.html) {
|
58
|
+
rewrite (.*) $1.html break;
|
59
|
+
}
|
60
|
+
|
61
|
+
if (!-f $request_filename) {
|
62
|
+
proxy_pass http://myapp;
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
|
68
|
+
# BUT there's a chance it could break the ajax calls.
|
69
|
+
location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
|
70
|
+
expires max;
|
71
|
+
break;
|
72
|
+
}
|
73
|
+
|
74
|
+
# Error pages
|
75
|
+
# error_page 500 502 503 504 /500.html;
|
76
|
+
location = /500.html {
|
77
|
+
root /myapp/current/public;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
```
|
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,96 @@
|
|
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
|
+
|
45
|
+
## Callbacks order in case of different signals
|
46
|
+
|
47
|
+
### Start application
|
48
|
+
|
49
|
+
```
|
50
|
+
puma configuration file reloaded, if there is one
|
51
|
+
* Pruning Bundler environment
|
52
|
+
puma configuration file reloaded, if there is one
|
53
|
+
|
54
|
+
before_fork
|
55
|
+
on_worker_fork
|
56
|
+
after_worker_fork
|
57
|
+
|
58
|
+
Gemfile in context
|
59
|
+
|
60
|
+
on_worker_boot
|
61
|
+
|
62
|
+
Code of the app is loaded and running
|
63
|
+
```
|
64
|
+
|
65
|
+
### Send USR2
|
66
|
+
|
67
|
+
```
|
68
|
+
on_worker_shutdown
|
69
|
+
on_restart
|
70
|
+
|
71
|
+
puma configuration file reloaded, if there is one
|
72
|
+
|
73
|
+
before_fork
|
74
|
+
on_worker_fork
|
75
|
+
after_worker_fork
|
76
|
+
|
77
|
+
Gemfile in context
|
78
|
+
|
79
|
+
on_worker_boot
|
80
|
+
|
81
|
+
Code of the app is loaded and running
|
82
|
+
```
|
83
|
+
|
84
|
+
### Send USR1
|
85
|
+
|
86
|
+
```
|
87
|
+
on_worker_shutdown
|
88
|
+
on_worker_fork
|
89
|
+
after_worker_fork
|
90
|
+
|
91
|
+
Gemfile in context
|
92
|
+
|
93
|
+
on_worker_boot
|
94
|
+
|
95
|
+
Code of the app is loaded and running
|
96
|
+
```
|
data/docs/systemd.md
ADDED
@@ -0,0 +1,290 @@
|
|
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). See also, the
|
17
|
+
[Alternative Forking Configuration](#alternative-forking-configuration)
|
18
|
+
below.
|
19
|
+
|
20
|
+
~~~~ ini
|
21
|
+
[Unit]
|
22
|
+
Description=Puma HTTP Server
|
23
|
+
After=network.target
|
24
|
+
|
25
|
+
# Uncomment for socket activation (see below)
|
26
|
+
# Requires=puma.socket
|
27
|
+
|
28
|
+
[Service]
|
29
|
+
# Foreground process (do not use --daemon in ExecStart or config.rb)
|
30
|
+
Type=simple
|
31
|
+
|
32
|
+
# Preferably configure a non-privileged user
|
33
|
+
# User=
|
34
|
+
|
35
|
+
# The path to the your application code root directory.
|
36
|
+
# Also replace the "<YOUR_APP_PATH>" place holders below with this path.
|
37
|
+
# Example /home/username/myapp
|
38
|
+
WorkingDirectory=<YOUR_APP_PATH>
|
39
|
+
|
40
|
+
# Helpful for debugging socket activation, etc.
|
41
|
+
# Environment=PUMA_DEBUG=1
|
42
|
+
|
43
|
+
# SystemD will not run puma even if it is in your path. You must specify
|
44
|
+
# an absolute URL to puma. For example /usr/local/bin/puma
|
45
|
+
# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
|
46
|
+
ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
|
47
|
+
|
48
|
+
# Variant: Rails start.
|
49
|
+
# ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
|
50
|
+
|
51
|
+
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
|
52
|
+
# Variant: Specify directives inline.
|
53
|
+
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
|
54
|
+
|
55
|
+
|
56
|
+
Restart=always
|
57
|
+
|
58
|
+
[Install]
|
59
|
+
WantedBy=multi-user.target
|
60
|
+
~~~~
|
61
|
+
|
62
|
+
See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
|
63
|
+
for additional details.
|
64
|
+
|
65
|
+
## Socket Activation
|
66
|
+
|
67
|
+
systemd and puma also support socket activation, where systemd opens
|
68
|
+
the listening socket(s) in advance and provides them to the puma
|
69
|
+
master process on startup. Among other advantages, this keeps
|
70
|
+
listening sockets open across puma restarts and achieves graceful
|
71
|
+
restarts, including when upgraded puma, and is compatible with both
|
72
|
+
clustered mode and application preload.
|
73
|
+
|
74
|
+
**Note:** Any wrapper scripts which `exec`, or other indirections in
|
75
|
+
`ExecStart`, may result in activated socket file descriptors being closed
|
76
|
+
before they reach the puma master process. For example, if using `bundle exec`,
|
77
|
+
pass the `--keep-file-descriptors` flag. `bundle exec` can be avoided by using a
|
78
|
+
`puma` executable generated by `bundle binstubs puma`. This is tracked in
|
79
|
+
[#1499].
|
80
|
+
|
81
|
+
**Note:** Socket activation doesn't currently work on jruby. This is
|
82
|
+
tracked in [#1367].
|
83
|
+
|
84
|
+
To use socket activation, configure one or more `ListenStream` sockets
|
85
|
+
in a companion `*.socket` unit file. Also uncomment the associated
|
86
|
+
`Requires` directive for the socket unit in the service file (see
|
87
|
+
above.) Here is a sample puma.socket, matching the ports used in the
|
88
|
+
above puma.service:
|
89
|
+
|
90
|
+
~~~~ ini
|
91
|
+
[Unit]
|
92
|
+
Description=Puma HTTP Server Accept Sockets
|
93
|
+
|
94
|
+
[Socket]
|
95
|
+
ListenStream=0.0.0.0:9292
|
96
|
+
ListenStream=0.0.0.0:9293
|
97
|
+
|
98
|
+
# AF_UNIX domain socket
|
99
|
+
# SocketUser, SocketGroup, etc. may be needed for Unix domain sockets
|
100
|
+
# ListenStream=/run/puma.sock
|
101
|
+
|
102
|
+
# Socket options matching Puma defaults
|
103
|
+
NoDelay=true
|
104
|
+
ReusePort=true
|
105
|
+
Backlog=1024
|
106
|
+
|
107
|
+
[Install]
|
108
|
+
WantedBy=sockets.target
|
109
|
+
~~~~
|
110
|
+
|
111
|
+
See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
|
112
|
+
for additional configuration details.
|
113
|
+
|
114
|
+
Note that the above configurations will work with Puma in either
|
115
|
+
single process or cluster mode.
|
116
|
+
|
117
|
+
### Sockets and symlinks
|
118
|
+
|
119
|
+
When using releases folders, you should set the socket path using the
|
120
|
+
shared folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the
|
121
|
+
release folder path (`/srv/projet/releases/1234/tmp/puma.sock`).
|
122
|
+
|
123
|
+
Puma will detect the release path socket as different than the one provided by
|
124
|
+
systemd and attempt to bind it again, resulting in the exception
|
125
|
+
`There is already a server bound to:`.
|
126
|
+
|
127
|
+
## Usage
|
128
|
+
|
129
|
+
Without socket activation, use `systemctl` as root (e.g. via `sudo`) as
|
130
|
+
with other system services:
|
131
|
+
|
132
|
+
~~~~ sh
|
133
|
+
# After installing or making changes to puma.service
|
134
|
+
systemctl daemon-reload
|
135
|
+
|
136
|
+
# Enable so it starts on boot
|
137
|
+
systemctl enable puma.service
|
138
|
+
|
139
|
+
# Initial start up.
|
140
|
+
systemctl start puma.service
|
141
|
+
|
142
|
+
# Check status
|
143
|
+
systemctl status puma.service
|
144
|
+
|
145
|
+
# A normal restart. Warning: listeners sockets will be closed
|
146
|
+
# while a new puma process initializes.
|
147
|
+
systemctl restart puma.service
|
148
|
+
~~~~
|
149
|
+
|
150
|
+
With socket activation, several but not all of these commands should
|
151
|
+
be run for both socket and service:
|
152
|
+
|
153
|
+
~~~~ sh
|
154
|
+
# After installing or making changes to either puma.socket or
|
155
|
+
# puma.service.
|
156
|
+
systemctl daemon-reload
|
157
|
+
|
158
|
+
# Enable both socket and service so they start on boot. Alternatively
|
159
|
+
# you could leave puma.service disabled and systemd will start it on
|
160
|
+
# first use (with startup lag on first request)
|
161
|
+
systemctl enable puma.socket puma.service
|
162
|
+
|
163
|
+
# Initial start up. The Requires directive (see above) ensures the
|
164
|
+
# socket is started before the service.
|
165
|
+
systemctl start puma.socket puma.service
|
166
|
+
|
167
|
+
# Check status of both socket and service.
|
168
|
+
systemctl status puma.socket puma.service
|
169
|
+
|
170
|
+
# A "hot" restart, with systemd keeping puma.socket listening and
|
171
|
+
# providing to the new puma (master) instance.
|
172
|
+
systemctl restart puma.service
|
173
|
+
|
174
|
+
# A normal restart, needed to handle changes to
|
175
|
+
# puma.socket, such as changing the ListenStream ports. Note
|
176
|
+
# daemon-reload (above) should be run first.
|
177
|
+
systemctl restart puma.socket puma.service
|
178
|
+
~~~~
|
179
|
+
|
180
|
+
Here is sample output from `systemctl status` with both service and
|
181
|
+
socket running:
|
182
|
+
|
183
|
+
~~~~
|
184
|
+
● puma.socket - Puma HTTP Server Accept Sockets
|
185
|
+
Loaded: loaded (/etc/systemd/system/puma.socket; enabled; vendor preset: enabled)
|
186
|
+
Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
|
187
|
+
Listen: 0.0.0.0:9233 (Stream)
|
188
|
+
0.0.0.0:9234 (Stream)
|
189
|
+
|
190
|
+
Apr 07 08:40:19 hx systemd[874]: Listening on Puma HTTP Server Accept Sockets.
|
191
|
+
|
192
|
+
● puma.service - Puma HTTP Server
|
193
|
+
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
|
194
|
+
Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
|
195
|
+
Main PID: 28320 (ruby)
|
196
|
+
CGroup: /system.slice/puma.service
|
197
|
+
├─28320 puma 3.3.0 (tcp://0.0.0.0:9233,ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem) [app]
|
198
|
+
├─28323 puma: cluster worker 0: 28320 [app]
|
199
|
+
└─28327 puma: cluster worker 1: 28320 [app]
|
200
|
+
|
201
|
+
Apr 07 08:40:19 hx puma[28320]: Puma starting in cluster mode...
|
202
|
+
Apr 07 08:40:19 hx puma[28320]: * Version 3.3.0 (ruby 2.2.4-p230), codename: Jovial Platypus
|
203
|
+
Apr 07 08:40:19 hx puma[28320]: * Min threads: 0, max threads: 16
|
204
|
+
Apr 07 08:40:19 hx puma[28320]: * Environment: production
|
205
|
+
Apr 07 08:40:19 hx puma[28320]: * Process workers: 2
|
206
|
+
Apr 07 08:40:19 hx puma[28320]: * Phased restart available
|
207
|
+
Apr 07 08:40:19 hx puma[28320]: * Activated tcp://0.0.0.0:9233
|
208
|
+
Apr 07 08:40:19 hx puma[28320]: * Activated ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem
|
209
|
+
Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
|
210
|
+
~~~~
|
211
|
+
|
212
|
+
## Alternative Forking Configuration
|
213
|
+
|
214
|
+
Other systems/tools might expect or need puma to be run as a
|
215
|
+
"traditional" forking server, for example so that the `pumactl`
|
216
|
+
command can be used directly and outside of systemd for
|
217
|
+
stop/start/restart. This use case is incompatible with systemd socket
|
218
|
+
activation, so it should not be configured. Below is an alternative
|
219
|
+
puma.service config sample, using `Type=forking` and the `--daemon`
|
220
|
+
flag in `ExecStart`. Here systemd is playing a role more equivalent to
|
221
|
+
SysV init.d, where it is responsible for starting Puma on boot
|
222
|
+
(multi-user.target) and stopping it on shutdown, but is not performing
|
223
|
+
continuous restarts. Therefore running Puma in cluster mode, where the
|
224
|
+
master can restart workers, is highly recommended. See the systemd
|
225
|
+
[Restart] directive for details.
|
226
|
+
|
227
|
+
~~~~ ini
|
228
|
+
[Unit]
|
229
|
+
Description=Puma HTTP Forking Server
|
230
|
+
After=network.target
|
231
|
+
|
232
|
+
[Service]
|
233
|
+
# Background process configuration (use with --daemon in ExecStart)
|
234
|
+
Type=forking
|
235
|
+
|
236
|
+
# Preferably configure a non-privileged user
|
237
|
+
# User=
|
238
|
+
|
239
|
+
# The path to the puma application root
|
240
|
+
# Also replace the "<WD>" place holders below with this path.
|
241
|
+
WorkingDirectory=
|
242
|
+
|
243
|
+
# The command to start Puma
|
244
|
+
# (replace "<WD>" below)
|
245
|
+
ExecStart=bundle exec puma -C <WD>/shared/puma.rb --daemon
|
246
|
+
|
247
|
+
# The command to stop Puma
|
248
|
+
# (replace "<WD>" below)
|
249
|
+
ExecStop=bundle exec pumactl -S <WD>/shared/tmp/pids/puma.state stop
|
250
|
+
|
251
|
+
# Path to PID file so that systemd knows which is the master process
|
252
|
+
PIDFile=<WD>/shared/tmp/pids/puma.pid
|
253
|
+
|
254
|
+
# Should systemd restart puma?
|
255
|
+
# Use "no" (the default) to ensure no interference when using
|
256
|
+
# stop/start/restart via `pumactl`. The "on-failure" setting might
|
257
|
+
# work better for this purpose, but you must test it.
|
258
|
+
# Use "always" if only `systemctl` is used for start/stop/restart, and
|
259
|
+
# reconsider if you actually need the forking config.
|
260
|
+
Restart=no
|
261
|
+
|
262
|
+
# `puma_ctl restart` wouldn't work without this. It's because `pumactl`
|
263
|
+
# changes PID on restart and systemd stops the service afterwards
|
264
|
+
# because of the PID change. This option prevents stopping after PID
|
265
|
+
# change.
|
266
|
+
RemainAfterExit=yes
|
267
|
+
|
268
|
+
[Install]
|
269
|
+
WantedBy=multi-user.target
|
270
|
+
~~~~
|
271
|
+
|
272
|
+
### capistrano3-puma
|
273
|
+
|
274
|
+
By default,
|
275
|
+
[capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
|
276
|
+
`pumactl` for deployment restarts, outside of systemd. To learn the
|
277
|
+
exact commands that this tool would use for `ExecStart` and
|
278
|
+
`ExecStop`, use the following `cap` commands in dry-run mode, and
|
279
|
+
update from the above forking service configuration accordingly. Note
|
280
|
+
also that the configured `User` should likely be the same as the
|
281
|
+
capistrano3-puma `:puma_user` option.
|
282
|
+
|
283
|
+
~~~~ sh
|
284
|
+
stage=production # or different stage, as needed
|
285
|
+
cap $stage puma:start --dry-run
|
286
|
+
cap $stage puma:stop --dry-run
|
287
|
+
~~~~
|
288
|
+
|
289
|
+
[Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
|
290
|
+
[#1367]: https://github.com/puma/puma/issues/1367
|