puma 3.11.2 → 6.0.0

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.

Files changed (99) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +1708 -422
  3. data/LICENSE +23 -20
  4. data/README.md +190 -64
  5. data/bin/puma-wild +3 -9
  6. data/docs/architecture.md +59 -21
  7. data/docs/compile_options.md +55 -0
  8. data/docs/deployment.md +69 -58
  9. data/docs/fork_worker.md +31 -0
  10. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  11. data/docs/images/puma-connection-flow.png +0 -0
  12. data/docs/images/puma-general-arch.png +0 -0
  13. data/docs/jungle/README.md +9 -0
  14. data/docs/jungle/rc.d/README.md +74 -0
  15. data/docs/jungle/rc.d/puma +61 -0
  16. data/docs/jungle/rc.d/puma.conf +10 -0
  17. data/docs/kubernetes.md +66 -0
  18. data/docs/nginx.md +1 -1
  19. data/docs/plugins.md +22 -12
  20. data/docs/rails_dev_mode.md +28 -0
  21. data/docs/restart.md +47 -22
  22. data/docs/signals.md +13 -11
  23. data/docs/stats.md +142 -0
  24. data/docs/systemd.md +100 -115
  25. data/docs/testing_benchmarks_local_files.md +150 -0
  26. data/docs/testing_test_rackup_ci_files.md +36 -0
  27. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  28. data/ext/puma_http11/ext_help.h +1 -1
  29. data/ext/puma_http11/extconf.rb +61 -3
  30. data/ext/puma_http11/http11_parser.c +106 -118
  31. data/ext/puma_http11/http11_parser.h +2 -2
  32. data/ext/puma_http11/http11_parser.java.rl +22 -38
  33. data/ext/puma_http11/http11_parser.rl +6 -4
  34. data/ext/puma_http11/http11_parser_common.rl +6 -6
  35. data/ext/puma_http11/mini_ssl.c +376 -93
  36. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  37. data/ext/puma_http11/org/jruby/puma/Http11.java +108 -116
  38. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +84 -99
  39. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +250 -88
  40. data/ext/puma_http11/puma_http11.c +49 -57
  41. data/lib/puma/app/status.rb +71 -49
  42. data/lib/puma/binder.rb +243 -148
  43. data/lib/puma/cli.rb +50 -35
  44. data/lib/puma/client.rb +369 -232
  45. data/lib/puma/cluster/worker.rb +175 -0
  46. data/lib/puma/cluster/worker_handle.rb +97 -0
  47. data/lib/puma/cluster.rb +268 -235
  48. data/lib/puma/commonlogger.rb +4 -2
  49. data/lib/puma/configuration.rb +116 -88
  50. data/lib/puma/const.rb +49 -30
  51. data/lib/puma/control_cli.rb +124 -77
  52. data/lib/puma/detect.rb +33 -2
  53. data/lib/puma/dsl.rb +685 -138
  54. data/lib/puma/error_logger.rb +112 -0
  55. data/lib/puma/events.rb +17 -111
  56. data/lib/puma/io_buffer.rb +34 -5
  57. data/lib/puma/jruby_restart.rb +4 -59
  58. data/lib/puma/json_serialization.rb +96 -0
  59. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  60. data/lib/puma/launcher.rb +197 -130
  61. data/lib/puma/log_writer.rb +137 -0
  62. data/lib/puma/minissl/context_builder.rb +92 -0
  63. data/lib/puma/minissl.rb +256 -70
  64. data/lib/puma/null_io.rb +20 -1
  65. data/lib/puma/plugin/tmp_restart.rb +3 -1
  66. data/lib/puma/plugin.rb +9 -13
  67. data/lib/puma/rack/builder.rb +8 -9
  68. data/lib/puma/rack/urlmap.rb +2 -0
  69. data/lib/puma/rack_default.rb +3 -1
  70. data/lib/puma/reactor.rb +90 -187
  71. data/lib/puma/request.rb +607 -0
  72. data/lib/puma/runner.rb +94 -71
  73. data/lib/puma/server.rb +336 -703
  74. data/lib/puma/single.rb +27 -72
  75. data/lib/puma/state_file.rb +46 -7
  76. data/lib/puma/systemd.rb +47 -0
  77. data/lib/puma/thread_pool.rb +185 -91
  78. data/lib/puma/util.rb +23 -10
  79. data/lib/puma.rb +68 -3
  80. data/lib/rack/handler/puma.rb +17 -14
  81. data/tools/Dockerfile +16 -0
  82. data/tools/trickletest.rb +0 -1
  83. metadata +53 -30
  84. data/ext/puma_http11/io_buffer.c +0 -155
  85. data/lib/puma/accept_nonblock.rb +0 -23
  86. data/lib/puma/compat.rb +0 -14
  87. data/lib/puma/convenient.rb +0 -23
  88. data/lib/puma/daemon_ext.rb +0 -31
  89. data/lib/puma/delegation.rb +0 -11
  90. data/lib/puma/java_io_buffer.rb +0 -45
  91. data/lib/puma/rack/backports/uri/common_193.rb +0 -33
  92. data/lib/puma/tcp_logger.rb +0 -39
  93. data/tools/jungle/README.md +0 -13
  94. data/tools/jungle/init.d/README.md +0 -59
  95. data/tools/jungle/init.d/puma +0 -421
  96. data/tools/jungle/init.d/run-puma +0 -18
  97. data/tools/jungle/upstart/README.md +0 -61
  98. data/tools/jungle/upstart/puma-manager.conf +0 -31
  99. data/tools/jungle/upstart/puma.conf +0 -69
