blib/lib/Daemon/Control/Plugin/HotStandby.pm

filter: Statementstmt 81.0% 60 / 74 Branchbran 41.6% 15 / 36 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 64.9% 87 / 134 CC 24 SCAR 21.5
CC
24
Cov
66%
__ANON__
16.0
__ANON__
8.4
__ANON__
7.5
CRAP 8.6
↑ Summary
1 package Daemon::Control::Plugin::HotStandby;
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Class::Method::Modifiers qw/fresh/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = '0.0001';
8  
9 =head2 NAME
10  
11 Daemon::Control::Plugin::HotStandby
12  
13 =head2 DESCRIPTION
14  
15 This is a plugin basically for PSGI workers so that a standby worker
16 can be spun up prior to terminating the original worker.
17  
18 =head2 USAGE
19  
20 Daemon::Control->with_plugins('HotStandby')->new({ ... });
21  
22 =head2 NOTES and CAUTIONS
23  
24 This is not a particularly smart hot standby daemon. It uses double
25 the value of $self->kill_timeout to work out how long to wait before
26 killing the original process, after bringing its hot standby up.
27 L<HADaemon::Control> does something smarter, but it has the
28 disadvantage of being based on a forked, older version of
29 L<Daemon::Control>, and doesn't ship with any tests. Hopefully one day
30 there will be a Daemon::Control::Plugin::HighAvailability that deals
31 with these problems, but for now this is a reasonable solution. Just
32 test it thoroughly with your kit before you send it out into the wild.
33  
34 Until we work out and optimise what needs to be factored out into
35 separate utility subroutines in L<Daemon::Control>, this module contains
36 far more code than is needed (copy/paste/refactor from the parent
37 module). Also it might break depending on future releases post
38 version 0.001007 of L<Daemon::Control>.
39  
40 =head2 LICENCE
41  
42 This code can be distributed under the same terms as perl itself.
43  
44 =head2 AUTHOR
45  
46 Kieren Diment <zarquon@cpan.org>
47  
48 =cut
49  
50  
51 around do_restart => sub {
52 1 ▶ my $orig = shift;
Subroutine __ANON__: called CC=6 SCAR=20.1
53 1 my ($self) = @_;
54  
55 # check old running
56 1 $self->read_pid;
57 1 my $old_pid = $self->pid;
58 1 ▶ if ($self->pid && $self->pid_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid and $self->pid_running) { } 1 0 1
Condition33%1/3
$self->pid and $self->pid_running
!ll&&!rl&&r
001
MC/DC0%0/2
$self->pid and $self->pid_running
$self->pid $self->pid_running
Truth table33%1/3
A and B
A= $self->pidB= $self->pid_running
ABresult
0X0
100
111
59 1 $self->pretty_print("Found existing process");
60 }
61 else { # warn if not
62 0 $self->pretty_print("No process running for hot standby zero downtime", "red");
63 }
64  
65 1 $self->_finish_start;
66 # Start new get pid.
67 1 $self->read_pid;
68 1 my $new_pid = $self->pid;
69 # check new came up. Die if failed.
70 1 sleep (($self->kill_timeout * 2) + 1);
71  
72  
73 1 ▶ return 1 unless $old_pid > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_pid > 1 0 1 1
74 1 ▶ if ( $self->pid_running($old_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_running($old_pid)) { } 1 0 1
75 1 my $failed = $self->_send_stop_signals($old_pid);
76 1 ▶ return 1 if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 1 0 1
77 } else {
78 0 $self->pretty_print( "Not Running", "red" );
79 }
80  
81 0 $self->_ensure_pid_file_exists;
82 0 return 0;
83 10 };
84  
85 fresh _finish_start => sub {
86 1 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.8
87 1 $self->_create_resource_dir;
88  
89 1 ▶ $self->fork( 2 ) unless defined $self->fork;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->fork 0 1 1
90 1 ▶ $self->_double_fork if $self->fork == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 2 1 0 1
91 1 ▶ $self->_fork if $self->fork == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 1 0 1 1
92 1 ▶ $self->_foreground if $self->fork == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 0 0 1 1
93 1 $self->pretty_print( "Started" );
94 1 return 0;
95 10 };
96  
97 fresh _send_stop_signals => sub {
98 1 ▶ my ($self, $start_pid) = @_;
Subroutine __ANON__: called CC=7 SCAR=21.3
99 SIGNAL:
100 1 foreach my $signal (@{ $self->stop_signals }) {
101 4 $self->trace( "Sending $signal signal to pid $start_pid..." );
102 4 kill $signal => $start_pid;
103
104 4 for (1..$self->kill_timeout)
105 {
106 # abort early if the process is now stopped
107 4 $self->trace("checking if pid $start_pid is still running...");
108 4 ▶ last if not $self->pid_running($start_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid_running($start_pid) 0 4 4
109 4 sleep 1;
110 }
111 4 ▶ last unless $self->pid_running($start_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid_running($start_pid) 0 4 4
112 }
113 1 ▶ if ( $ARGV[0] ne 'restart' && $self->pid_running($start_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] ne "restart" and $self->pid_running($start_pid)) 0 1 1
Condition33%1/3
$ARGV[0] ne "restart" and $self->pid_running($start_pid)
!ll&&!rl&&r
100
MC/DC0%0/2
$ARGV[0] ne "restart" and $self->pid_running($start_pid)
$ARGV[0] ne "restart" $self->pid_running($start_pid)
Truth table33%1/3
A and B
A= $ARGV[0] ne "restart"B= $self->pid_running($start_pid)
ABresult
0X0
100
111
114 0 $self->pretty_print( "Failed to Stop", "red" );
115 0 return 1;
116 }
117 1 $self->pretty_print( "Stopped" );
118 10 };
119  
120 fresh _ensure_pid_file_exists => sub {
121 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 ▶ if ( ! -f $self->pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $self->pid_file) 0 0 0
123 0 $self->pid( 0 ); # Make PID invalid.
124 0 $self->write_pid();
125 }
126 10 };
127  
128  
129  
130 # We need to nuke these methods and replace with our own until
131 # Daemon::Control supports what we want to do with them.
132  
133 around 'pid_running' => sub {
134 21 ▶ my $orig = shift;
Subroutine __ANON__: called CC=8 SCAR=27.7
135 21 my ($self, $pid) = @_;
136 21 ▶ $pid ||= $self->read_pid;
Condition66%2/3
$pid ||= $self->read_pid
l!l&&r!l&&!r
1740
MC/DC0%0/2
$pid ||= $self->read_pid
$pid $self->read_pid
Truth table66%2/3
A ||= B
A= $pidB= $self->read_pid
ABresult
1X1
011
000
137  
138 21 ▶ return 0 unless $self->pid >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid >= 1 0 21 21
139 21 ▶ return 0 unless kill 0, $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $self->pid 4 17 21
140  
141 17 ▶ if ( $self->scan_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scan_name) 0 17 17
142 0 ▶ open my $lf, "-|", "ps", "-p", $self->pid, "-o", "command="
Branch: 1 branch
BranchTrueFalseTotal
open my $lf, "-|", "ps", "-p", $self->pid, "-o", "command=" or die "Failed to get pipe to ps for scan_name." 0 0 0
143 or die "Failed to get pipe to ps for scan_name.";
144 0 ▶ while ( my $line = <$lf> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $lf)) 0 0 0
145 0 ▶ return 1 if $line =~ $self->scan_name;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ $self->scan_name 0 0 0
146 }
147 0 return 0;
148 }
149 # Scan name wasn't used, testing normal PID.
150 17 return kill 0, $self->pid;
151 10 };
152  
153  
154 10 1;
↑ Summary