1Mojo::Server::Prefork(3U)ser Contributed Perl DocumentatiMoonjo::Server::Prefork(3)
2
3
4

NAME

6       Mojo::Server::Prefork - Pre-forking non-blocking I/O HTTP and WebSocket
7       server
8

SYNOPSIS

10         use Mojo::Server::Prefork;
11
12         my $prefork = Mojo::Server::Prefork->new(listen => ['http://*:8080']);
13         $prefork->unsubscribe('request')->on(request => sub ($prefork, $tx) {
14
15           # Request
16           my $method = $tx->req->method;
17           my $path   = $tx->req->url->path;
18
19           # Response
20           $tx->res->code(200);
21           $tx->res->headers->content_type('text/plain');
22           $tx->res->body("$method request for $path!");
23
24           # Resume transaction
25           $tx->resume;
26         });
27         $prefork->run;
28

DESCRIPTION

30       Mojo::Server::Prefork is a full featured, UNIX optimized, pre-forking
31       non-blocking I/O HTTP and WebSocket server, built around the very well
32       tested and reliable Mojo::Server::Daemon, with IPv6, TLS, SNI, UNIX
33       domain socket, Comet (long polling), keep-alive and multiple event loop
34       support. Note that the server uses signals for process management, so
35       you should avoid modifying signal handlers in your applications.
36
37       For better scalability (epoll, kqueue) and to provide non-blocking name
38       resolution, SOCKS5 as well as TLS support, the optional modules EV
39       (4.32+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and
40       IO::Socket::SSL (1.84+) will be used automatically if possible.
41       Individual features can also be disabled with the "MOJO_NO_NNR",
42       "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.
43
44       See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
45

MANAGER SIGNALS

47       The Mojo::Server::Prefork manager process can be controlled at runtime
48       with the following signals.
49
50   INT, TERM
51       Shut down server immediately.
52
53   QUIT
54       Shut down server gracefully.
55
56   TTIN
57       Increase worker pool by one.
58
59   TTOU
60       Decrease worker pool by one.
61

WORKER SIGNALS

63       Mojo::Server::Prefork worker processes can be controlled at runtime
64       with the following signals.
65
66   QUIT
67       Stop worker gracefully.
68

EVENTS

70       Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and
71       can emit the following new ones.
72
73   finish
74         $prefork->on(finish => sub ($prefork, $graceful) {...});
75
76       Emitted when the server shuts down.
77
78         $prefork->on(finish => sub ($prefork, $graceful) {
79           say $graceful ? 'Graceful server shutdown' : 'Server shutdown';
80         });
81
82   heartbeat
83         $prefork->on(heartbeat => sub ($prefork, $pid) {...});
84
85       Emitted when a heartbeat message has been received from a worker.
86
87         $prefork->on(heartbeat => sub ($prefork, $pid) { say "Worker $pid has a heartbeat" });
88
89   reap
90         $prefork->on(reap => sub ($prefork, $pid) {...});
91
92       Emitted when a child process exited.
93
94         $prefork->on(reap => sub ($prefork, $pid) { say "Worker $pid stopped" });
95
96   spawn
97         $prefork->on(spawn => sub ($prefork, $pid) {...});
98
99       Emitted when a worker process is spawned.
100
101         $prefork->on(spawn => sub ($prefork, $pid) { say "Worker $pid started" });
102
103   wait
104         $prefork->on(wait => sub ($prefork) {...});
105
106       Emitted when the manager starts waiting for new heartbeat messages.
107
108         $prefork->on(wait => sub ($prefork) {
109           my $workers = $prefork->workers;
110           say "Waiting for heartbeat messages from $workers workers";
111         });
112

ATTRIBUTES

114       Mojo::Server::Prefork inherits all attributes from Mojo::Server::Daemon
115       and implements the following new ones.
116
117   accepts
118         my $accepts = $prefork->accepts;
119         $prefork    = $prefork->accepts(100);
120
121       Maximum number of connections a worker is allowed to accept, before
122       stopping gracefully and then getting replaced with a newly started
123       worker, passed along to "max_accepts" in Mojo::IOLoop, defaults to
124       10000. Setting the value to 0 will allow workers to accept new
125       connections indefinitely. Note that up to half of this value can be
126       subtracted randomly to improve load balancing, and to make sure that
127       not all workers restart at the same time.
128
129   cleanup
130         my $bool = $prefork->cleanup;
131         $prefork = $prefork->cleanup($bool);
132
133       Delete "pid_file" automatically once it is not needed anymore, defaults
134       to a true value.
135
136   graceful_timeout
137         my $timeout = $prefork->graceful_timeout;
138         $prefork    = $prefork->graceful_timeout(15);
139
140       Maximum amount of time in seconds stopping a worker gracefully may take
141       before being forced, defaults to 120. Note that this value should
142       usually be a little larger than the maximum amount of time you expect
143       any one request to take.
144
145   heartbeat_interval
146         my $interval = $prefork->heartbeat_interval;
147         $prefork     = $prefork->heartbeat_interval(3);
148
149       Heartbeat interval in seconds, defaults to 5.
150
151   heartbeat_timeout
152         my $timeout = $prefork->heartbeat_timeout;
153         $prefork    = $prefork->heartbeat_timeout(2);
154
155       Maximum amount of time in seconds before a worker without a heartbeat
156       will be stopped gracefully, defaults to 50.  Note that this value
157       should usually be a little larger than the maximum amount of time you
158       expect any one operation to block the event loop.
159
160   pid_file
161         my $file = $prefork->pid_file;
162         $prefork = $prefork->pid_file('/tmp/prefork.pid');
163
164       Full path of process id file, defaults to "prefork.pid" in a temporary
165       directory.
166
167   spare
168         my $spare = $prefork->spare;
169         $prefork  = $prefork->spare(4);
170
171       Temporarily spawn up to this number of additional workers if there is a
172       need, defaults to 2. This allows for new workers to be started while
173       old ones are still shutting down gracefully, drastically reducing the
174       performance cost of worker restarts.
175
176   workers
177         my $workers = $prefork->workers;
178         $prefork    = $prefork->workers(10);
179
180       Number of worker processes, defaults to 4. A good rule of thumb is two
181       worker processes per CPU core for applications that perform mostly non-
182       blocking operations, blocking operations often require more and benefit
183       from decreasing concurrency with "max_clients" in Mojo::Server::Daemon
184       (often as low as 1).
185

METHODS

187       Mojo::Server::Prefork inherits all methods from Mojo::Server::Daemon
188       and implements the following new ones.
189
190   check_pid
191         my $pid = $prefork->check_pid;
192
193       Get process id for running server from "pid_file" or delete it if
194       server is not running.
195
196         say 'Server is not running' unless $prefork->check_pid;
197
198   ensure_pid_file
199         $prefork->ensure_pid_file($pid);
200
201       Ensure "pid_file" exists.
202
203   healthy
204         my $healthy = $prefork->healthy;
205
206       Number of currently active worker processes with a heartbeat.
207
208   run
209         $prefork->run;
210
211       Run server and wait for "MANAGER SIGNALS".
212

SEE ALSO

214       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
215
216
217
218perl v5.34.0                      2021-07-22          Mojo::Server::Prefork(3)
Impressum