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

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