piesync-puma 3.12.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +1429 -0
  3. data/LICENSE +26 -0
  4. data/README.md +280 -0
  5. data/bin/puma +10 -0
  6. data/bin/puma-wild +31 -0
  7. data/bin/pumactl +12 -0
  8. data/docs/architecture.md +36 -0
  9. data/docs/deployment.md +91 -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/nginx.md +80 -0
  14. data/docs/plugins.md +28 -0
  15. data/docs/restart.md +39 -0
  16. data/docs/signals.md +96 -0
  17. data/docs/systemd.md +272 -0
  18. data/ext/puma_http11/PumaHttp11Service.java +17 -0
  19. data/ext/puma_http11/ext_help.h +15 -0
  20. data/ext/puma_http11/extconf.rb +15 -0
  21. data/ext/puma_http11/http11_parser.c +1071 -0
  22. data/ext/puma_http11/http11_parser.h +65 -0
  23. data/ext/puma_http11/http11_parser.java.rl +161 -0
  24. data/ext/puma_http11/http11_parser.rl +149 -0
  25. data/ext/puma_http11/http11_parser_common.rl +54 -0
  26. data/ext/puma_http11/io_buffer.c +155 -0
  27. data/ext/puma_http11/mini_ssl.c +494 -0
  28. data/ext/puma_http11/org/jruby/puma/Http11.java +234 -0
  29. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +470 -0
  30. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +352 -0
  31. data/ext/puma_http11/puma_http11.c +500 -0
  32. data/lib/puma.rb +23 -0
  33. data/lib/puma/accept_nonblock.rb +23 -0
  34. data/lib/puma/app/status.rb +74 -0
  35. data/lib/puma/binder.rb +413 -0
  36. data/lib/puma/cli.rb +235 -0
  37. data/lib/puma/client.rb +480 -0
  38. data/lib/puma/cluster.rb +531 -0
  39. data/lib/puma/commonlogger.rb +108 -0
  40. data/lib/puma/compat.rb +14 -0
  41. data/lib/puma/configuration.rb +361 -0
  42. data/lib/puma/const.rb +239 -0
  43. data/lib/puma/control_cli.rb +264 -0
  44. data/lib/puma/convenient.rb +25 -0
  45. data/lib/puma/daemon_ext.rb +33 -0
  46. data/lib/puma/delegation.rb +13 -0
  47. data/lib/puma/detect.rb +15 -0
  48. data/lib/puma/dsl.rb +518 -0
  49. data/lib/puma/events.rb +153 -0
  50. data/lib/puma/io_buffer.rb +9 -0
  51. data/lib/puma/java_io_buffer.rb +47 -0
  52. data/lib/puma/jruby_restart.rb +84 -0
  53. data/lib/puma/launcher.rb +433 -0
  54. data/lib/puma/minissl.rb +285 -0
  55. data/lib/puma/null_io.rb +44 -0
  56. data/lib/puma/plugin.rb +117 -0
  57. data/lib/puma/plugin/tmp_restart.rb +34 -0
  58. data/lib/puma/rack/backports/uri/common_193.rb +33 -0
  59. data/lib/puma/rack/builder.rb +299 -0
  60. data/lib/puma/rack/urlmap.rb +91 -0
  61. data/lib/puma/rack_default.rb +7 -0
  62. data/lib/puma/reactor.rb +347 -0
  63. data/lib/puma/runner.rb +184 -0
  64. data/lib/puma/server.rb +1072 -0
  65. data/lib/puma/single.rb +123 -0
  66. data/lib/puma/state_file.rb +31 -0
  67. data/lib/puma/tcp_logger.rb +41 -0
  68. data/lib/puma/thread_pool.rb +346 -0
  69. data/lib/puma/util.rb +129 -0
  70. data/lib/rack/handler/puma.rb +115 -0
  71. data/tools/jungle/README.md +19 -0
  72. data/tools/jungle/init.d/README.md +61 -0
  73. data/tools/jungle/init.d/puma +421 -0
  74. data/tools/jungle/init.d/run-puma +18 -0
  75. data/tools/jungle/rc.d/README.md +74 -0
  76. data/tools/jungle/rc.d/puma +61 -0
  77. data/tools/jungle/rc.d/puma.conf +10 -0
  78. data/tools/jungle/upstart/README.md +61 -0
  79. data/tools/jungle/upstart/puma-manager.conf +31 -0
  80. data/tools/jungle/upstart/puma.conf +69 -0
  81. data/tools/trickletest.rb +45 -0
  82. metadata +131 -0
@@ -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
+ ```
@@ -0,0 +1,28 @@
1
+ ## Plugins
2
+
3
+ Puma 3.0 added support for plugins that can augment configuration and service operations.
4
+
5
+ 2 canonical plugins to look to aid in development of further plugins:
6
+
7
+ * [tmp\_restart](https://github.com/puma/puma/blob/master/lib/puma/plugin/tmp_restart.rb): Restarts the server if the file `tmp/restart.txt` is touched
8
+ * [heroku](https://github.com/puma/puma-heroku/blob/master/lib/puma/plugin/heroku.rb): Packages up the default configuration used by puma on Heroku
9
+
10
+ Plugins are activated in a puma configuration file (such as `config/puma.rb'`) by adding `plugin "name"`, such as `plugin "heroku"`.
11
+
12
+ Plugins are activated based simply on path requirements so, activating the `heroku` plugin will simply be doing `require "puma/plugin/heroku"`. This allows gems to provide multiple plugins (as well as unrelated gems to provide puma plugins).
13
+
14
+ The `tmp_restart` plugin is bundled with puma, so it can always be used.
15
+
16
+ To use the `heroku` plugin, add `puma-heroku` to your Gemfile or install it.
17
+
18
+ ### API
19
+
20
+ At present, there are 2 hooks that plugins can use: `start` and `config`.
21
+
22
+ `start` runs when the server has started and allows the plugin to start other functionality to augment puma.
23
+
24
+ `config` runs when the server is being configured and is passed a `Puma::DSL` object that can be used to add additional configuration.
25
+
26
+ Any public methods in `Puma::Plugin` are the public API that any plugin may use.
27
+
28
+ In the future, more hooks and APIs will be added.
@@ -0,0 +1,39 @@
1
+ # Restarts
2
+
3
+ To perform a restart, there are 3 builtin mechanisms:
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`
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
+ ### Release Directory
26
+
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.
28
+
29
+ ```ruby
30
+ # config/puma.rb
31
+
32
+ directory '/var/www/current'
33
+ ```
34
+
35
+ ### Cleanup Code
36
+
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.
38
+
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.
@@ -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
+ ```
@@ -0,0 +1,272 @@
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 puma application root
36
+ # Also replace the "<WD>" place holders below with this path.
37
+ WorkingDirectory=
38
+
39
+ # Helpful for debugging socket activation, etc.
40
+ # Environment=PUMA_DEBUG=1
41
+
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
46
+
47
+ # Variant: Use config file with `bind` directives instead:
48
+ # ExecStart=<WD>/sbin/puma -C config.rb
49
+ # Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
50
+
51
+ Restart=always
52
+
53
+ [Install]
54
+ WantedBy=multi-user.target
55
+ ~~~~
56
+
57
+ See [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
58
+ for additional details.
59
+
60
+ ## Socket Activation
61
+
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.
68
+
69
+ **Note:** Socket activation doesn't currently work on jruby. This is
70
+ tracked in [#1367].
71
+
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:
77
+
78
+ ~~~~ ini
79
+ [Unit]
80
+ Description=Puma HTTP Server Accept Sockets
81
+
82
+ [Socket]
83
+ ListenStream=0.0.0.0:9292
84
+ ListenStream=0.0.0.0:9293
85
+
86
+ # AF_UNIX domain socket
87
+ # SocketUser, SocketGroup, etc. may be needed for Unix domain sockets
88
+ # ListenStream=/run/puma.sock
89
+
90
+ # Socket options matching Puma defaults
91
+ NoDelay=true
92
+ ReusePort=true
93
+ Backlog=1024
94
+
95
+ [Install]
96
+ WantedBy=sockets.target
97
+ ~~~~
98
+
99
+ See [systemd.socket](https://www.freedesktop.org/software/systemd/man/systemd.socket.html)
100
+ for additional configuration details.
101
+
102
+ Note that the above configurations will work with Puma in either
103
+ single process or cluster mode.
104
+
105
+ ### Sockets and symlinks
106
+
107
+ When using releases folders, you should set the socket path using the
108
+ shared folder path (ex. `/srv/projet/shared/tmp/puma.sock`), not the
109
+ release folder path (`/srv/projet/releases/1234/tmp/puma.sock`).
110
+
111
+ Puma will detect the release path socket as different than the one provided by
112
+ systemd and attempt to bind it again, resulting in the exception
113
+ `There is already a server bound to:`.
114
+
115
+ ## Usage
116
+
117
+ Without socket activation, use `systemctl` as root (e.g. via `sudo`) as
118
+ with other system services:
119
+
120
+ ~~~~ sh
121
+ # After installing or making changes to puma.service
122
+ systemctl daemon-reload
123
+
124
+ # Enable so it starts on boot
125
+ systemctl enable puma.service
126
+
127
+ # Initial start up.
128
+ systemctl start puma.service
129
+
130
+ # Check status
131
+ systemctl status puma.service
132
+
133
+ # A normal restart. Warning: listeners sockets will be closed
134
+ # while a new puma process initializes.
135
+ systemctl restart puma.service
136
+ ~~~~
137
+
138
+ With socket activation, several but not all of these commands should
139
+ be run for both socket and service:
140
+
141
+ ~~~~ sh
142
+ # After installing or making changes to either puma.socket or
143
+ # puma.service.
144
+ systemctl daemon-reload
145
+
146
+ # Enable both socket and service so they start on boot. Alternatively
147
+ # you could leave puma.service disabled and systemd will start it on
148
+ # first use (with startup lag on first request)
149
+ systemctl enable puma.socket puma.service
150
+
151
+ # Initial start up. The Requires directive (see above) ensures the
152
+ # socket is started before the service.
153
+ systemctl start puma.socket puma.service
154
+
155
+ # Check status of both socket and service.
156
+ systemctl status puma.socket puma.service
157
+
158
+ # A "hot" restart, with systemd keeping puma.socket listening and
159
+ # providing to the new puma (master) instance.
160
+ systemctl restart puma.service
161
+
162
+ # A normal restart, needed to handle changes to
163
+ # puma.socket, such as changing the ListenStream ports. Note
164
+ # daemon-reload (above) should be run first.
165
+ systemctl restart puma.socket puma.service
166
+ ~~~~
167
+
168
+ Here is sample output from `systemctl status` with both service and
169
+ socket running:
170
+
171
+ ~~~~
172
+ ● puma.socket - Puma HTTP Server Accept Sockets
173
+ Loaded: loaded (/etc/systemd/system/puma.socket; enabled; vendor preset: enabled)
174
+ Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
175
+ Listen: 0.0.0.0:9233 (Stream)
176
+ 0.0.0.0:9234 (Stream)
177
+
178
+ Apr 07 08:40:19 hx systemd[874]: Listening on Puma HTTP Server Accept Sockets.
179
+
180
+ ● puma.service - Puma HTTP Server
181
+ Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
182
+ Active: active (running) since Thu 2016-04-07 08:40:19 PDT; 1h 2min ago
183
+ Main PID: 28320 (ruby)
184
+ CGroup: /system.slice/puma.service
185
+ ├─28320 puma 3.3.0 (tcp://0.0.0.0:9233,ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem) [app]
186
+ ├─28323 puma: cluster worker 0: 28320 [app]
187
+ └─28327 puma: cluster worker 1: 28320 [app]
188
+
189
+ Apr 07 08:40:19 hx puma[28320]: Puma starting in cluster mode...
190
+ Apr 07 08:40:19 hx puma[28320]: * Version 3.3.0 (ruby 2.2.4-p230), codename: Jovial Platypus
191
+ Apr 07 08:40:19 hx puma[28320]: * Min threads: 0, max threads: 16
192
+ Apr 07 08:40:19 hx puma[28320]: * Environment: production
193
+ Apr 07 08:40:19 hx puma[28320]: * Process workers: 2
194
+ Apr 07 08:40:19 hx puma[28320]: * Phased restart available
195
+ Apr 07 08:40:19 hx puma[28320]: * Activated tcp://0.0.0.0:9233
196
+ Apr 07 08:40:19 hx puma[28320]: * Activated ssl://0.0.0.0:9234?key=key.pem&cert=cert.pem
197
+ Apr 07 08:40:19 hx puma[28320]: Use Ctrl-C to stop
198
+ ~~~~
199
+
200
+ ## Alternative Forking Configuration
201
+
202
+ Other systems/tools might expect or need puma to be run as a
203
+ "traditional" forking server, for example so that the `pumactl`
204
+ command can be used directly and outside of systemd for
205
+ stop/start/restart. This use case is incompatible with systemd socket
206
+ activation, so it should not be configured. Below is an alternative
207
+ puma.service config sample, using `Type=forking` and the `--daemon`
208
+ flag in `ExecStart`. Here systemd is playing a role more equivalent to
209
+ SysV init.d, where it is responsible for starting Puma on boot
210
+ (multi-user.target) and stopping it on shutdown, but is not performing
211
+ continuous restarts. Therefore running Puma in cluster mode, where the
212
+ master can restart workers, is highly recommended. See the systemd
213
+ [Restart] directive for details.
214
+
215
+ ~~~~ ini
216
+ [Unit]
217
+ Description=Puma HTTP Forking Server
218
+ After=network.target
219
+
220
+ [Service]
221
+ # Background process configuration (use with --daemon in ExecStart)
222
+ Type=forking
223
+
224
+ # Preferably configure a non-privileged user
225
+ # User=
226
+
227
+ # The path to the puma application root
228
+ # Also replace the "<WD>" place holders below with this path.
229
+ WorkingDirectory=
230
+
231
+ # The command to start Puma
232
+ # (replace "<WD>" below)
233
+ ExecStart=bundle exec puma -C <WD>/shared/puma.rb --daemon
234
+
235
+ # The command to stop Puma
236
+ # (replace "<WD>" below)
237
+ ExecStop=bundle exec pumactl -S <WD>/shared/tmp/pids/puma.state stop
238
+
239
+ # Path to PID file so that systemd knows which is the master process
240
+ PIDFile=<WD>/shared/tmp/pids/puma.pid
241
+
242
+ # Should systemd restart puma?
243
+ # Use "no" (the default) to ensure no interference when using
244
+ # stop/start/restart via `pumactl`. The "on-failure" setting might
245
+ # work better for this purpose, but you must test it.
246
+ # Use "always" if only `systemctl` is used for start/stop/restart, and
247
+ # reconsider if you actually need the forking config.
248
+ Restart=no
249
+
250
+ [Install]
251
+ WantedBy=multi-user.target
252
+ ~~~~
253
+
254
+ ### capistrano3-puma
255
+
256
+ By default,
257
+ [capistrano3-puma](https://github.com/seuros/capistrano-puma) uses
258
+ `pumactl` for deployment restarts, outside of systemd. To learn the
259
+ exact commands that this tool would use for `ExecStart` and
260
+ `ExecStop`, use the following `cap` commands in dry-run mode, and
261
+ update from the above forking service configuration accordingly. Note
262
+ also that the configured `User` should likely be the same as the
263
+ capistrano3-puma `:puma_user` option.
264
+
265
+ ~~~~ sh
266
+ stage=production # or different stage, as needed
267
+ cap $stage puma:start --dry-run
268
+ cap $stage puma:stop --dry-run
269
+ ~~~~
270
+
271
+ [Restart]: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
272
+ [#1367]: https://github.com/puma/puma/issues/1367