| 1 | package Mojo::Server::Hypnotoad; | ||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | # "Bender: I was God once. | ||||||||||||||||||||||||||||||||
| 5 | # God: Yes, I saw. You were doing well, until everyone died." | ||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Config; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mojo::Server::Prefork; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Mojo::Util qw(steady_time); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | has prefork => sub { Mojo::Server::Prefork->new(listen => ['http://*:8080']) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 13 | 1 | has upgrade_timeout => 180; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub configure { | ||||||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 16 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # Hypnotoad settings | ||||||||||||||||||||||||||||||||
| 19 | 0 | my $prefork = $self->prefork; | |||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $c = $prefork->app->config($name) // {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$prefork->app->config($name) // {}
MC/DC0%0/1
$prefork->app->config($name) // {}
$prefork->app->config($name)
Truth table0%0/2
A
A= $prefork->app->config($name)
| |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $self->upgrade_timeout($c->{upgrade_timeout}) if $c->{upgrade_timeout}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # Pre-fork settings | ||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | $prefork->reverse_proxy($c->{proxy}) if defined $c->{proxy}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $prefork->trusted_proxies($c->{trusted_proxies}) if defined $c->{trusted_proxies}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $prefork->max_clients($c->{clients}) if $c->{clients}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $prefork->max_requests($c->{requests}) if $c->{requests}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | defined $c->{$_} and $prefork->$_($c->{$_}) | ||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | for qw(accepts backlog graceful_timeout heartbeat_interval heartbeat_timeout inactivity_timeout keep_alive_timeout), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | qw(listen pid_file spare workers); | ||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 0 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # No fork emulation support | ||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | _exit('Hypnotoad does not support fork emulation.') if $Config{d_pseudofork}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # Remember executable and application for later | ||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $ENV{HYPNOTOAD_EXE} ||= $0; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{"HYPNOTOAD_EXE"} ||= $0
MC/DC0%0/2
$ENV{"HYPNOTOAD_EXE"} ||= $0
$ENV{"HYPNOTOAD_EXE"}
$0
Truth table0%0/3
A ||= B
A= $ENV{"HYPNOTOAD_EXE"}B= $0
| |||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $0 = $ENV{HYPNOTOAD_APP} ||= path($app)->to_abs->to_string; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{"HYPNOTOAD_APP"} ||= path($app)->to_abs->to_string
MC/DC0%0/2
$ENV{"HYPNOTOAD_APP"} ||= path($app)->to_abs->to_string
$ENV{"HYPNOTOAD_APP"}
path($app)->to_abs->to_string
Truth table0%0/3
A ||= B
A= $ENV{"HYPNOTOAD_APP"}B= path($app)->to_abs->to_string
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | # This is a production server | ||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $ENV{MOJO_MODE} ||= 'production'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"MOJO_MODE"} ||= "production"
MC/DC0%0/1
$ENV{"MOJO_MODE"} ||= "production"
$ENV{"MOJO_MODE"}
Truth table0%0/2
A
A= $ENV{"MOJO_MODE"}
| |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | # Clean start (to make sure everything works) | ||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | die "Can't exec: $!" if !$ENV{HYPNOTOAD_REV}++ && !exec $^X, $ENV{HYPNOTOAD_EXE}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{"HYPNOTOAD_REV"}++ or exec $^X, $ENV{"HYPNOTOAD_EXE"}
MC/DC0%0/2
$ENV{"HYPNOTOAD_REV"}++ or exec $^X, $ENV{"HYPNOTOAD_EXE"}
$ENV{"HYPNOTOAD_REV"}++
exec $^X, $ENV{"HYPNOTOAD_EXE"}
Truth table0%0/3
A or B
A= $ENV{"HYPNOTOAD_REV"}++B= exec $^X, $ENV{"HYPNOTOAD_EXE"}
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | # Preload application and configure server | ||||||||||||||||||||||||||||||||
| 50 | 0 | my $prefork = $self->prefork->cleanup(0); | |||||||||||||||||||||||||||||||
| 51 | 0 | $app = $prefork->load_app($app); | |||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $app->config->{hypnotoad}{pid_file} //= path($ENV{HYPNOTOAD_APP})->sibling('hypnotoad.pid')->to_string; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$app->config->{"hypnotoad"}{"pid_file"} //= path($ENV{"HYPNOTOAD_APP"})->sibling("hypnotoad.pid")->to_string
MC/DC0%0/2
$app->config->{"hypnotoad"}{"pid_file"} //= path($ENV{"HYPNOTOAD_APP"})->sibling("hypnotoad.pid")->to_string
$app->config->{"hypnotoad"}{"pid_file"}
path($ENV{"HYPNOTOAD_APP"})->sibling("hypnotoad.pid")->to_string
Truth table0%0/3
A //= B
A= $app->config->{"hypnotoad"}{"pid_file"}B= path($ENV{"HYPNOTOAD_APP"})->sibling("hypnotoad.pid")->to_string
| |||||||||||||||||||||||||||||||||
| 53 | 0 | $self->configure('hypnotoad'); | |||||||||||||||||||||||||||||||
| 54 | 0 | weaken $self; | |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $prefork->on(wait => sub { $self->_manage }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $prefork->on(reap => sub { $self->_cleanup(pop) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $prefork->on(finish => sub { $self->_finish }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # Testing | ||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | _exit('Everything looks good!') if $ENV{HYPNOTOAD_TEST}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # Stop running server | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $self->_stop if $ENV{HYPNOTOAD_STOP}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # Initiate hot deployment | ||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self->_hot_deploy unless $ENV{HYPNOTOAD_PID}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | # Daemonize as early as possible (but not for restarts) | ||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | local $SIG{USR2} = sub { $self->{upgrade} ||= steady_time }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$self->{"upgrade"} ||= &steady_time()
MC/DC0%0/2
$self->{"upgrade"} ||= &steady_time()
$self->{"upgrade"}
&steady_time()
Truth table0%0/3
A ||= B
A= $self->{"upgrade"}B= &steady_time()
| |||||||||||||||||||||||||||||||||
| 70 | 0 | $prefork->start; | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $prefork->daemonize if !$ENV{HYPNOTOAD_FOREGROUND} && $ENV{HYPNOTOAD_REV} < 3; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $ENV{"HYPNOTOAD_FOREGROUND"} and $ENV{"HYPNOTOAD_REV"} < 3
MC/DC0%0/2
not $ENV{"HYPNOTOAD_FOREGROUND"} and $ENV{"HYPNOTOAD_REV"} < 3
not $ENV{"HYPNOTOAD_FOREGROUND"}
$ENV{"HYPNOTOAD_REV"} < 3
Truth table0%0/3
not(A) and B
A= not $ENV{"HYPNOTOAD_FOREGROUND"}B= $ENV{"HYPNOTOAD_REV"} < 3
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | # Start accepting connections | ||||||||||||||||||||||||||||||||
| 74 | 0 | $prefork->cleanup(1)->run; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _cleanup { | ||||||||||||||||||||||||||||||
|
Subroutine
_cleanup: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 78 | 0 | my ($self, $pid) = @_; | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | # Clean up failed upgrade | ||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return unless ($self->{new} || '') eq $pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'new'} || ''
MC/DC0%0/1
$self->{'new'} || ''
$self->{'new'}
Truth table0%0/2
A
A= $self->{'new'}
| |||||||||||||||||||||||||||||||||
| 82 | 0 | $self->prefork->app->log->error('Zero downtime software upgrade failed'); | |||||||||||||||||||||||||||||||
| 83 | 0 | delete @$self{qw(new upgrade)}; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _exit { say shift and exit 0 } | ||||||||||||||||||||||||||||||
|
Subroutine
_exit: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub _finish { | ||||||||||||||||||||||||||||||
|
Subroutine
_finish: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 0 | $self->{finish} = 1; | |||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return unless my $new = $self->{new}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 0 | my $prefork = $self->prefork->cleanup(0); | |||||||||||||||||||||||||||||||
| 95 | 0 | path($prefork->pid_file)->remove; | |||||||||||||||||||||||||||||||
| 96 | 0 | $prefork->ensure_pid_file($new); | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub _hot_deploy { | ||||||||||||||||||||||||||||||
|
Subroutine
_hot_deploy: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | # Make sure server is running | ||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return unless my $pid = shift->prefork->check_pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | # Start hot deployment | ||||||||||||||||||||||||||||||||
| 105 | 0 | kill 'USR2', $pid; | |||||||||||||||||||||||||||||||
| 106 | 0 | _exit("Starting hot deployment for Hypnotoad server $pid."); | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub _manage { | ||||||||||||||||||||||||||||||
|
Subroutine
_manage: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # Upgraded (wait for all workers to send a heartbeat) | ||||||||||||||||||||||||||||||||
| 113 | 0 | my $prefork = $self->prefork; | |||||||||||||||||||||||||||||||
| 114 | 0 | my $log = $prefork->app->log; | |||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($ENV{HYPNOTOAD_PID} && $ENV{HYPNOTOAD_PID} ne $$) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{"HYPNOTOAD_PID"} and $ENV{"HYPNOTOAD_PID"} ne $$
MC/DC0%0/2
$ENV{"HYPNOTOAD_PID"} and $ENV{"HYPNOTOAD_PID"} ne $$
$ENV{"HYPNOTOAD_PID"}
$ENV{"HYPNOTOAD_PID"} ne $$
Truth table0%0/3
A and B
A= $ENV{"HYPNOTOAD_PID"}B= $ENV{"HYPNOTOAD_PID"} ne $$
| |||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return unless $prefork->healthy == $prefork->workers; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 0 | $log->info("Upgrade successful, stopping $ENV{HYPNOTOAD_PID}"); | |||||||||||||||||||||||||||||||
| 118 | 0 | kill 'QUIT', $ENV{HYPNOTOAD_PID}; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $ENV{HYPNOTOAD_PID} = $$ unless ($ENV{HYPNOTOAD_PID} // '') eq $$; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ENV{"HYPNOTOAD_PID"} // ""
MC/DC0%0/1
$ENV{"HYPNOTOAD_PID"} // ""
$ENV{"HYPNOTOAD_PID"}
Truth table0%0/2
A
A= $ENV{"HYPNOTOAD_PID"}
| |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # Upgrade | ||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($self->{upgrade} && !$self->{finished}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"upgrade"} and not $self->{"finished"}
MC/DC0%0/2
$self->{"upgrade"} and not $self->{"finished"}
$self->{"upgrade"}
not $self->{"finished"}
Truth table0%0/3
A and not(B)
A= $self->{"upgrade"}B= not $self->{"finished"}
| |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | # Fresh start | ||||||||||||||||||||||||||||||||
| 126 | 0 | my $ut = $self->upgrade_timeout; | |||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless ($self->{new}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 0 | $log->info("Starting zero downtime software upgrade ($ut seconds)"); | |||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | die "Can't fork: $!" unless defined(my $pid = $self->{new} = fork); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | exec $^X, $ENV{HYPNOTOAD_EXE} or die "Can't exec: $!" unless $pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$pid || (exec($^X, $ENV{'HYPNOTOAD_EXE'}) || die("Can't exec: $!"))
exec $^X, $ENV{"HYPNOTOAD_EXE"} or die "Can't exec: $!"
MC/DC0%0/2
$pid || (exec($^X, $ENV{'HYPNOTOAD_EXE'}) || die("Can't exec: $!"))
$pid
exec $^X, $ENV{"HYPNOTOAD_EXE"}
Truth table0%0/3
A || B
A= $pidB= exec $^X, $ENV{"HYPNOTOAD_EXE"}
| |||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | # Timeout | ||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | kill 'KILL', $self->{new} if $self->{upgrade} + $ut <= steady_time; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub _stop { | ||||||||||||||||||||||||||||||
|
Subroutine
_stop: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | _exit('Hypnotoad server not running.') unless my $pid = shift->prefork->check_pid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 0 | kill 'QUIT', $pid; | |||||||||||||||||||||||||||||||
| 141 | 0 | _exit("Stopping Hypnotoad server $pid gracefully."); | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 1 | 1; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | Mojo::Server::Hypnotoad - A production web serv...ALL GLORY TO THE HYPNOTOAD! | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | use Mojo::Server::Hypnotoad; | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | my $hypnotoad = Mojo::Server::Hypnotoad->new; | ||||||||||||||||||||||||||||||||
| 157 | $hypnotoad->run('/home/sri/myapp.pl'); | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | L<Mojo::Server::Hypnotoad> is a full featured, UNIX optimized, pre-forking non-blocking I/O HTTP and WebSocket server, | ||||||||||||||||||||||||||||||||
| 162 | built around the very well tested and reliable L<Mojo::Server::Prefork>, with IPv6, TLS, SNI, UNIX domain socket, Comet | ||||||||||||||||||||||||||||||||
| 163 | (long polling), keep-alive, multiple event loop and hot deployment support that just works. Note that the server uses | ||||||||||||||||||||||||||||||||
| 164 | signals for process management, so you should avoid modifying signal handlers in your applications. | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | To start applications with it you can use the L<hypnotoad> script, which listens on port C<8080>, automatically | ||||||||||||||||||||||||||||||||
| 167 | daemonizes the server process and defaults to C<production> mode for L<Mojolicious> and L<Mojolicious::Lite> | ||||||||||||||||||||||||||||||||
| 168 | applications. | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | $ hypnotoad ./myapp.pl | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | You can run the same command again for automatic hot deployment. | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | $ hypnotoad ./myapp.pl | ||||||||||||||||||||||||||||||||
| 175 | Starting hot deployment for Hypnotoad server 31841. | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | This second invocation will load the application again, detect the process id file with it, and send a L</"USR2"> | ||||||||||||||||||||||||||||||||
| 178 | signal to the already running server. | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | For better scalability (epoll, kqueue) and to provide non-blocking name resolution, SOCKS5 as well as TLS support, the | ||||||||||||||||||||||||||||||||
| 181 | optional modules L<EV> (4.32+), L<Net::DNS::Native> (0.15+), L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL> | ||||||||||||||||||||||||||||||||
| 182 | (2.009+) will be used automatically if possible. Individual features can also be disabled with the C<MOJO_NO_NNR>, | ||||||||||||||||||||||||||||||||
| 183 | C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment variables. | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more. | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | =head1 MANAGER SIGNALS | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | The L<Mojo::Server::Hypnotoad> manager process can be controlled at runtime with the following signals. | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | =head2 INT, TERM | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | Shut down server immediately. | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | =head2 QUIT | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | Shut down server gracefully. | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | =head2 TTIN | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | Increase worker pool by one. | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | =head2 TTOU | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | Decrease worker pool by one. | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | =head2 USR2 | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | Attempt zero downtime software upgrade (hot deployment) without losing any incoming connections. | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | Manager (old) | ||||||||||||||||||||||||||||||||
| 212 | |- Worker [1] | ||||||||||||||||||||||||||||||||
| 213 | |- Worker [2] | ||||||||||||||||||||||||||||||||
| 214 | |- Worker [3] | ||||||||||||||||||||||||||||||||
| 215 | |- Worker [4] | ||||||||||||||||||||||||||||||||
| 216 | +- Manager (new) | ||||||||||||||||||||||||||||||||
| 217 | |- Worker [1] | ||||||||||||||||||||||||||||||||
| 218 | |- Worker [2] | ||||||||||||||||||||||||||||||||
| 219 | |- Worker [3] | ||||||||||||||||||||||||||||||||
| 220 | +- Worker [4] | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | The new manager will automatically send a L</"QUIT"> signal to the old manager and take over serving requests after | ||||||||||||||||||||||||||||||||
| 223 | starting up successfully. | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | =head1 WORKER SIGNALS | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | L<Mojo::Server::Hypnotoad> worker processes can be controlled at runtime with the following signals. | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | =head2 QUIT | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | Stop worker gracefully. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =head1 SETTINGS | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | L<Mojo::Server::Hypnotoad> can be configured with the following settings, see | ||||||||||||||||||||||||||||||||
| 236 | L<Mojolicious::Guides::Cookbook/"Hypnotoad"> for examples. | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | =head2 accepts | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | accepts => 100 | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | Maximum number of connections a worker is allowed to accept, before stopping gracefully and then getting replaced with | ||||||||||||||||||||||||||||||||
| 243 | a newly started worker, defaults to the value of L<Mojo::Server::Prefork/"accepts">. Setting the value to C<0> will | ||||||||||||||||||||||||||||||||
| 244 | allow workers to accept new connections indefinitely. Note that up to half of this value can be subtracted randomly to | ||||||||||||||||||||||||||||||||
| 245 | improve load balancing, and to make sure that not all workers restart at the same time. | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =head2 backlog | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | backlog => 128 | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | Listen backlog size, defaults to the value of L<Mojo::Server::Daemon/"backlog">. | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | =head2 clients | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | clients => 100 | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | Maximum number of accepted connections each worker process is allowed to handle concurrently, before stopping to accept | ||||||||||||||||||||||||||||||||
| 258 | new incoming connections, defaults to the value of L<Mojo::IOLoop/"max_connections">. Note that high concurrency works | ||||||||||||||||||||||||||||||||
| 259 | best with applications that perform mostly non-blocking operations, to optimize for blocking operations you can | ||||||||||||||||||||||||||||||||
| 260 | decrease this value and increase L</"workers"> instead for better performance. | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | =head2 graceful_timeout | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | graceful_timeout => 15 | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | Maximum amount of time in seconds stopping a worker gracefully may take before being forced, defaults to the value of | ||||||||||||||||||||||||||||||||
| 267 | L<Mojo::Server::Prefork/"graceful_timeout">. Note that this value should usually be a little larger than the maximum | ||||||||||||||||||||||||||||||||
| 268 | amount of time you expect any one request to take. | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | =head2 heartbeat_interval | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | heartbeat_interval => 3 | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | Heartbeat interval in seconds, defaults to the value of L<Mojo::Server::Prefork/"heartbeat_interval">. | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | =head2 heartbeat_timeout | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | heartbeat_timeout => 2 | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | Maximum amount of time in seconds before a worker without a heartbeat will be stopped gracefully, defaults to the value | ||||||||||||||||||||||||||||||||
| 281 | of L<Mojo::Server::Prefork/"heartbeat_timeout">. Note that this value should usually be a little larger than the | ||||||||||||||||||||||||||||||||
| 282 | maximum amount of time you expect any one operation to block the event loop. | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | =head2 inactivity_timeout | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | inactivity_timeout => 10 | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | Maximum amount of time in seconds a connection with an active request can be inactive before getting closed, defaults | ||||||||||||||||||||||||||||||||
| 289 | to the value of L<Mojo::Server::Daemon/"inactivity_timeout">. Setting the value to C<0> will allow connections to be | ||||||||||||||||||||||||||||||||
| 290 | inactive indefinitely. | ||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | =head2 keep_alive_timeout | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | keep_alive_timeout => 10 | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | Maximum amount of time in seconds a connection without an active request can be inactive before getting closed, | ||||||||||||||||||||||||||||||||
| 297 | defaults to the value of L<Mojo::Server::Daemon/"keep_alive_timeout">. Setting the value to C<0> will allow connections | ||||||||||||||||||||||||||||||||
| 298 | to be inactive indefinitely. | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | =head2 listen | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | listen => ['http://*:80'] | ||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | Array reference with one or more locations to listen on, defaults to C<http://*:8080>. See also | ||||||||||||||||||||||||||||||||
| 305 | L<Mojo::Server::Daemon/"listen"> for more examples. | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | =head2 pid_file | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | pid_file => '/var/run/hypnotoad.pid' | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | Full path to process id file, defaults to C<hypnotoad.pid> in the same directory as the application. Note that this | ||||||||||||||||||||||||||||||||
| 312 | value can only be changed after the server has been stopped. | ||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | =head2 proxy | ||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | proxy => 1 | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | Activate reverse proxy support, which allows for the C<X-Forwarded-For> and C<X-Forwarded-Proto> headers to be picked | ||||||||||||||||||||||||||||||||
| 319 | up automatically, defaults to the value of L<Mojo::Server/"reverse_proxy">. | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | =head2 requests | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | requests => 50 | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | Number of keep-alive requests per connection, defaults to the value of L<Mojo::Server::Daemon/"max_requests">. | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | =head2 spare | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | spare => 4 | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | Temporarily spawn up to this number of additional workers if there is a need, defaults to the value of | ||||||||||||||||||||||||||||||||
| 332 | L<Mojo::Server::Prefork/"spare">. This allows for new workers to be started while old ones are still shutting down | ||||||||||||||||||||||||||||||||
| 333 | gracefully, drastically reducing the performance cost of worker restarts. | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | =head2 trusted_proxies | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | trusted_proxies => ['10.0.0.0/8', '127.0.0.1', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7'] | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | Trusted reverse proxies, addresses or networks in CIDR form. | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | =head2 upgrade_timeout | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | upgrade_timeout => 45 | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | Maximum amount of time in seconds a zero downtime software upgrade may take before getting canceled, defaults to | ||||||||||||||||||||||||||||||||
| 346 | C<180>. | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | =head2 workers | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | workers => 10 | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | Number of worker processes, defaults to the value of L<Mojo::Server::Prefork/"workers">. A good rule of thumb is two | ||||||||||||||||||||||||||||||||
| 353 | worker processes per CPU core for applications that perform mostly non-blocking operations, blocking operations often | ||||||||||||||||||||||||||||||||
| 354 | require more and benefit from decreasing concurrency with L</"clients"> (often as low as C<1>). Note that during zero | ||||||||||||||||||||||||||||||||
| 355 | downtime software upgrades there will be twice as many workers active for a short amount of time. | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | L<Mojo::Server::Hypnotoad> implements the following attributes. | ||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | =head2 prefork | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | my $prefork = $hypnotoad->prefork; | ||||||||||||||||||||||||||||||||
| 364 | $hypnotoad = $hypnotoad->prefork(Mojo::Server::Prefork->new); | ||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||
| 366 | L<Mojo::Server::Prefork> object this server manages. | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | =head2 upgrade_timeout | ||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | my $timeout = $hypnotoad->upgrade_timeout; | ||||||||||||||||||||||||||||||||
| 371 | $hypnotoad = $hypnotoad->upgrade_timeout(15); | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | Maximum amount of time in seconds a zero downtime software upgrade may take before getting canceled, defaults to | ||||||||||||||||||||||||||||||||
| 374 | C<180>. | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | L<Mojo::Server::Hypnotoad> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | =head2 configure | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | $hypnotoad->configure('hypnotoad'); | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | Configure server from application settings. | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | =head2 run | ||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | $hypnotoad->run('script/my_app'); | ||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | Run server for application and wait for L</"MANAGER SIGNALS">. | ||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||
| 392 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||
| 394 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||
| 396 | =cut | ||||||||||||||||||||||||||||||||