puma 5.3.2 → 5.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/History.md +211 -11
- data/README.md +47 -6
- data/docs/architecture.md +49 -16
- data/docs/compile_options.md +4 -2
- data/docs/deployment.md +53 -67
- data/docs/plugins.md +15 -15
- data/docs/rails_dev_mode.md +2 -3
- data/docs/restart.md +6 -6
- data/docs/signals.md +11 -10
- data/docs/stats.md +8 -8
- data/docs/systemd.md +64 -67
- data/ext/puma_http11/extconf.rb +34 -6
- data/ext/puma_http11/http11_parser.c +23 -10
- data/ext/puma_http11/http11_parser_common.rl +1 -1
- data/ext/puma_http11/mini_ssl.c +90 -12
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +49 -47
- data/ext/puma_http11/org/jruby/puma/MiniSSL.java +38 -55
- data/ext/puma_http11/puma_http11.c +1 -1
- data/lib/puma/app/status.rb +7 -4
- data/lib/puma/binder.rb +51 -6
- data/lib/puma/cli.rb +14 -4
- data/lib/puma/client.rb +143 -25
- data/lib/puma/cluster/worker.rb +8 -18
- data/lib/puma/cluster/worker_handle.rb +4 -0
- data/lib/puma/cluster.rb +30 -24
- data/lib/puma/configuration.rb +4 -1
- data/lib/puma/const.rb +9 -8
- data/lib/puma/control_cli.rb +19 -13
- data/lib/puma/detect.rb +8 -2
- data/lib/puma/dsl.rb +111 -13
- data/lib/puma/{json.rb → json_serialization.rb} +1 -1
- data/lib/puma/launcher.rb +15 -1
- data/lib/puma/minissl/context_builder.rb +8 -6
- data/lib/puma/minissl.rb +33 -27
- data/lib/puma/null_io.rb +5 -0
- data/lib/puma/plugin.rb +2 -2
- data/lib/puma/rack/builder.rb +1 -1
- data/lib/puma/request.rb +19 -10
- data/lib/puma/runner.rb +22 -8
- data/lib/puma/server.rb +37 -29
- data/lib/puma/state_file.rb +42 -7
- data/lib/puma/thread_pool.rb +7 -5
- data/lib/puma/util.rb +20 -4
- data/lib/puma.rb +6 -4
- data/lib/rack/version_restriction.rb +15 -0
- data/tools/Dockerfile +1 -1
- metadata +8 -7
data/docs/deployment.md
CHANGED
@@ -1,35 +1,32 @@
|
|
1
1
|
# Deployment engineering for Puma
|
2
2
|
|
3
|
-
Puma
|
4
|
-
|
5
|
-
it in their production deployments as well.
|
3
|
+
Puma expects to be run in a deployed environment eventually. You can use it as
|
4
|
+
your development server, but most people use it in their production deployments.
|
6
5
|
|
7
|
-
To that end, this
|
8
|
-
|
6
|
+
To that end, this document serves as a foundation of wisdom regarding deploying
|
7
|
+
Puma to production while increasing happiness and decreasing downtime.
|
9
8
|
|
10
9
|
## Specifying Puma
|
11
10
|
|
12
|
-
Most people
|
13
|
-
|
11
|
+
Most people will specify Puma by including `gem "puma"` in a Gemfile, so we'll
|
12
|
+
assume this is how you're using Puma.
|
14
13
|
|
15
|
-
|
14
|
+
## Single vs. Cluster mode
|
16
15
|
|
17
|
-
|
16
|
+
Initially, Puma was conceived as a thread-only web server, but support for
|
17
|
+
processes was added in version 2.
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
To run `puma` in single mode (i.e., as a development environment), set the
|
20
|
+
number of workers to 0; anything higher will run in cluster mode.
|
21
21
|
|
22
|
-
|
23
|
-
set the number of workers to 0, anything above will run in cluster mode.
|
24
|
-
|
25
|
-
Here are some rules of thumb for cluster mode:
|
22
|
+
Here are some tips for cluster mode:
|
26
23
|
|
27
24
|
### MRI
|
28
25
|
|
29
|
-
* Use cluster mode and set the number of workers to 1.5x the number of
|
30
|
-
in the machine, minimum 2.
|
31
|
-
* Set the number of threads to desired concurrent requests
|
32
|
-
Puma defaults to 5 and that's a decent number.
|
26
|
+
* Use cluster mode and set the number of workers to 1.5x the number of CPU cores
|
27
|
+
in the machine, starting from a minimum of 2.
|
28
|
+
* Set the number of threads to desired concurrent requests/number of workers.
|
29
|
+
Puma defaults to 5, and that's a decent number.
|
33
30
|
|
34
31
|
#### Migrating from Unicorn
|
35
32
|
|
@@ -37,7 +34,7 @@ Here are some rules of thumb for cluster mode:
|
|
37
34
|
* Set workers to half the number of unicorn workers you're using
|
38
35
|
* Set threads to 2
|
39
36
|
* Enjoy 50% memory savings
|
40
|
-
* As you grow more confident in the thread
|
37
|
+
* As you grow more confident in the thread-safety of your app, you can tune the
|
41
38
|
workers down and the threads up.
|
42
39
|
|
43
40
|
#### Ubuntu / Systemd (Systemctl) Installation
|
@@ -48,69 +45,58 @@ See [systemd.md](systemd.md)
|
|
48
45
|
|
49
46
|
**How do you know if you've got enough (or too many workers)?**
|
50
47
|
|
51
|
-
A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
|
52
|
-
But since so many apps are waiting on IO from DBs, etc., they can
|
53
|
-
|
48
|
+
A good question. Due to MRI's GIL, only one thread can be executing Ruby code at
|
49
|
+
a time. But since so many apps are waiting on IO from DBs, etc., they can
|
50
|
+
utilize threads to use the process more efficiently.
|
54
51
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
52
|
+
Generally, you never want processes that are pegged all the time. That can mean
|
53
|
+
there is more work to do than the process can get through. On the other hand, if
|
54
|
+
you have processes that sit around doing nothing, then they're just eating up
|
55
|
+
resources.
|
59
56
|
|
60
|
-
Watch your CPU utilization over time and aim for about 70% on average.
|
61
|
-
you've got capacity still but aren't starving threads.
|
57
|
+
Watch your CPU utilization over time and aim for about 70% on average. 70%
|
58
|
+
utilization means you've got capacity still but aren't starving threads.
|
62
59
|
|
63
60
|
**Measuring utilization**
|
64
61
|
|
65
|
-
Using a timestamp header from an upstream proxy server (
|
66
|
-
|
67
|
-
thread to become available.
|
62
|
+
Using a timestamp header from an upstream proxy server (e.g., `nginx` or
|
63
|
+
`haproxy`) makes it possible to indicate how long requests have been waiting for
|
64
|
+
a Puma thread to become available.
|
68
65
|
|
69
66
|
* Have your upstream proxy set a header with the time it received the request:
|
70
67
|
* nginx: `proxy_set_header X-Request-Start "${msec}";`
|
71
|
-
* haproxy >= 1.9: `http-request set-header X-Request-Start
|
68
|
+
* haproxy >= 1.9: `http-request set-header X-Request-Start
|
69
|
+
t=%[date()]%[date_us()]`
|
72
70
|
* haproxy < 1.9: `http-request set-header X-Request-Start t=%[date()]`
|
73
|
-
* In your Rack middleware, determine the amount of time elapsed since
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
*
|
78
|
-
|
71
|
+
* In your Rack middleware, determine the amount of time elapsed since
|
72
|
+
`X-Request-Start`.
|
73
|
+
* To improve accuracy, you will want to subtract time spent waiting for slow
|
74
|
+
clients:
|
75
|
+
* `env['puma.request_body_wait']` contains the number of milliseconds Puma
|
76
|
+
spent waiting for the client to send the request body.
|
77
|
+
* haproxy: `%Th` (TLS handshake time) and `%Ti` (idle time before request)
|
78
|
+
can can also be added as headers.
|
79
79
|
|
80
80
|
## Should I daemonize?
|
81
81
|
|
82
|
-
|
82
|
+
The Puma 5.0 release removed daemonization. For older versions and alternatives,
|
83
|
+
continue reading.
|
83
84
|
|
84
|
-
I prefer to
|
85
|
-
monitor them as child processes. This gives them fast response to crashes and
|
85
|
+
I prefer not to daemonize my servers and use something like `runit` or `systemd`
|
86
|
+
to monitor them as child processes. This gives them fast response to crashes and
|
86
87
|
makes it easy to figure out what is going on. Additionally, unlike `unicorn`,
|
87
|
-
|
88
|
+
Puma does not require daemonization to do zero-downtime restarts.
|
88
89
|
|
89
|
-
I see people using daemonization because they start puma directly via
|
90
|
-
task and thus want it to live on past the `cap deploy`. To these people I say:
|
91
|
-
You need to be using a process monitor. Nothing is making sure
|
92
|
-
this scenario! You're just waiting for something weird to happen,
|
93
|
-
and to get paged at
|
94
|
-
your OS comes with, be it `sysvinit` or `systemd`. Or branch out
|
95
|
-
|
90
|
+
I see people using daemonization because they start puma directly via Capistrano
|
91
|
+
task and thus want it to live on past the `cap deploy`. To these people, I say:
|
92
|
+
You need to be using a process monitor. Nothing is making sure Puma stays up in
|
93
|
+
this scenario! You're just waiting for something weird to happen, Puma to die,
|
94
|
+
and to get paged at 3 AM. Do yourself a favor, at least the process monitoring
|
95
|
+
your OS comes with, be it `sysvinit` or `systemd`. Or branch out and use `runit`
|
96
|
+
or hell, even `monit`.
|
96
97
|
|
97
98
|
## Restarting
|
98
99
|
|
99
100
|
You probably will want to deploy some new code at some point, and you'd like
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
1. Don't use `preload!`. This dirties the master process and means it will have
|
104
|
-
to shutdown all the workers and re-exec itself to get your new code. It is not compatible with phased-restart and `prune_bundler` as well.
|
105
|
-
|
106
|
-
1. Use `prune_bundler`. This makes it so that the cluster master will detach itself
|
107
|
-
from a Bundler context on start. This allows the cluster workers to load your app
|
108
|
-
and start a brand new Bundler context within the worker only. This means your
|
109
|
-
master remains pristine and can live on between new releases of your code.
|
110
|
-
|
111
|
-
1. Use phased-restart (`SIGUSR1` or `pumactl phased-restart`). This tells the master
|
112
|
-
to kill off one worker at a time and restart them in your new code. This minimizes
|
113
|
-
downtime and staggers the restart nicely. **WARNING** This means that both your
|
114
|
-
old code and your new code will be running concurrently. Most deployment solutions
|
115
|
-
already cause that, but it's worth warning you about it again. Be careful with your
|
116
|
-
migrations, etc!
|
101
|
+
Puma to start running that new code. There are a few options for restarting
|
102
|
+
Puma, described separately in our [restart documentation](restart.md).
|
data/docs/plugins.md
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
Puma 3.0 added support for plugins that can augment configuration and service
|
4
4
|
operations.
|
5
5
|
|
6
|
-
|
6
|
+
There are two canonical plugins to aid in the development of new plugins:
|
7
7
|
|
8
8
|
* [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb):
|
9
9
|
Restarts the server if the file `tmp/restart.txt` is touched
|
10
10
|
* [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb):
|
11
|
-
Packages up the default configuration used by
|
11
|
+
Packages up the default configuration used by Puma on Heroku (being sunset
|
12
|
+
with the release of Puma 5.0)
|
12
13
|
|
13
|
-
Plugins are activated in a
|
14
|
+
Plugins are activated in a Puma configuration file (such as `config/puma.rb'`)
|
14
15
|
by adding `plugin "name"`, such as `plugin "heroku"`.
|
15
16
|
|
16
|
-
Plugins are activated based
|
17
|
-
|
18
|
-
|
19
|
-
puma plugins).
|
17
|
+
Plugins are activated based on path requirements so, activating the `heroku`
|
18
|
+
plugin is much like `require "puma/plugin/heroku"`. This allows gems to provide
|
19
|
+
multiple plugins (as well as unrelated gems to provide Puma plugins).
|
20
20
|
|
21
|
-
The `tmp_restart` plugin
|
21
|
+
The `tmp_restart` plugin comes with Puma, so it is always available.
|
22
22
|
|
23
23
|
To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
|
24
24
|
|
@@ -26,13 +26,13 @@ To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
|
|
26
26
|
|
27
27
|
## Server-wide hooks
|
28
28
|
|
29
|
-
Plugins can use a couple of hooks at server level: `start` and `config`.
|
29
|
+
Plugins can use a couple of hooks at the server level: `start` and `config`.
|
30
30
|
|
31
|
-
`start` runs when the server has started and allows the plugin to
|
32
|
-
functionality to augment
|
31
|
+
`start` runs when the server has started and allows the plugin to initiate other
|
32
|
+
functionality to augment Puma.
|
33
33
|
|
34
|
-
`config` runs when the server is being configured and
|
35
|
-
object that
|
34
|
+
`config` runs when the server is being configured and receives a `Puma::DSL`
|
35
|
+
object that is useful for additional configuration.
|
36
36
|
|
37
|
-
|
38
|
-
|
37
|
+
Public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are treated as a
|
38
|
+
public API for plugins.
|
data/docs/rails_dev_mode.md
CHANGED
@@ -2,16 +2,15 @@
|
|
2
2
|
|
3
3
|
## "Loopback requests"
|
4
4
|
|
5
|
-
Be cautious of "loopback requests"
|
5
|
+
Be cautious of "loopback requests," where a Rails application executes a request to a server that, in turn, results in another request back to the same Rails application before the first request completes. Having a loopback request will trigger [Rails' load interlock](https://guides.rubyonrails.org/threading_and_code_execution.html#load-interlock) mechanism. The load interlock mechanism prevents a thread from using Rails autoloading mechanism to load constants while the application code is still running inside another thread.
|
6
6
|
|
7
7
|
This issue only occurs in the development environment as Rails' load interlock is not used in production environments. Although we're not sure, we believe this issue may not occur with the new `zeitwerk` code loader.
|
8
8
|
|
9
9
|
### Solutions
|
10
10
|
|
11
|
-
|
12
11
|
#### 1. Bypass Rails' load interlock with `.permit_concurrent_loads`
|
13
12
|
|
14
|
-
Wrap the first request inside a block that will allow concurrent loads
|
13
|
+
Wrap the first request inside a block that will allow concurrent loads: [`ActiveSupport::Dependencies.interlock.permit_concurrent_loads`](https://guides.rubyonrails.org/threading_and_code_execution.html#permit-concurrent-loads). Anything wrapped inside the `.permit_concurrent_loads` block will bypass the load interlock mechanism, allowing new threads to access the Rails environment and boot properly.
|
15
14
|
|
16
15
|
###### Example
|
17
16
|
|
data/docs/restart.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
Puma provides three distinct kinds of restart operations, each for different use cases.
|
1
|
+
Puma provides three distinct kinds of restart operations, each for different use cases. This document describes "hot restarts" and "phased restarts." The third kind of restart operation is called "refork" and is described in the documentation for [`fork_worker`](fork_worker.md).
|
2
2
|
|
3
3
|
## Hot restart
|
4
4
|
|
5
|
-
To perform a "hot" restart, Puma performs an `exec` operation to start the process up again, so no memory is shared between the old process and the new process. As a result, it is safe to issue a restart any place where you would manually stop Puma and start it again. In particular, it is safe to upgrade Puma itself using a hot restart.
|
5
|
+
To perform a "hot" restart, Puma performs an `exec` operation to start the process up again, so no memory is shared between the old process and the new process. As a result, it is safe to issue a restart at any place where you would manually stop Puma and start it again. In particular, it is safe to upgrade Puma itself using a hot restart.
|
6
6
|
|
7
7
|
If the new process is unable to load, it will simply exit. You should therefore run Puma under a process monitor when using it in production.
|
8
8
|
|
@@ -16,14 +16,14 @@ Any of the following will cause a Puma server to perform a hot restart:
|
|
16
16
|
|
17
17
|
### Supported configurations
|
18
18
|
|
19
|
-
* Works in cluster mode and
|
19
|
+
* Works in cluster mode and single mode
|
20
20
|
* Supported on all platforms
|
21
21
|
|
22
22
|
### Client experience
|
23
23
|
|
24
|
-
* All platforms:
|
24
|
+
* All platforms: clients with an in-flight request are served responses before the connection is closed gracefully. Puma gracefully disconnects any idle HTTP persistent connections before restarting.
|
25
25
|
* On MRI or TruffleRuby on Linux and BSD: Clients who connect just before the server restarts may experience increased latency while the server stops and starts again, but their connections will not be closed prematurely.
|
26
|
-
* On Windows and
|
26
|
+
* On Windows and JRuby: Clients who connect just before a restart may experience "connection reset" errors.
|
27
27
|
|
28
28
|
### Additional notes
|
29
29
|
|
@@ -32,7 +32,7 @@ Any of the following will cause a Puma server to perform a hot restart:
|
|
32
32
|
|
33
33
|
## Phased restart
|
34
34
|
|
35
|
-
Phased restarts replace all running workers in a Puma cluster. This is a useful way to
|
35
|
+
Phased restarts replace all running workers in a Puma cluster. This is a useful way to upgrade the application that Puma is serving gracefully. A phased restart works by first killing an old worker, then starting a new worker, waiting until the new worker has successfully started before proceeding to the next worker. This process continues until all workers are replaced. The master process is not restarted.
|
36
36
|
|
37
37
|
### How-to
|
38
38
|
|
data/docs/signals.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
The [unix signal](https://en.wikipedia.org/wiki/Unix_signal) is a method of sending messages between [processes](https://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.
|
1
|
+
The [unix signal](https://en.wikipedia.org/wiki/Unix_signal) is a method of sending messages between [processes](https://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
2
|
|
3
3
|
## Sending Signals
|
4
4
|
|
5
|
-
If you are new to signals it can be
|
5
|
+
If you are new to signals, it can be helpful to see how they are used. When a process starts in a *nix-like operating system, it will have a [PID - or process identifier](https://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
6
|
|
7
7
|
```sh
|
8
8
|
$ echo "foo" >> my.log
|
@@ -10,7 +10,7 @@ $ irb
|
|
10
10
|
> pid = Process.spawn 'tail -f my.log'
|
11
11
|
```
|
12
12
|
|
13
|
-
From here we can see that the tail process is running by using the `ps` command:
|
13
|
+
From here, we can see that the tail process is running by using the `ps` command:
|
14
14
|
|
15
15
|
```sh
|
16
16
|
$ ps aux | grep tail
|
@@ -27,7 +27,7 @@ Process.detach(pid) # https://ruby-doc.org/core-2.1.1/Process.html#method-c-deta
|
|
27
27
|
Process.kill("TERM", pid)
|
28
28
|
```
|
29
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
|
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 example, `SIGTERM` is equivalent to sending `TERM` via `Process.kill`.
|
31
31
|
|
32
32
|
## Puma Signals
|
33
33
|
|
@@ -35,13 +35,14 @@ Puma cluster responds to these signals:
|
|
35
35
|
|
36
36
|
- `TTIN` increment the worker count by 1
|
37
37
|
- `TTOU` decrement the worker count by 1
|
38
|
-
- `TERM` send `TERM` to worker.
|
39
|
-
- `USR2` restart workers. This also reloads
|
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.
|
38
|
+
- `TERM` send `TERM` to worker. The worker will attempt to finish then exit.
|
39
|
+
- `USR2` restart workers. This also reloads the Puma configuration file, if there is one.
|
40
|
+
- `USR1` restart workers in phases, a rolling restart. This will not reload the 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. Puma will attempt to finish then exit.
|
43
43
|
- `CHLD`
|
44
|
-
- `URG ` refork workers in phases from worker 0
|
44
|
+
- `URG ` refork workers in phases from worker 0 if `fork_workers` option is enabled.
|
45
|
+
- `INFO` print backtraces of all puma threads
|
45
46
|
|
46
47
|
## Callbacks order in case of different signals
|
47
48
|
|
data/docs/stats.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
##
|
1
|
+
## Accessing stats
|
2
2
|
|
3
3
|
Stats can be accessed in two ways:
|
4
4
|
|
@@ -47,18 +47,18 @@ end
|
|
47
47
|
|
48
48
|
## Explanation of stats
|
49
49
|
|
50
|
-
`Puma.stats` returns different information and a different structure depending on if Puma is in single vs cluster mode. There is one top-level attribute that is common to both modes:
|
50
|
+
`Puma.stats` returns different information and a different structure depending on if Puma is in single vs. cluster mode. There is one top-level attribute that is common to both modes:
|
51
51
|
|
52
|
-
* started_at: when
|
52
|
+
* started_at: when Puma was started
|
53
53
|
|
54
54
|
### single mode and individual workers in cluster mode
|
55
55
|
|
56
|
-
When Puma
|
56
|
+
When Puma runs in single mode, these stats are available at the top level. When Puma runs in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes where one hash represents each worker.
|
57
57
|
|
58
58
|
* backlog: requests that are waiting for an available thread to be available. if this is above 0, you need more capacity [always true?]
|
59
59
|
* running: how many threads are running
|
60
|
-
* pool_capacity: the number of requests that the server is capable of taking right now. For example if the number is 5 then it means there are 5 threads sitting idle ready to take a request. If one request comes in, then the value would be 4 until it finishes processing. If the minimum threads allowed is zero, this number will still have a maximum value of the maximum threads allowed.
|
61
|
-
* max_threads: the maximum number of threads
|
60
|
+
* pool_capacity: the number of requests that the server is capable of taking right now. For example, if the number is 5, then it means there are 5 threads sitting idle ready to take a request. If one request comes in, then the value would be 4 until it finishes processing. If the minimum threads allowed is zero, this number will still have a maximum value of the maximum threads allowed.
|
61
|
+
* max_threads: the maximum number of threads Puma is configured to spool per worker
|
62
62
|
* requests_count: the number of requests this worker has served since starting
|
63
63
|
|
64
64
|
|
@@ -72,9 +72,9 @@ When Puma is run in single mode, these stats are available at the top level. Whe
|
|
72
72
|
|
73
73
|
### worker status
|
74
74
|
|
75
|
-
* started_at: when the worker
|
75
|
+
* started_at: when the worker started
|
76
76
|
* pid: the process id of the worker process
|
77
|
-
* index: each worker gets a number. if
|
77
|
+
* index: each worker gets a number. if Puma is configured to have 3 workers, then this will be 0, 1, or 2
|
78
78
|
* booted: if it's done booting [?]
|
79
79
|
* last_checkin: Last time the worker responded to the master process' heartbeat check.
|
80
80
|
* last_status: a hash of info about the worker's state handling requests. See the explanation for this in "single mode and individual workers in cluster mode" section above.
|
data/docs/systemd.md
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
# systemd
|
2
2
|
|
3
|
-
[systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
[systemd](https://www.freedesktop.org/wiki/Software/systemd/) is a commonly
|
4
|
+
available init system (PID 1) on many Linux distributions. It offers process
|
5
|
+
monitoring (including automatic restarts) and other useful features for running
|
6
|
+
Puma in production.
|
7
7
|
|
8
8
|
## Service Configuration
|
9
9
|
|
10
|
-
Below is a sample puma.service configuration file for systemd, which
|
11
|
-
|
12
|
-
|
10
|
+
Below is a sample puma.service configuration file for systemd, which can be
|
11
|
+
copied or symlinked to `/etc/systemd/system/puma.service`, or if desired, using
|
12
|
+
an application or instance-specific name.
|
13
13
|
|
14
|
-
Note that this uses the systemd preferred "simple" type where the
|
15
|
-
|
16
|
-
exit).
|
14
|
+
Note that this uses the systemd preferred "simple" type where the start command
|
15
|
+
remains running in the foreground (does not fork and exit).
|
17
16
|
|
18
17
|
~~~~ ini
|
19
18
|
[Unit]
|
@@ -37,8 +36,8 @@ WatchdogSec=10
|
|
37
36
|
# Preferably configure a non-privileged user
|
38
37
|
# User=
|
39
38
|
|
40
|
-
# The path to
|
41
|
-
# Also replace the "<YOUR_APP_PATH>"
|
39
|
+
# The path to your application code root directory.
|
40
|
+
# Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
|
42
41
|
# Example /home/username/myapp
|
43
42
|
WorkingDirectory=<YOUR_APP_PATH>
|
44
43
|
|
@@ -64,33 +63,31 @@ Restart=always
|
|
64
63
|
WantedBy=multi-user.target
|
65
64
|
~~~~
|
66
65
|
|
67
|
-
See
|
66
|
+
See
|
67
|
+
[systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
|
68
68
|
for additional details.
|
69
69
|
|
70
70
|
## Socket Activation
|
71
71
|
|
72
|
-
systemd and
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
`Requires` directive for the socket unit in the service file (see
|
92
|
-
above.) Here is a sample puma.socket, matching the ports used in the
|
93
|
-
above puma.service:
|
72
|
+
systemd and Puma also support socket activation, where systemd opens the
|
73
|
+
listening socket(s) in advance and provides them to the Puma master process on
|
74
|
+
startup. Among other advantages, this keeps listening sockets open across puma
|
75
|
+
restarts and achieves graceful restarts, including when upgraded Puma, and is
|
76
|
+
compatible with both clustered mode and application preload.
|
77
|
+
|
78
|
+
**Note:** Any wrapper scripts which `exec`, or other indirections in `ExecStart`
|
79
|
+
may result in activated socket file descriptors being closed before reaching the
|
80
|
+
puma master process. For example, if using `bundle exec`, pass the
|
81
|
+
`--keep-file-descriptors` flag. `bundle exec` can be avoided by using a `puma`
|
82
|
+
executable generated by `bundle binstubs puma`. This is tracked in [#1499].
|
83
|
+
|
84
|
+
**Note:** Socket activation doesn't currently work on JRuby. This is tracked in
|
85
|
+
[#1367].
|
86
|
+
|
87
|
+
Configure one or more `ListenStream` sockets in a companion `*.socket` unit file
|
88
|
+
to use socket activation. Also, uncomment the associated `Requires` directive
|
89
|
+
for the socket unit in the service file (see above.) Here is a sample
|
90
|
+
puma.socket, matching the ports used in the above puma.service:
|
94
91
|
|
95
92
|
~~~~ ini
|
96
93
|
[Unit]
|
@@ -113,31 +110,32 @@ Backlog=1024
|
|
113
110
|
WantedBy=sockets.target
|
114
111
|
~~~~
|
115
112
|
|
116
|
-
See
|
113
|
+
See
|
114
|
+
[systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
|
117
115
|
for additional configuration details.
|
118
116
|
|
119
|
-
Note that the above configurations will work with Puma in either
|
120
|
-
|
117
|
+
Note that the above configurations will work with Puma in either single process
|
118
|
+
or cluster mode.
|
121
119
|
|
122
120
|
### Sockets and symlinks
|
123
121
|
|
124
|
-
When using releases folders, you should set the socket path using the
|
125
|
-
|
126
|
-
|
122
|
+
When using releases folders, you should set the socket path using the shared
|
123
|
+
folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the release folder
|
124
|
+
path (`/srv/projet/releases/1234/tmp/puma.sock`).
|
127
125
|
|
128
126
|
Puma will detect the release path socket as different than the one provided by
|
129
|
-
systemd and attempt to bind it again, resulting in the exception
|
130
|
-
|
127
|
+
systemd and attempt to bind it again, resulting in the exception `There is
|
128
|
+
already a server bound to:`.
|
131
129
|
|
132
130
|
### Binding
|
133
131
|
|
134
|
-
By default you need to configure
|
132
|
+
By default, you need to configure Puma to have binds matching with all
|
135
133
|
ListenStream statements. Any mismatched systemd ListenStreams will be closed by
|
136
|
-
|
134
|
+
Puma.
|
137
135
|
|
138
136
|
To automatically bind to all activated sockets, the option
|
139
137
|
`--bind-to-activated-sockets` can be used. This matches the config DSL
|
140
|
-
`bind_to_activated_sockets` statement. This will cause
|
138
|
+
`bind_to_activated_sockets` statement. This will cause Puma to create a bind
|
141
139
|
automatically for any activated socket. When systemd socket activation is not
|
142
140
|
enabled, this option does nothing.
|
143
141
|
|
@@ -146,8 +144,8 @@ binds that's not socket activated.
|
|
146
144
|
|
147
145
|
## Usage
|
148
146
|
|
149
|
-
Without socket activation, use `systemctl` as root (e
|
150
|
-
|
147
|
+
Without socket activation, use `systemctl` as root (i.e., via `sudo`) as with
|
148
|
+
other system services:
|
151
149
|
|
152
150
|
~~~~ sh
|
153
151
|
# After installing or making changes to puma.service
|
@@ -156,35 +154,35 @@ systemctl daemon-reload
|
|
156
154
|
# Enable so it starts on boot
|
157
155
|
systemctl enable puma.service
|
158
156
|
|
159
|
-
# Initial
|
157
|
+
# Initial startup.
|
160
158
|
systemctl start puma.service
|
161
159
|
|
162
160
|
# Check status
|
163
161
|
systemctl status puma.service
|
164
162
|
|
165
|
-
# A normal restart. Warning:
|
163
|
+
# A normal restart. Warning: listener's sockets will be closed
|
166
164
|
# while a new puma process initializes.
|
167
165
|
systemctl restart puma.service
|
168
166
|
~~~~
|
169
167
|
|
170
|
-
With socket activation, several but not all of these commands should
|
171
|
-
|
168
|
+
With socket activation, several but not all of these commands should be run for
|
169
|
+
both socket and service:
|
172
170
|
|
173
171
|
~~~~ sh
|
174
172
|
# After installing or making changes to either puma.socket or
|
175
173
|
# puma.service.
|
176
174
|
systemctl daemon-reload
|
177
175
|
|
178
|
-
# Enable both socket and service so they start on boot. Alternatively
|
179
|
-
# you could leave puma.service disabled and systemd will start it on
|
180
|
-
# first use (with startup lag on first request)
|
176
|
+
# Enable both socket and service, so they start on boot. Alternatively
|
177
|
+
# you could leave puma.service disabled, and systemd will start it on
|
178
|
+
# the first use (with startup lag on the first request)
|
181
179
|
systemctl enable puma.socket puma.service
|
182
180
|
|
183
|
-
# Initial
|
181
|
+
# Initial startup. The Requires directive (see above) ensures the
|
184
182
|
# socket is started before the service.
|
185
183
|
systemctl start puma.socket puma.service
|
186
184
|
|
187
|
-
# Check status of both socket and service.
|
185
|
+
# Check the status of both socket and service.
|
188
186
|
systemctl status puma.socket puma.service
|
189
187
|
|
190
188
|
# A "hot" restart, with systemd keeping puma.socket listening and
|
@@ -197,8 +195,8 @@ systemctl restart puma.service
|
|
197
195
|
systemctl restart puma.socket puma.service
|
198
196
|
~~~~
|
199
197
|
|
200
|
-
Here is sample output from `systemctl status` with both service and
|
201
|
-
|
198
|
+
Here is sample output from `systemctl status` with both service and socket
|
199
|
+
running:
|
202
200
|
|
203
201
|
~~~~
|
204
202
|
● puma.socket - Puma HTTP Server Accept Sockets
|
@@ -231,14 +229,12 @@ Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
|
|
231
229
|
|
232
230
|
### capistrano3-puma
|
233
231
|
|
234
|
-
By default,
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
also that the configured `User` should likely be the same as the
|
241
|
-
capistrano3-puma `:puma_user` option.
|
232
|
+
By default, [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
|
233
|
+
`pumactl` for deployment restarts outside of systemd. To learn the exact
|
234
|
+
commands that this tool would use for `ExecStart` and `ExecStop`, use the
|
235
|
+
following `cap` commands in dry-run mode, and update from the above forking
|
236
|
+
service configuration accordingly. Note also that the configured `User` should
|
237
|
+
likely be the same as the capistrano3-puma `:puma_user` option.
|
242
238
|
|
243
239
|
~~~~ sh
|
244
240
|
stage=production # or different stage, as needed
|
@@ -248,3 +244,4 @@ cap $stage puma:stop --dry-run
|
|
248
244
|
|
249
245
|
[Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
|
250
246
|
[#1367]: https://github.com/puma/puma/issues/1367
|
247
|
+
[#1499]: https://github.com/puma/puma/issues/1499
|