.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Mojo::Server::Prefork 3pm" .TH Mojo::Server::Prefork 3pm "2017-01-24" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::Server::Prefork \- Pre\-forking non\-blocking I/O HTTP and WebSocket server .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Server::Prefork; \& \& my $prefork = Mojo::Server::Prefork\->new(listen => [\*(Aqhttp://*:8080\*(Aq]); \& $prefork\->unsubscribe(\*(Aqrequest\*(Aq)\->on(request => sub { \& my ($prefork, $tx) = @_; \& \& # Request \& my $method = $tx\->req\->method; \& my $path = $tx\->req\->url\->path; \& \& # Response \& $tx\->res\->code(200); \& $tx\->res\->headers\->content_type(\*(Aqtext/plain\*(Aq); \& $tx\->res\->body("$method request for $path!"); \& \& # Resume transaction \& $tx\->resume; \& }); \& $prefork\->run; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::Server::Prefork is a full featured, \s-1UNIX\s0 optimized, pre-forking non-blocking I/O \s-1HTTP\s0 and WebSocket server, built around the very well tested and reliable Mojo::Server::Daemon, with IPv6, \s-1TLS, SNI,\s0 Comet (long polling), keep-alive and multiple event loop support. Note that the server uses signals for process management, so you should avoid modifying signal handlers in your applications. .PP For better scalability (epoll, kqueue) and to provide non-blocking name resolution, \s-1SOCKS5\s0 as well as \s-1TLS\s0 support, the optional modules \s-1EV\s0 (4.0+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and IO::Socket::SSL (1.84+) will be used automatically if possible. Individual features can also be disabled with the \f(CW\*(C`MOJO_NO_NNR\*(C'\fR, \f(CW\*(C`MOJO_NO_SOCKS\*(C'\fR and \&\f(CW\*(C`MOJO_NO_TLS\*(C'\fR environment variables. .PP See \*(L"\s-1DEPLOYMENT\*(R"\s0 in Mojolicious::Guides::Cookbook for more. .SH "MANAGER SIGNALS" .IX Header "MANAGER SIGNALS" The Mojo::Server::Prefork manager process can be controlled at runtime with the following signals. .SS "\s-1INT, TERM\s0" .IX Subsection "INT, TERM" Shut down server immediately. .SS "\s-1QUIT\s0" .IX Subsection "QUIT" Shut down server gracefully. .SS "\s-1TTIN\s0" .IX Subsection "TTIN" Increase worker pool by one. .SS "\s-1TTOU\s0" .IX Subsection "TTOU" Decrease worker pool by one. .SH "WORKER SIGNALS" .IX Header "WORKER SIGNALS" Mojo::Server::Prefork worker processes can be controlled at runtime with the following signals. .SS "\s-1QUIT\s0" .IX Subsection "QUIT" Stop worker gracefully. .SH "EVENTS" .IX Header "EVENTS" Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and can emit the following new ones. .SS "finish" .IX Subsection "finish" .Vb 4 \& $prefork\->on(finish => sub { \& my ($prefork, $graceful) = @_; \& ... \& }); .Ve .PP Emitted when the server shuts down. .PP .Vb 4 \& $prefork\->on(finish => sub { \& my ($prefork, $graceful) = @_; \& say $graceful ? \*(AqGraceful server shutdown\*(Aq : \*(AqServer shutdown\*(Aq; \& }); .Ve .SS "heartbeat" .IX Subsection "heartbeat" .Vb 4 \& $prefork\->on(heartbeat => sub { \& my ($prefork, $pid) = @_; \& ... \& }); .Ve .PP Emitted when a heartbeat message has been received from a worker. .PP .Vb 4 \& $prefork\->on(heartbeat => sub { \& my ($prefork, $pid) = @_; \& say "Worker $pid has a heartbeat"; \& }); .Ve .SS "reap" .IX Subsection "reap" .Vb 4 \& $prefork\->on(reap => sub { \& my ($prefork, $pid) = @_; \& ... \& }); .Ve .PP Emitted when a child process dies. .PP .Vb 4 \& $prefork\->on(reap => sub { \& my ($prefork, $pid) = @_; \& say "Worker $pid stopped"; \& }); .Ve .SS "spawn" .IX Subsection "spawn" .Vb 4 \& $prefork\->on(spawn => sub { \& my ($prefork, $pid) = @_; \& ... \& }); .Ve .PP Emitted when a worker process is spawned. .PP .Vb 4 \& $prefork\->on(spawn => sub { \& my ($prefork, $pid) = @_; \& say "Worker $pid started"; \& }); .Ve .SS "wait" .IX Subsection "wait" .Vb 4 \& $prefork\->on(wait => sub { \& my $prefork = shift; \& ... \& }); .Ve .PP Emitted when the manager starts waiting for new heartbeat messages. .PP .Vb 5 \& $prefork\->on(wait => sub { \& my $prefork = shift; \& my $workers = $prefork\->workers; \& say "Waiting for heartbeat messages from $workers workers"; \& }); .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::Server::Prefork inherits all attributes from Mojo::Server::Daemon and implements the following new ones. .SS "accepts" .IX Subsection "accepts" .Vb 2 \& my $accepts = $prefork\->accepts; \& $prefork = $prefork\->accepts(100); .Ve .PP Maximum number of connections a worker is allowed to accept, before stopping gracefully and then getting replaced with a newly started worker, passed along to \*(L"max_accepts\*(R" in Mojo::IOLoop, defaults to \f(CW10000\fR. Setting the value to \&\f(CW0\fR will allow workers to accept new connections indefinitely. Note that up to half of this value can be subtracted randomly to improve load balancing, and to make sure that not all workers restart at the same time. .SS "cleanup" .IX Subsection "cleanup" .Vb 2 \& my $bool = $prefork\->cleanup; \& $prefork = $prefork\->cleanup($bool); .Ve .PP Delete \*(L"pid_file\*(R" automatically once it is not needed anymore, defaults to a true value. .SS "graceful_timeout" .IX Subsection "graceful_timeout" .Vb 2 \& my $timeout = $prefork\->graceful_timeout; \& $prefork = $prefork\->graceful_timeout(15); .Ve .PP Maximum amount of time in seconds stopping a worker gracefully may take before being forced, defaults to \f(CW20\fR. Note that this value should usually be a little larger than the maximum amount of time you expect any one request to take. .SS "heartbeat_interval" .IX Subsection "heartbeat_interval" .Vb 2 \& my $interval = $prefork\->heartbeat_interval; \& $prefork = $prefork\->heartbeat_interval(3); .Ve .PP Heartbeat interval in seconds, defaults to \f(CW5\fR. .SS "heartbeat_timeout" .IX Subsection "heartbeat_timeout" .Vb 2 \& my $timeout = $prefork\->heartbeat_timeout; \& $prefork = $prefork\->heartbeat_timeout(2); .Ve .PP Maximum amount of time in seconds before a worker without a heartbeat will be stopped gracefully, defaults to \f(CW20\fR. Note that this value should usually be a little larger than the maximum amount of time you expect any one operation to block the event loop. .SS "pid_file" .IX Subsection "pid_file" .Vb 2 \& my $file = $prefork\->pid_file; \& $prefork = $prefork\->pid_file(\*(Aq/tmp/prefork.pid\*(Aq); .Ve .PP Full path of process id file, defaults to \f(CW\*(C`prefork.pid\*(C'\fR in a temporary directory. .SS "workers" .IX Subsection "workers" .Vb 2 \& my $workers = $prefork\->workers; \& $prefork = $prefork\->workers(10); .Ve .PP Number of worker processes, defaults to \f(CW4\fR. A good rule of thumb is two worker processes per \s-1CPU\s0 core for applications that perform mostly non-blocking operations, blocking operations often require more and benefit from decreasing concurrency with \*(L"clients\*(R" in Mojo::Server::Daemon (often as low as \f(CW1\fR). .SH "METHODS" .IX Header "METHODS" Mojo::Server::Prefork inherits all methods from Mojo::Server::Daemon and implements the following new ones. .SS "check_pid" .IX Subsection "check_pid" .Vb 1 \& my $pid = $prefork\->check_pid; .Ve .PP Get process id for running server from \*(L"pid_file\*(R" or delete it if server is not running. .PP .Vb 1 \& say \*(AqServer is not running\*(Aq unless $prefork\->check_pid; .Ve .SS "ensure_pid_file" .IX Subsection "ensure_pid_file" .Vb 1 \& $prefork\->ensure_pid_file($pid); .Ve .PP Ensure \*(L"pid_file\*(R" exists. .SS "healthy" .IX Subsection "healthy" .Vb 1 \& my $healthy = $prefork\->healthy; .Ve .PP Number of currently active worker processes with a heartbeat. .SS "run" .IX Subsection "run" .Vb 1 \& $prefork\->run; .Ve .PP Run server and wait for \*(L"\s-1MANAGER SIGNALS\*(R"\s0. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .