DaemonControl.pm

filter: Statementstmt 31.4% 55 / 175 Branchbran 12.2% 10 / 82 Conditioncond 24.4% 12 / 49 MC/DCmcdc 9.3% 3 / 32 Subroutinesub 45.7% 16 / 35 Podpod 100% 5 / 5 totaltotal 26.7% 101 / 378 CC 61 SCAR 45.4
CC
61
Cov
25%
_manage
380.0
_read_heartbeat
90.0
_spawn
72.0
CRAP 94.2
↑ Summary DaemonControl/Worker.pm »
1 package Mojo::Server::DaemonControl;
2 7 ▶use Mojo::Base 'Mojo::EventEmitter', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Mojo::File qw(curfile path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mojo::Server::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mojo::Util qw(steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Mojolicious;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our $VERSION = '0.07';
14  
15 # This should be considered internal for now
16 7 our $MOJODCTL = do {
17 7 ▶ my $x = $0 =~ m!\bmojodctl$! && -x $0 ? $0 : $ENV{MOJODCTL_BINARY};
Branch: 1 branch
BranchTrueFalseTotal
$0 =~ /\bmojodctl$/u && -x $0 ? : 0 7 7
Condition33%1/3
$0 =~ /\bmojodctl$/u and -x $0
!ll&&!rl&&r
700
MC/DC0%0/2
$0 =~ /\bmojodctl$/u and -x $0
$0 =~ /\bmojodctl$/u -x $0
Truth table33%1/3
A and B
A= $0 =~ /\bmojodctl$/uB= -x $0
ABresult
0X0
100
111
18 7 ▶ $x ||= curfile->dirname->dirname->dirname->dirname->child(qw(script mojodctl));
Condition33%1/3
$x ||= curfile()->dirname->dirname->dirname->dirname->child("script", "mojodctl")
l!l&&r!l&&!r
070
MC/DC0%0/2
$x ||= curfile()->dirname->dirname->dirname->dirname->child("script", "mojodctl")
$x curfile()->dirname->dirname->dirname->dirname->child("script", "mojodctl")
Truth table33%1/3
A ||= B
A= $xB= curfile()->dirname->dirname->dirname->dirname->child("script", "mojodctl")
ABresult
1X1
011
000
19 7 ▶ -x $x ? $x : 'mojodctl';
Branch: 1 branch
BranchTrueFalseTotal
-x $x ? : 7 0 7
20 };
21  
22 7 has cleanup => 0;
23 7 has graceful_timeout => 120;
24 7 has heartbeat_interval => 5;
25 7 has heartbeat_timeout => 50;
26 1 ▶has listen => sub ($self) { [split /,/, $ENV{MOJO_LISTEN} || 'http://*:8080'] };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$ENV{'MOJO_LISTEN'} || 'http://*:8080'
l!l
01
MC/DC0%0/1
$ENV{'MOJO_LISTEN'} || 'http://*:8080'
$ENV{'MOJO_LISTEN'}
Truth table50%1/2
A
A= $ENV{'MOJO_LISTEN'}
Aresult
11
00
27 1 ▶has log => sub ($self) { $self->_build_log };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶has pid_file => sub ($self) { path(File::Spec->tmpdir, path($0)->basename . '.pid') };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 7 has workers => 4;
30  
31 0 ▶has _listen_handles => sub ($self) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
32 0 my $daemon = Mojo::Server::Daemon->new(listen => $self->listen, silent => 1);
33 0 my $loop = $daemon->start->ioloop;
34 0 my @fh = map { $loop->acceptor($_)->handle } @{$daemon->acceptors};
35 0 ▶ die "Did you forget to specify listen addresses?" unless @fh;
Branch: 1 branch
BranchTrueFalseTotal
unless @fh 0 0 0
36 0 return \@fh;
37 7 };
38  
39 2 ▶sub check_pid ($self) {
Subroutine check_pid: called CC=5 SCAR=18.9
Pod documented
40 2 ▶ return 0 unless my $pid = -r $self->pid_file && $self->pid_file->slurp;
Branch: 1 branch
BranchTrueFalseTotal
unless my $pid = -r $self->pid_file && $self->pid_file->slurp 1 1 2
Condition66%2/3
-r $self->pid_file && $self->pid_file->slurp
!ll&&!rl&&r
011
MC/DC50%1/2
-r $self->pid_file && $self->pid_file->slurp
-r $self->pid_file $self->pid_file->slurp
Truth table66%2/3
A && B
A= -r $self->pid_fileB= $self->pid_file->slurp
ABresult
0X0
100
111
41 1 chomp $pid;
42 1 ▶ return $pid if $pid && kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid and kill 0, $pid 1 0 1
Condition33%1/3
$pid and kill 0, $pid
!ll&&!rl&&r
001
MC/DC0%0/2
$pid and kill 0, $pid
$pid kill 0, $pid
Truth table33%1/3
A and B
A= $pidB= kill 0, $pid
ABresult
0X0
100
111
43 0 $self->pid_file->remove;
44 0 return 0;
45 }
46  
47 1 ▶sub ensure_pid_file ($self, $pid) {
Subroutine ensure_pid_file: called CC=3 SCAR=12.5
Pod documented
48 1 ▶ return $self if -s (my $file = $self->pid_file);
Branch: 1 branch
BranchTrueFalseTotal
if -s (my $file = $self->pid_file) 0 1 1
49 1 $self->log->info("Writing pid $pid to $file");
50 1 ▶ return $file->spurt("$pid\n")->chmod(0644) && $self;
Condition33%1/3
$file->spurt("$pid\n")->chmod(420) && $self
!ll&&!rl&&r
001
MC/DC0%0/2
$file->spurt("$pid\n")->chmod(420) && $self
$file->spurt("$pid\n")->chmod(420) $self
Truth table33%1/3
A && B
A= $file->spurt("$pid\n")->chmod(420)B= $self
ABresult
0X0
100
111
51 }
52  
53 0 ▶sub reload ($self, $app) {
Subroutine reload: not called CC=3 SCAR=24.8
Pod documented
54 0 ▶ return _errno(3) unless my $pid = $self->check_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless my $pid = $self->check_pid 0 0 0
55 0 $self->log->info("Starting hot deployment of $pid");
56 0 ▶ return kill(USR2 => $pid) ? _errno(0) : _errno(1);
Branch: 1 branch
BranchTrueFalseTotal
kill('USR2', $pid) ? : 0 0 0
57 }
58  
59 0 ▶sub run ($self, $app) {
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
60  
61 # Cannot run two daemons at the same time
62 0 ▶ if (my $pid = $self->check_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->check_pid) 0 0 0
63 0 $self->log->info("Manager for $app is already running ($pid)");
64 0 return _errno(16);
65 }
66  
67 # Listen for signals and start the daemon
68 0 ▶ local $SIG{CHLD} = sub { $self->_waitpid };
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 ▶ local $SIG{INT} = sub { $self->stop('INT') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 ▶ local $SIG{QUIT} = sub { $self->stop('QUIT') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ local $SIG{TERM} = sub { $self->stop('TERM') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 ▶ local $SIG{TTIN} = sub { $self->workers($self->workers + 1) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ local $SIG{TTOU} = sub { $self->workers($self->workers - 1) if $self->workers > 0 };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->workers > 0 0 0 0
74 0 ▶ local $SIG{USR2} = sub { $self->_start_hot_deployment };
Subroutine __ANON__: not called CC=1 SCAR=6.9
75  
76 0 $self->cleanup(1)->ensure_pid_file($self->{pid} = $$);
77 0 $self->_create_worker_read_write_pipe;
78 0 $self->_listen_handles;
79  
80 0 @$self{qw(pid pool running)} = ($$, {}, 1);
81 0 $self->emit('start');
82 0 $self->log->info("Manager for $app started");
83 0 ▶ $self->_manage($app) while $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"running"} and do { $self->_manage($app) } 0 0 0
84 0 $self->log->info("Manager for $app stopped");
85 0 $self->cleanup(0)->pid_file->remove;
86 0 return _errno(0);
87 }
88  
89 0 ▶sub stop ($self, $signal = 'TERM') {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
90 0 $self->{stop_signal} = $signal;
91 0 $self->log->info("Manager will stop workers with signal $signal");
92 0 return $self->emit(stop => $signal);
93 }
94  
95 1 ▶sub _build_log ($self) {
Subroutine _build_log: called CC=4 SCAR=16.9
96 my $level = $ENV{MOJO_LOG_LEVEL}
97 1 ▶ || ($ENV{HARNESS_IS_VERBOSE} ? 'debug' : $ENV{HARNESS_ACTIVE} ? 'error' : 'info');
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'HARNESS_IS_VERBOSE'} ? : 0 1 1
$ENV{'HARNESS_ACTIVE'} ? : 1 0 1
Condition33%1/3
$ENV{'MOJO_LOG_LEVEL'} || ($ENV{'HARNESS_IS_VERBOSE'} ? 'debug' : ($ENV{'HARNESS_ACTIVE'} ? 'error' : 'info'))
l!l&&r!l&&!r
010
MC/DC0%0/2
$ENV{'MOJO_LOG_LEVEL'} || ($ENV{'HARNESS_IS_VERBOSE'} ? 'debug' : ($ENV{'HARNESS_ACTIVE'} ? 'error' : 'info'))
$ENV{'MOJO_LOG_LEVEL'} ($ENV{'HARNESS_IS_VERBOSE'} ? 'debug' : ($ENV{'HARNESS_ACTIVE'} ? 'error' : 'info'))
Truth table33%1/3
A || B
A= $ENV{'MOJO_LOG_LEVEL'}B= ($ENV{'HARNESS_IS_VERBOSE'} ? 'debug' : ($ENV{'HARNESS_ACTIVE'} ? 'error' : 'info'))
ABresult
1X1
011
000
98 1 return Mojo::Log->new(level => $level);
99 }
100  
101 0 ▶sub _create_worker_read_write_pipe ($self) {
Subroutine _create_worker_read_write_pipe: not called CC=3 SCAR=24.8
102 0 ▶ return if $self->{worker_read};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"worker_read"} 0 0 0
103 0 ▶ pipe $self->{worker_read}, $self->{worker_write} or die "pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $self->{"worker_read"}, $self->{"worker_write"} or die "pipe: $!" 0 0 0
104 }
105  
106 0 ▶sub _errno ($n) { $! = $n }
Subroutine _errno: not called CC=1 SCAR=6.9
107  
108 0 ▶sub _kill ($self, $signal, $w, $level, $reason) {
Subroutine _kill: not called CC=3 SCAR=24.8
109 0 ▶ return if $w->{$signal};
Branch: 1 branch
BranchTrueFalseTotal
if $w->{$signal} 0 0 0
110 0 ▶ $w->{$signal} = kill($signal, $w->{pid}) || 0;
Condition0%0/2
kill($signal, $w->{'pid'}) || 0
l!l
00
MC/DC0%0/1
kill($signal, $w->{'pid'}) || 0
kill($signal, $w->{'pid'})
Truth table0%0/2
A
A= kill($signal, $w->{'pid'})
Aresult
11
00
111 0 $self->log->$level("$reason (signalled=$w->{$signal})");
112 }
113  
114 0 ▶sub _manage ($self, $app) {
Subroutine _manage: not called CC=19 SCAR=59.4
115  
116 # Make sure we have a PID file and get status from workers
117 0 $self->ensure_pid_file($self->{pid});
118 0 $self->_read_heartbeat;
119  
120 # Stop workers and eventually manager
121 0 my $pool = $self->{pool};
122 0 ▶ if (my $signal = $self->{stop_signal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $signal = $self->{"stop_signal"}) 0 0 0
123 0 ▶ return delete @$self{qw(running stop_signal)} unless keys %$pool; # Fully stopped
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$pool 0 0 0
124 0 ▶ return map { $_->{$signal} ||= kill $signal, $_->{pid} } values %$pool;
Condition0%0/3
$_->{$signal} ||= kill($signal, $_->{"pid"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{$signal} ||= kill($signal, $_->{"pid"})
$_->{$signal} kill($signal, $_->{"pid"})
Truth table0%0/3
A ||= B
A= $_->{$signal}B= kill($signal, $_->{"pid"})
ABresult
1X1
011
000
125 }
126  
127 # Decrease workers on SIGTTOU
128 0 my $time = steady_time;
129 0 my @maybe_stop = grep { !$_->{graceful} } values %$pool;
130 0 $_->{graceful} = $time for splice @maybe_stop, $self->workers;
131  
132 # Figure out worker health
133 0 my $ht = $self->heartbeat_timeout;
134 0 my (@graceful, @healthy, @starting);
135 0 for my $pid (sort keys %$pool) {
136 0 ▶ my $w = $pool->{$pid} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $w = $pool->{$pid} or next 0 0 0
137 0 ▶ if ($w->{graceful}) { push @graceful, $pid }
Branch: 3 branches
BranchTrueFalseTotal
if ($w->{'graceful'}) { } 0 0 0
elsif (not $w->{'time'}) { } 0 0 0
elsif ($w->{'time'} + $ht <= $time) { } 0 0 0
138 0 elsif (!$w->{time}) { push @starting, $pid }
139 0 ▶ elsif ($w->{time} + $ht <= $time) { $w->{graceful} //= $time; push @graceful, $pid }
Condition0%0/3
$w->{"graceful"} //= $time
l!l&&r!l&&!r
000
MC/DC0%0/2
$w->{"graceful"} //= $time
$w->{"graceful"} $time
Truth table0%0/3
A //= B
A= $w->{"graceful"}B= $time
ABresult
1X1
011
000
140 0 else { push @healthy, $pid }
141 }
142  
143 # Start or stop workers based on worker health
144 0 my $n_missing = $self->workers - (@healthy + @starting);
145 0 ▶ if ($n_missing > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n_missing > 0) { } 0 0 0
elsif (not @starting) { } 0 0 0
146 0 $self->log->info("Manager starting $n_missing workers");
147 0 ▶ $self->_spawn($app) while !$self->{stop_signal} && $n_missing-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
not $self->{"stop_signal"} and $n_missing-- > 0 and do { $self->_spawn($app) } 0 0 0
Condition0%0/3
not $self->{"stop_signal"} and $n_missing-- > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"stop_signal"} and $n_missing-- > 0
not $self->{"stop_signal"} $n_missing-- > 0
Truth table0%0/3
not(A) and B
A= not $self->{"stop_signal"}B= $n_missing-- > 0
ABresult
0X0
100
111
148 }
149 elsif (!@starting) {
150 0 my $gt = $self->graceful_timeout;
151 0 for my $pid (@graceful) {
152 0 ▶ next unless my $w = $pool->{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless my $w = $pool->{$pid} 0 0 0
153 0 ▶ if ($gt && $w->{graceful} + $gt < $time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gt and $w->{'graceful'} + $gt < $time) { } 0 0 0
Condition0%0/3
$gt and $w->{"graceful"} + $gt < $time
!ll&&!rl&&r
000
MC/DC0%0/2
$gt and $w->{"graceful"} + $gt < $time
$gt $w->{"graceful"} + $gt < $time
Truth table0%0/3
A and B
A= $gtB= $w->{"graceful"} + $gt < $time
ABresult
0X0
100
111
154 0 $self->_kill(KILL => $w, warn => "Stopping worker $pid immediately");
155 }
156 else {
157 0 $self->_kill(QUIT => $w, info => "Stopping worker $pid gracefully ($gt seconds)");
158 }
159 }
160 }
161 }
162  
163 0 ▶sub _read_heartbeat ($self) {
Subroutine _read_heartbeat: not called CC=9 SCAR=45.0
164 0 ▶ my $select = $self->{select} ||= IO::Select->new($self->{worker_read});
Condition0%0/3
$self->{"select"} ||= "IO::Select"->new($self->{"worker_read"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"select"} ||= "IO::Select"->new($self->{"worker_read"})
$self->{"select"} "IO::Select"->new($self->{"worker_read"})
Truth table0%0/3
A ||= B
A= $self->{"select"}B= "IO::Select"->new($self->{"worker_read"})
ABresult
1X1
011
000
165 0 ▶ return unless $select->can_read(0.1);
Branch: 1 branch
BranchTrueFalseTotal
unless $select->can_read("0.1") 0 0 0
166 0 ▶ return unless $self->{worker_read}->sysread(my $chunk, 4194304);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"worker_read"}->sysread(my $chunk, 4194304) 0 0 0
167  
168 0 my $time = steady_time;
169 0 ▶ while ($chunk =~ s/mojodctl:\d+:(\d+):(\w)\n//mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ s/mojodctl:\d+:(\d+):(\w)\n//gmu) 0 0 0
170 0 ▶ next unless my $w = $self->{pool}{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless my $w = $self->{"pool"}{$1} 0 0 0
171 0 ▶ ($w->{killed} = $time), $self->log->fatal("Worker $w->{pid} force killed") if $2 eq 'k';
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq "k" 0 0 0
172 0 ▶ $w->{graceful} ||= $time if $2 eq 'g';
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq "g" 0 0 0
Condition0%0/3
$w->{"graceful"} ||= $time
l!l&&r!l&&!r
000
MC/DC0%0/2
$w->{"graceful"} ||= $time
$w->{"graceful"} $time
Truth table0%0/3
A ||= B
A= $w->{"graceful"}B= $time
ABresult
1X1
011
000
173 0 $w->{time} = $time;
174 0 $self->emit(heartbeat => $w);
175 }
176 }
177  
178 0 ▶sub _spawn ($self, $app) {
Subroutine _spawn: not called CC=8 SCAR=42.8
179  
180 # Parent
181 0 ▶ die "Can't fork: $!" unless defined(my $pid = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $pid = fork) 0 0 0
182 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
183 0 my $w = $self->{pool}{$pid} = {pid => $pid, time => steady_time};
184 0 $self->log->info("Worker $pid started");
185 0 return $self->emit(spawn => $w);
186 }
187  
188 # Child
189 0 ▶ $ENV{MOJO_LOG_LEVEL} ||= $self->log->level;
Condition0%0/3
$ENV{"MOJO_LOG_LEVEL"} ||= $self->log->level
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"MOJO_LOG_LEVEL"} ||= $self->log->level
$ENV{"MOJO_LOG_LEVEL"} $self->log->level
Truth table0%0/3
A ||= B
A= $ENV{"MOJO_LOG_LEVEL"}B= $self->log->level
ABresult
1X1
011
000
190 0 $ENV{MOJODCTL_CONTROL_CLASS} = 'Mojo::Server::DaemonControl::Worker';
191 0 $ENV{MOJODCTL_HEARTBEAT_FD} = fileno $self->{worker_write};
192 0 $ENV{MOJODCTL_HEARTBEAT_INTERVAL} = $self->heartbeat_interval;
193  
194 0 my (@urls, @args) = @{$self->listen};
195 0 for my $fh ($self->{worker_write}, @{$self->_listen_handles}) {
196  
197 # Remove close-on-exec flag
198 # https://stackoverflow.com/questions/14351147/perl-passing-an-open-socket-across-fork-exec
199 0 ▶ my $flags = fcntl $fh, F_GETFD, 0 or die "fcntl F_GETFD: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($fh, 1, 0) or die "fcntl F_GETFD: $!" 0 0 0
200 0 ▶ fcntl $fh, F_SETFD, $flags & ~FD_CLOEXEC or die "fcntl F_SETFD: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 2, $flags & '18446744073709551614' or die "fcntl F_SETFD: $!" 0 0 0
201  
202 0 ▶ next if $fh eq $self->{worker_write};
Branch: 1 branch
BranchTrueFalseTotal
if $fh eq $self->{"worker_write"} 0 0 0
203 0 my $url = Mojo::URL->new(shift @urls);
204 0 $url->query->param(fd => fileno $fh);
205 0 push @args, -l => $url->to_string;
206 }
207  
208 0 $self->log->debug("Starting $^X $MOJODCTL $app daemon @args");
209 0 exec $^X, $MOJODCTL => $app => daemon => @args;
210 0 die "Could not exec $app: $!";
211 }
212  
213 0 ▶sub _start_hot_deployment ($self) {
Subroutine _start_hot_deployment: not called CC=2 SCAR=17.9
214 0 $self->log->info('Starting hot deployment.');
215 0 my $time = steady_time;
216 0 $_->{graceful} = $time for values %{$self->{pool}};
217 }
218  
219 0 ▶sub _waitpid ($self) {
Subroutine _waitpid: not called CC=3 SCAR=24.8
220 0 ▶ while ((my $pid = waitpid -1, WNOHANG) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = waitpid(-1, 1)) > 0) 0 0 0
221 0 ▶ next unless my $w = delete $self->{pool}{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless my $w = delete $self->{"pool"}{$pid} 0 0 0
222 0 $self->log->info("Worker $pid stopped");
223 0 $self->emit(reap => $w);
224 }
225 }
226  
227 2 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=4 SCAR=14.2
228 2 ▶ my $path = $self->cleanup && $self->pid_file;
Condition66%2/3
$self->cleanup && $self->pid_file
!ll&&!rl&&r
101
MC/DC50%1/2
$self->cleanup && $self->pid_file
$self->cleanup $self->pid_file
Truth table66%2/3
A && B
A= $self->cleanupB= $self->pid_file
ABresult
0X0
100
111
229 2 ▶ $path->remove if $path and -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path and -e $path 1 1 2
Condition66%2/3
$path and -e $path
!ll&&!rl&&r
101
MC/DC50%1/2
$path and -e $path
$path -e $path
Truth table66%2/3
A and B
A= $pathB= -e $path
ABresult
0X0
100
111
230 }
231  
232 7 1;
233  
234 =encoding utf8
235  
236 =head1 NAME
237  
238 Mojo::Server::DaemonControl - A Mojolicious daemon manager
239  
240 =head1 SYNOPSIS
241  
242 =head2 Commmand line
243  
244 # Start the manager
245 $ mojodctl -l 'http://*:8080' -P /tmp/myapp.pid -w 4 /path/to/myapp.pl;
246  
247 # Reload the manager
248 $ mojodctl -R -P /tmp/myapp.pid /path/to/myapp.pl;
249  
250 # For more options
251 $ mojodctl --help
252  
253 =head2 Perl API
254  
255 use Mojo::Server::DaemonControl;
256 my $dctl = Mojo::Server::DaemonControl->new(listen => ['http://*:8080'], workers => 4);
257 $dctl->run('/path/to/my-mojo-app.pl');
258  
259 =head2 Mojolicious application
260  
261 It is possible to use the L<Mojolicious/before_server_start> hook to change
262 server settings. The C<$app> is also available, meaning the values can be read
263 from a config file. See L<Mojo::Server::DaemonControl::Worker> and
264 L<Mojo::Server::Daemon> for more information about what to tweak.
265  
266 use Mojolicious::Lite -signatures;
267  
268 app->hook(before_server_start => sub ($server, $app) {
269 if ($sever->isa('Mojo::Server::DaemonControl::Worker')) {
270 $server->inactivity_timeout(60);
271 $server->max_clients(100);
272 $server->max_requests(10);
273 }
274 });
275  
276 =head1 DESCRIPTION
277  
278 L<Mojo::Server::DaemonControl> is not a web server. Instead it manages one or
279 more L<Mojo::Server::Daemon> processes that can handle web requests.
280  
281 This server is an alternative to L<Mojo::Server::Hypnotoad> where each of the
282 workers handle long running (WebSocket) requests. The main difference is that a
283 hot deploy will simply start new workers, instead of restarting the manager.
284 This is useful if you need/want to deploy a new version of your server during
285 the L</graceful_timeout>. Normally this is not something you would need, but in
286 some cases where the graceful timeout and long running requests last for
287 several hours or even days, then it might come in handy to let the old
288 code run, while new processes are deployed.
289  
290 Note that L<Mojo::Server::DaemonControl> is currently EXPERIMENTAL and it has
291 not been tested in production yet. Feedback is more than welcome.
292  
293 =head1 SIGNALS
294  
295 =head2 INT, TERM
296  
297 Shut down server immediately.
298  
299 =head2 QUIT
300  
301 Shut down server gracefully.
302  
303 =head2 TTIN
304  
305 Increase worker pool by one.
306  
307 =head2 TTOU
308  
309 Decrease worker pool by one.
310  
311 =head2 USR2
312  
313 Will prevent existing workers from accepting new connections and eventually
314 stop them, and start new workers in a fresh environment that handles the new
315 connections. The manager process will remain the same.
316  
317 $ mojodctl
318 |- myapp.pl-1647405707
319 |- myapp.pl-1647405707
320 |- myapp.pl-1647405707
321 |- myapp.pl
322 |- myapp.pl
323 '- myapp.pl
324  
325 EXPERIMENTAL: The workers that waits to be stopped will have a timestamp
326 appended to C<$0> to illustrate which is new and which is old.
327  
328 =head1 ATTRIBUTES
329  
330 L<Mojo::Server::DaemonControl> inherits all attributes from
331 L<Mojo::EventEmitter> and implements the following ones.
332  
333 =head2 cleanup
334  
335 $bool = $dctl->cleanup;
336 $dctl = $dctl->cleanup(1);
337  
338 Set this to true and L<Mojo::Server::DaemonControl> will remove L</pid_file>
339 when this object goes out of scope.
340  
341 =head2 graceful_timeout
342  
343 $timeout = $dctl->graceful_timeout;
344 $dctl = $dctl->graceful_timeout(120);
345  
346 A worker will be forced stopped if it could not be gracefully stopped after
347 this amount of time.
348  
349 =head2 heartbeat_interval
350  
351 $num = $dctl->heartbeat_interval;
352 $dctl = $dctl->heartbeat_interval(5);
353  
354 Heartbeat interval in seconds. This value is passed on to
355 L<Mojo::Server::DaemonControl::Worker/heartbeat_interval>.
356  
357 =head2 heartbeat_timeout
358  
359 $num = $dctl->heartbeat_timeout;
360 $dctl = $dctl->heartbeat_timeout(120);
361  
362 A worker will be stopped gracefully if a heartbeat has not been seen within
363 this amount of time.
364  
365 =head2 listen
366  
367 $array_ref = $dctl->listen;
368 $dctl = $dctl->listen(['http://127.0.0.1:3000']);
369  
370 Array reference with one or more locations to listen on.
371 See L<Mojo::Server::Daemon/listen> for more details.
372  
373 =head2 log
374  
375 $log = $dctl->log;
376 $dctl = $dctl->log(Mojo::Log->new);
377  
378 A L<Mojo::Log> object used for logging.
379  
380 =head2 pid_file
381  
382 $file = $dctl->pid_file;
383 $dctl = $dctl->pid_file(Mojo::File->new);
384  
385 A L<Mojo::File> object with the path to the pid file.
386  
387 Note that the PID file must end with ".pid"! Default path is "mojodctl.pid" in
388 L<File::Spec/tmpdir>.
389  
390 =head2 workers
391  
392 $int = $dctl->workers;
393 $dctl = $dctl->workers(4);
394  
395 Number of worker processes, defaults to 4. See L<Mojo::Server::Prefork/workers>
396 for more details.
397  
398 =head1 METHODS
399  
400 L<Mojo::Server::DaemonControl> inherits all methods from
401 L<Mojo::EventEmitter> and implements the following ones.
402  
403 =head2 check_pid
404  
405 $int = $dctl->check_pid;
406  
407 Returns the PID of the running process documented in L</pid_file> or zero (0)
408 if it is not running.
409  
410 =head2 ensure_pid_file
411  
412 $dctl->ensure_pid_file($pid);
413  
414 Makes sure L</pid_file> exists and contains the current PID.
415  
416 =head2 reload
417  
418 $int = $dctl->reload($app);
419  
420 Tries to reload a running instance by sending L</USR2> to L</pid_file>.
421  
422 =head2 run
423  
424 $int = $dctl->run($app);
425  
426 Run the menager and wait for L</SIGNALS>. Note that C<$app> is not loaded in
427 the manager process, which means that each worker does not share any code or
428 memory.
429  
430 =head2 stop
431  
432 $dctl->stop($signal);
433  
434 Used to stop the running manager and any L</workers> with the C<$signal> INT,
435 QUIT or TERM (default).
436  
437 =head1 AUTHOR
438  
439 Jan Henning Thorsen
440  
441 =head1 COPYRIGHT AND LICENSE
442  
443 Copyright (C) Jan Henning Thorsen
444  
445 This program is free software, you can redistribute it and/or modify it under
446 the terms of the Artistic License version 2.0.
447  
448 =head1 SEE ALSO
449  
450 L<Mojo::Server::Daemon>, L<Mojo::Server::Hypnotoad>,
451 L<Mojo::Server::DaemonControl::Worker>.
452  
453 =cut
↑ Summary DaemonControl/Worker.pm »