| 1 | package Mojo::Server::DaemonControl::Worker; | ||
| 2 | use Mojo::Base 'Mojo::Server::Daemon', -signatures; | ||
| 3 | |||
| 4 | use Scalar::Util qw(weaken); | ||
| 5 | |||
| 6 | has heartbeat_interval => sub ($self) { $ENV{MOJODCTL_HEARTBEAT_INTERVAL} || 5 }; | ||
| 7 | has manager_pid => sub ($self) {getppid}; | ||
| 8 | has silent => 1; | ||
| 9 | |||
| 10 | sub run ($self, $app, @) { | ||
| 11 | weaken $self; | ||
| 12 | $0 = $app; | ||
| 13 | |||
| 14 | my $loop = $self->ioloop; | ||
| 15 | my $heartbeat_cb = sub { $self->_heartbeat_tick }; | ||
| 16 | $loop->next_tick($heartbeat_cb); | ||
| 17 | $self->{heartbeat_tid} ||= $loop->recurring($self->heartbeat_interval, $heartbeat_cb); | ||
| 18 | |||
| 19 | $loop->on(finish => sub { $self->max_requests(1) }); | ||
| 20 | local $SIG{QUIT} = sub { $self->_stop_gracefully }; | ||
| 21 | return $self->tap('manager_pid')->tap(load_app => $app)->SUPER::run; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _heartbeat ($self, $state) { | ||
| 25 | state $fh = IO::Handle->new_from_fd($ENV{MOJODCTL_HEARTBEAT_FD} || 1, '>') | ||
| 26 | or die "open heartbeat fd: $!"; | ||
| 27 | $fh->autoflush(1); | ||
| 28 | $fh->printf("mojodctl:%s:%s:%s\n", $self->manager_pid, $$, $state); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _heartbeat_tick ($self) { | ||
| 32 | return $self->_heartbeat('h') if $self->manager_pid eq getppid; | ||
| 33 | |||
| 34 | # Force kill when parent pid changes | ||
| 35 | $self->_heartbeat('k'); | ||
| 36 | kill KILL => $$; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _stop_gracefully ($self) { | ||
| 40 | my $loop = $self->ioloop; | ||
| 41 | $loop->remove(delete $self->{heartbeat_tid}) if $self->{heartbeat_tid}; | ||
| 42 | $loop->stop_gracefully; | ||
| 43 | $self->_heartbeat('g'); | ||
| 44 | $0 .= '-' . time; # Rename process to indicate which is going to be replaced | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; | ||
| 48 | |||
| 49 | =encoding utf8 | ||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | Mojo::Server::DaemonControl::Worker - A Mojolicious daemon that can shutdown gracefully | ||
| 54 | |||
| 55 | =head1 SYNOPSIS | ||
| 56 | |||
| 57 | use Mojo::Server::DaemonControl::Worker; | ||
| 58 | my $daemon = Mojo::Server::DaemonControl::Worrker->new(listen => ['http://*:8080']); | ||
| 59 | $daemon->run; | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | L<Mojo::Server::DaemonControl::Worker> is a sub class of | ||
| 64 | L<Mojo::Server::Daemon>, that is used by L<Mojo::Server::DaemonControl> | ||
| 65 | to support graceful shutdown and hot deployment. | ||
| 66 | |||
| 67 | =head1 SIGNALS | ||
| 68 | |||
| 69 | The L<Mojo::Server::DaemonControl::Worker> process can be controlled by the | ||
| 70 | same signals as L<Mojo::Server::Daemon>, but it also supports the following | ||
| 71 | signals. | ||
| 72 | |||
| 73 | =head2 QUIT | ||
| 74 | |||
| 75 | Used to shut down the server gracefully. | ||
| 76 | |||
| 77 | =head1 EVENTS | ||
| 78 | |||
| 79 | L<Mojo::Server::DaemonControl::Worker> inherits all events from | ||
| 80 | L<Mojo::Server::Daemon>. | ||
| 81 | |||
| 82 | =head1 ATTRIBUTES | ||
| 83 | |||
| 84 | L<Mojo::Server::DaemonControl::Worker> inherits all attributes from | ||
| 85 | L<Mojo::Server::Daemon> and implements the following ones. | ||
| 86 | |||
| 87 | =head2 heartbeat_interval | ||
| 88 | |||
| 89 | $int = $daemon->heartbeat_interval; | ||
| 90 | $daemon = $daemon->heartbeat_interval(2.5); | ||
| 91 | |||
| 92 | Heartbeat interval in seconds. See | ||
| 93 | L<Mojo::Server::DaemonControl::Worker/heartbeat_interval> for more details. | ||
| 94 | |||
| 95 | =head2 manager_pid | ||
| 96 | |||
| 97 | $int = $daemon->manager_pid; | ||
| 98 | |||
| 99 | Holds the PID of the L<Mojo::Server::DaemonControl> process. | ||
| 100 | |||
| 101 | =head2 silent | ||
| 102 | |||
| 103 | $bool = $daemon->silent; | ||
| 104 | $daemon = $daemon->silent(1); | ||
| 105 | |||
| 106 | Changes the default in L<Mojo::Server::Daemon/silent> to 1. | ||
| 107 | |||
| 108 | =head1 METHODS | ||
| 109 | |||
| 110 | L<Mojo::Server::DaemonControl::Worker> inherits all methods from | ||
| 111 | L<Mojo::Server::Daemon> and implements the following ones. | ||
| 112 | |||
| 113 | =head2 run | ||
| 114 | |||
| 115 | $daemon->run($app); | ||
| 116 | |||
| 117 | Load C<$app> using L<Mojo::Server/load_app> and run server and wait for | ||
| 118 | L</SIGNALS>. | ||
| 119 | |||
| 120 | =head1 SEE ALSO | ||
| 121 | |||
| 122 | L<Mojo::Server::DaemonControl>. | ||
| 123 | |||
| 124 | =cut |