data/docs/restart.md CHANGED
@@ -1,39 +1,64 @@
1
- # Restarts
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
- To perform a restart, there are 3 builtin mechanisms:
3
+ ## Hot restart
4
4
 
5
- * Send the `puma` process the `SIGUSR2` signal
6
- * Send the `puma` process the `SIGUSR1` signal (rolling restart, cluster mode only)
7
- * Use the status server and issue `/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.
8
6
 
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.
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.
10
8
 
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.
9
+ ### How-to
12
10
 
13
- ### Normal vs Hot vs Phased Restart
11
+ Any of the following will cause a Puma server to perform a hot restart:
14
12
 
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.
13
+ * Send the `puma` process the `SIGUSR2` signal
14
+ * Issue a `GET` request to the Puma status/control server with the path `/restart`
15
+ * Issue `pumactl restart` (this uses the control server method if available, otherwise sends the `SIGUSR2` signal to the process)
16
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.
17
+ ### Supported configurations
18
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!).
19
+ * Works in cluster mode and single mode
20
+ * Supported on all platforms
20
21
 
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
+ ### Client experience
22
23
 
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 below).
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
+ * 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 JRuby: Clients who connect just before a restart may experience "connection reset" errors.
24
27
 
25
- ### Release Directory
28
+ ### Additional notes
26
29
 
27
- 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
+ * Only one version of the application is running at a time.
31
+ * `on_restart` is invoked just before the server shuts down. This can be used to clean up resources (like long-lived database connections) gracefully. Since Ruby 2.0, it is not typically necessary to explicitly close file descriptors on restart. This is because any file descriptor opened by Ruby will have the `FD_CLOEXEC` flag set, meaning that file descriptors are closed on `exec`. `on_restart` is useful, though, if your application needs to perform any more graceful protocol-specific shutdown procedures before closing connections.
28
32
 
29
- ```ruby
30
- # config/puma.rb
33
+ ## Phased restart
31
34
 
32
- directory '/var/www/current'
33
- ```
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.
34
36
 
35
- ### Cleanup Code
37
+ ### How-to
36
38
 
37
- 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.
39
+ Any of the following will cause a Puma server to perform a phased restart:
38
40
 
39
- 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.
41
+ * Send the `puma` process the `SIGUSR1` signal
42
+ * Issue a `GET` request to the Puma status/control server with the path `/phased-restart`
43
+ * Issue `pumactl phased-restart` (this uses the control server method if available, otherwise sends the `SIGUSR1` signal to the process)
44
+
45
+ ### Supported configurations
46
+
47
+ * Works in cluster mode only
48
+ * To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app!` is disabled
49
+ * Supported on all platforms where cluster mode is supported
50
+
51
+ ### Client experience
52
+
53
+ * In-flight requests are always served responses before the connection is closed gracefully
54
+ * Idle persistent connections are gracefully disconnected
55
+ * New connections are not lost, and clients will not experience any increase in latency (as long as the number of configured workers is greater than one)
56
+
57
+ ### Additional notes
58
+
59
+ * When a phased restart begins, the Puma master process changes its current working directory to the directory specified by the `directory` option. If `directory` is set to symlink, this is automatically re-evaluated, so this mechanism can be used to upgrade the application.
60
+ * On a single server, it's possible that two versions of the application are running concurrently during a phased restart.
61
+ * `on_restart` is not invoked
62
+ * Phased restarts can be slow for Puma clusters with many workers. Hot restarts often complete more quickly, but at the cost of increased latency during the restart.
63
+ * Phased restarts cannot be used to upgrade any gems loaded by the Puma master process, including `puma` itself, anything in `extra_runtime_dependencies`, or dependencies thereof. Upgrading other gems is safe.
64
+ * If you remove the gems from old releases as part of your deployment strategy, there are additional considerations. Do not put any gems into `extra_runtime_dependencies` that have native extensions or have dependencies that have native extensions (one common example is `puma_worker_killer` and its dependency on `ffi`). Workers will fail on boot during a phased restart. The underlying issue is recorded in [an issue on the rubygems project](https://github.com/rubygems/rubygems/issues/4004). Hot restarts are your only option here if you need these dependencies.
data/docs/signals.md CHANGED
@@ -1,8 +1,8 @@
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.
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 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:
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,24 +10,24 @@ $ 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
17
17
  schneems 87152 0.0 0.0 2432772 492 s032 S+ 12:46PM 0:00.00 tail -f my.log
18
18
  ```
19
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):
20
+ You can send a signal in Ruby using the [Process module](https://www.ruby-doc.org/core-2.1.1/Process.html#kill-method):
21
21
 
22
22
  ```
23
23
  $ irb
24
24
  > puts pid
25
25
  => 87152
26
- Process.detach(pid) # http://ruby-doc.org/core-2.1.1/Process.html#method-c-detach
26
+ Process.detach(pid) # https://ruby-doc.org/core-2.1.1/Process.html#method-c-detach
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 for instance `SIGTERM` is equivalent to sending `TERM` via `Process.kill`.
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,12 +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. 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.
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 if `fork_workers` option is enabled.
45
+ - `INFO` print backtraces of all puma threads
44
46
 
45
47
  ## Callbacks order in case of different signals
46
48
 
data/docs/stats.md ADDED
@@ -0,0 +1,142 @@
1
+ ## Accessing stats
2
+
3
+ Stats can be accessed in two ways:
4
+
5
+ ### control server
6
+
7
+ `$ pumactl stats` or `GET /stats`
8
+
9
+ [Read more about `pumactl` and the control server in the README.](https://github.com/puma/puma#controlstatus-server).
10
+
11
+ ### Puma.stats
12
+
13
+ `Puma.stats` produces a JSON string. `Puma.stats_hash` produces a ruby hash.
14
+
15
+ #### in single mode
16
+
17
+ Invoke `Puma.stats` anywhere in runtime, e.g. in a rails initializer:
18
+
19
+ ```ruby
20
+ # config/initializers/puma_stats.rb
21
+
22
+ Thread.new do
23
+ loop do
24
+ sleep 30
25
+ puts Puma.stats
26
+ end
27
+ end
28
+ ```
29
+
30
+ #### in cluster mode
31
+
32
+ Invoke `Puma.stats` from the master process
33
+
34
+ ```ruby
35
+ # config/puma.rb
36
+
37
+ before_fork do
38
+ Thread.new do
39
+ loop do
40
+ puts Puma.stats
41
+ sleep 30
42
+ end
43
+ end
44
+ end
45
+ ```
46
+
47
+
48
+ ## Explanation of stats
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:
51
+
52
+ * started_at: when Puma was started
53
+
54
+ ### single mode and individual workers in cluster mode
55
+
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
+
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
+ * 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 Puma is configured to spool per worker
62
+ * requests_count: the number of requests this worker has served since starting
63
+
64
+
65
+ ### cluster mode
66
+
67
+ * phase: which phase of restart the process is in, during [phased restart](https://github.com/puma/puma/blob/master/docs/restart.md)
68
+ * workers: ??
69
+ * booted_workers: how many workers currently running?
70
+ * old_workers: ??
71
+ * worker_status: array of hashes of info for each worker (see below)
72
+
73
+ ### worker status
74
+
75
+ * started_at: when the worker started
76
+ * pid: the process id of the worker process
77
+ * index: each worker gets a number. if Puma is configured to have 3 workers, then this will be 0, 1, or 2
78
+ * booted: if it's done booting [?]
79
+ * last_checkin: Last time the worker responded to the master process' heartbeat check.
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.
81
+
82
+
83
+ ## Examples
84
+
85
+ Here are two example stats hashes produced by `Puma.stats`:
86
+
87
+ ### single
88
+
89
+ ```json
90
+ {
91
+ "started_at": "2021-01-14T07:12:35Z",
92
+ "backlog": 0,
93
+ "running": 5,
94
+ "pool_capacity": 5,
95
+ "max_threads": 5,
96
+ "requests_count": 3
97
+ }
98
+ ```
99
+
100
+ ### cluster
101
+
102
+ ```json
103
+ {
104
+ "started_at": "2021-01-14T07:09:17Z",
105
+ "workers": 2,
106
+ "phase": 0,
107
+ "booted_workers": 2,
108
+ "old_workers": 0,
109
+ "worker_status": [
110
+ {
111
+ "started_at": "2021-01-14T07:09:24Z",
112
+ "pid": 64136,
113
+ "index": 0,
114
+ "phase": 0,
115
+ "booted": true,
116
+ "last_checkin": "2021-01-14T07:11:09Z",
117
+ "last_status": {
118
+ "backlog": 0,
119
+ "running": 5,
120
+ "pool_capacity": 5,
121
+ "max_threads": 5,
122
+ "requests_count": 2
123
+ }
124
+ },
125
+ {
126
+ "started_at": "2021-01-14T07:09:24Z",
127
+ "pid": 64137,
128
+ "index": 1,
129
+ "phase": 0,
130
+ "booted": true,
131
+ "last_checkin": "2021-01-14T07:11:09Z",
132
+ "last_status": {
133
+ "backlog": 0,
134
+ "running": 5,
135
+ "pool_capacity": 5,
136
+ "max_threads": 5,
137
+ "requests_count": 1
138
+ }
139
+ }
140
+ ]
141
+ }
142
+ ```
data/docs/systemd.md CHANGED
@@ -1,21 +1,18 @@
1
1
  # systemd
2
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.
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
- can be copied or symlinked to /etc/systemd/system/puma.service, or if
12
- desired, using an application or instance specific name.
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
- 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.
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).
19
16
 
20
17
  ~~~~ ini
21
18
  [Unit]
@@ -26,27 +23,39 @@ After=network.target
26
23
  # Requires=puma.socket
27
24
 
28
25
  [Service]
29
- # Foreground process (do not use --daemon in ExecStart or config.rb)
30
- Type=simple
26
+ # Puma supports systemd's `Type=notify` and watchdog service
27
+ # monitoring, if the [sd_notify](https://github.com/agis/ruby-sdnotify) gem is installed,
28
+ # as of Puma 5.1 or later.
29
+ # On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
30
+ # the `WatchdogSec` line.
31
+ Type=notify
32
+
33
+ # If your Puma process locks up, systemd's watchdog will restart it within seconds.
34
+ WatchdogSec=10
31
35
 
32
36
  # Preferably configure a non-privileged user
33
37
  # User=
34
38
 
35
- # The path to the puma application root
36
- # Also replace the "<WD>" place holders below with this path.
37
- WorkingDirectory=
39
+ # The path to your application code root directory.
40
+ # Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
41
+ # Example /home/username/myapp
42
+ WorkingDirectory=<YOUR_APP_PATH>
38
43
 
39
44
  # Helpful for debugging socket activation, etc.
40
45
  # Environment=PUMA_DEBUG=1
41
46
 
42
- # The command to start Puma. This variant uses a binstub generated via
43
- # `bundle binstubs puma --path ./sbin` in the WorkingDirectory
44
- # (replace "<WD>" below)
45
- ExecStart=<WD>/sbin/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
47
+ # SystemD will not run puma even if it is in your path. You must specify
48
+ # an absolute URL to puma. For example /usr/local/bin/puma
49
+ # Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
50
+ ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
51
+
52
+ # Variant: Rails start.
53
+ # ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
46
54
 
47
- # Variant: Use config file with `bind` directives instead:
48
- # ExecStart=<WD>/sbin/puma -C config.rb
49
55
  # Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
56
+ # Variant: Specify directives inline.
57
+ # ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
58
+
50
59
 
51
60
  Restart=always
52
61
 
@@ -54,26 +63,31 @@ Restart=always
54
63
  WantedBy=multi-user.target
55
64
  ~~~~
56
65
 
57
- See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
66
+ See
67
+ [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
58
68
  for additional details.
59
69
 
60
70
  ## Socket Activation
61
71
 
62
- systemd and puma also support socket activation, where systemd opens
63
- the listening socket(s) in advance and provides them to the puma
64
- master process on startup. Among other advantages, this keeps
65
- listening sockets open across puma restarts and achieves graceful
66
- restarts, including when upgraded puma, and is compatible with both
67
- clustered mode and application preload.
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].
68
83
 
69
- **Note:** Socket activation doesn't currently work on jruby. This is
70
- tracked in [#1367].
84
+ **Note:** Socket activation doesn't currently work on JRuby. This is tracked in
85
+ [#1367].
71
86
 
72
- To use socket activation, configure one or more `ListenStream` sockets
73
- in a companion `*.socket` unit file. Also uncomment the associated
74
- `Requires` directive for the socket unit in the service file (see
75
- above.) Here is a sample puma.socket, matching the ports used in the
76
- above puma.service:
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:
77
91
 
78
92
  ~~~~ ini
79
93
  [Unit]
@@ -96,16 +110,42 @@ Backlog=1024
96
110
  WantedBy=sockets.target
97
111
  ~~~~
98
112
 
99
- See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
113
+ See
114
+ [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
100
115
  for additional configuration details.
101
116
 
102
- Note that the above configurations will work with Puma in either
103
- single process or cluster mode.
117
+ Note that the above configurations will work with Puma in either single process
118
+ or cluster mode.
119
+
120
+ ### Sockets and symlinks
121
+
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`).
125
+
126
+ Puma will detect the release path socket as different than the one provided by
127
+ systemd and attempt to bind it again, resulting in the exception `There is
128
+ already a server bound to:`.
129
+
130
+ ### Binding
131
+
132
+ By default, you need to configure Puma to have binds matching with all
133
+ ListenStream statements. Any mismatched systemd ListenStreams will be closed by
134
+ Puma.
135
+
136
+ To automatically bind to all activated sockets, the option
137
+ `--bind-to-activated-sockets` can be used. This matches the config DSL
138
+ `bind_to_activated_sockets` statement. This will cause Puma to create a bind
139
+ automatically for any activated socket. When systemd socket activation is not
140
+ enabled, this option does nothing.
141
+
142
+ This also accepts an optional argument `only` (DSL: `'only'`) to discard any
143
+ binds that's not socket activated.
104
144
 
105
145
  ## Usage
106
146
 
107
- Without socket activation, use `systemctl` as root (e.g. via `sudo`) as
108
- with other system services:
147
+ Without socket activation, use `systemctl` as root (i.e., via `sudo`) as with
148
+ other system services:
109
149
 
110
150
  ~~~~ sh
111
151
  # After installing or making changes to puma.service
@@ -114,35 +154,35 @@ systemctl daemon-reload
114
154
  # Enable so it starts on boot
115
155
  systemctl enable puma.service
116
156
 
117
- # Initial start up.
157
+ # Initial startup.
118
158
  systemctl start puma.service
119
159
 
120
160
  # Check status
121
161
  systemctl status puma.service
122
162
 
123
- # A normal restart. Warning: listeners sockets will be closed
163
+ # A normal restart. Warning: listener's sockets will be closed
124
164
  # while a new puma process initializes.
125
165
  systemctl restart puma.service
126
166
  ~~~~
127
167
 
128
- With socket activation, several but not all of these commands should
129
- be run for both socket and service:
168
+ With socket activation, several but not all of these commands should be run for
169
+ both socket and service:
130
170
 
131
171
  ~~~~ sh
132
172
  # After installing or making changes to either puma.socket or
133
173
  # puma.service.
134
174
  systemctl daemon-reload
135
175
 
136
- # Enable both socket and service so they start on boot. Alternatively
137
- # you could leave puma.service disabled and systemd will start it on
138
- # 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)
139
179
  systemctl enable puma.socket puma.service
140
180
 
141
- # Initial start up. The Requires directive (see above) ensures the
181
+ # Initial startup. The Requires directive (see above) ensures the
142
182
  # socket is started before the service.
143
183
  systemctl start puma.socket puma.service
144
184
 
145
- # Check status of both socket and service.
185
+ # Check the status of both socket and service.
146
186
  systemctl status puma.socket puma.service
147
187
 
148
188
  # A "hot" restart, with systemd keeping puma.socket listening and
@@ -155,8 +195,8 @@ systemctl restart puma.service
155
195
  systemctl restart puma.socket puma.service
156
196
  ~~~~
157
197
 
158
- Here is sample output from `systemctl status` with both service and
159
- socket running:
198
+ Here is sample output from `systemctl status` with both service and socket
199
+ running:
160
200
 
161
201
  ~~~~
162
202
  ● puma.socket - Puma HTTP Server Accept Sockets
@@ -187,70 +227,14 @@ Apr 07 08:40:19 hx puma[28320]: * Activated ssl://0.0.0.0:9234?key=key.pem&cert=
187
227
  Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
188
228
  ~~~~
189
229
 
190
- ## Alternative Forking Configuration
191
-
192
- Other systems/tools might expect or need puma to be run as a
193
- "traditional" forking server, for example so that the `pumactl`
194
- command can be used directly and outside of systemd for
195
- stop/start/restart. This use case is incompatible with systemd socket
196
- activation, so it should not be configured. Below is an alternative
197
- puma.service config sample, using `Type=forking` and the `--daemon`
198
- flag in `ExecStart`. Here systemd is playing a role more equivalent to
199
- SysV init.d, where it is responsible for starting Puma on boot
200
- (multi-user.target) and stopping it on shutdown, but is not performing
201
- continuous restarts. Therefore running Puma in cluster mode, where the
202
- master can restart workers, is highly recommended. See the systemd
203
- [Restart] directive for details.
204
-
205
- ~~~~ ini
206
- [Unit]
207
- Description=Puma HTTP Forking Server
208
- After=network.target
209
-
210
- [Service]
211
- # Background process configuration (use with --daemon in ExecStart)
212
- Type=forking
213
-
214
- # Preferably configure a non-privileged user
215
- # User=
216
-
217
- # The path to the puma application root
218
- # Also replace the "<WD>" place holders below with this path.
219
- WorkingDirectory=
220
-
221
- # The command to start Puma
222
- # (replace "<WD>" below)
223
- ExecStart=bundle exec puma -C <WD>/shared/puma.rb --daemon
224
-
225
- # The command to stop Puma
226
- # (replace "<WD>" below)
227
- ExecStop=bundle exec pumactl -S <WD>/shared/tmp/pids/puma.state stop
228
-
229
- # Path to PID file so that systemd knows which is the master process
230
- PIDFile=<WD>/shared/tmp/pids/puma.pid
231
-
232
- # Should systemd restart puma?
233
- # Use "no" (the default) to ensure no interference when using
234
- # stop/start/restart via `pumactl`. The "on-failure" setting might
235
- # work better for this purpose, but you must test it.
236
- # Use "always" if only `systemctl` is used for start/stop/restart, and
237
- # reconsider if you actually need the forking config.
238
- Restart=no
239
-
240
- [Install]
241
- WantedBy=multi-user.target
242
- ~~~~
243
-
244
230
  ### capistrano3-puma
245
231
 
246
- By default,
247
- [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
248
- `pumactl` for deployment restarts, outside of systemd. To learn the
249
- exact commands that this tool would use for `ExecStart` and
250
- `ExecStop`, use the following `cap` commands in dry-run mode, and
251
- update from the above forking service configuration accordingly. Note
252
- also that the configured `User` should likely be the same as the
253
- 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.
254
238
 
255
239
  ~~~~ sh
256
240
  stage=production # or different stage, as needed
@@ -260,3 +244,4 @@ cap $stage puma:stop --dry-run
260
244
 
261
245
  [Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
262
246
  [#1367]: https://github.com/puma/puma/issues/1367
247
+ [#1499]: https://github.com/puma/puma/issues/1499