blib/lib/IO/Async/Process/GracefulShutdown.pm

filter: Statementstmt 88.8% 32 / 36 Branchbran 66.6% 4 / 6 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 88.8% 8 / 9 Podpod 100% 4 / 4 totaltotal 80.9% 51 / 63 CC 6 SCAR 8.5
CC
6
Cov
83%
__ANON__
6.0
shutdown
2.4
finish_future
2.0
CRAP 2.3
« Build ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Process::GracefulShutdown;
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use 5.010; # //
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use base qw( IO::Async::Process );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.01';
14  
15 =head1 NAME
16  
17 C<IO::Async::Process::GracefulShutdown> - controlled shutdown of a process
18  
19 =head1 SYNOPSIS
20  
21 use IO::Async::Process::GracefulShutdown;
22  
23 use IO::Async::Loop;
24 my $loop = IO::Async::Loop;
25  
26 my $process = IO::Async::Process::GracefulShutdown->new(
27 command => [ "my-proc" ],
28 );
29  
30 $loop->add( $process );
31  
32 ...
33  
34 $process->shutdown( "TERM" )->get;
35  
36 =head1 DESCRIPTION
37  
38 This subclass of L<IO::Async::Process> adds a method to perform a shutdown of
39 the invoked process by sending a signal. If after some delay the process has
40 not yet exited, it is sent a C<SIGKILL> instead.
41  
42 =cut
43  
44 sub configure
45 {
46 2 ▶ my $self = shift;
Subroutine configure: called CC=2 SCAR=6.9
Pod documented
47 2 my %params = @_;
48  
49 2 ▶ if (my $on_finish = delete $params{on_finish}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_finish = delete $params{"on_finish"}) 1 1 2
50 1 $self->{ia__process__gracefulshutdown_wrapped_on_finish} = $on_finish;
51 }
52  
53 2 return $self->SUPER::configure(%params);
54 }
55  
56 =head1 METHODS
57  
58 =cut
59  
60 =head2 finish_future
61  
62 $f = $process->finish_future
63  
64 Returns a L<Future> that completes when the process finishes. It will yield
65 the exit code from the process.
66  
67 =cut
68  
69 # TODO: Migrate this to regular IO::Async::Process
70 sub finish_future
71 {
72 6 ▶ my $self = shift;
Subroutine finish_future: called CC=2 SCAR=7.1
Pod documented
73 6 ▶ return $self->{ia__process__gracefulshutdown_future} //= $self->loop->new_future;
Condition66%2/3
$self->{"ia__process__gracefulshutdown_future"} //= $self->loop->new_future
l!l&&r!l&&!r
420
MC/DC0%0/2
$self->{"ia__process__gracefulshutdown_future"} //= $self->loop->new_future
$self->{"ia__process__gracefulshutdown_future"} $self->loop->new_future
Truth table66%2/3
A //= B
A= $self->{"ia__process__gracefulshutdown_future"}B= $self->loop->new_future
ABresult
1X1
011
000
74 }
75  
76 sub on_finish
77 {
78 2 ▶ my $self = shift;
Subroutine on_finish: called CC=2 SCAR=6.9
Pod documented
79 2 my ( $exitcode ) = @_;
80  
81 2 ▶ if( my $on_finish = $self->{ia__process__gracefulshutdown_wrapped_on_finish}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_finish = $self->{"ia__process__gracefulshutdown_wrapped_on_finish"}) 1 1 2
82 1 $self->$on_finish( $exitcode );
83 }
84  
85 2 $self->finish_future->done($exitcode);
86 }
87  
88 =head2 shutdown
89  
90 $process->shutdown( $signal, %args )->get
91  
92 Requests the process shut down by sending it the given signal (either
93 specified by name or number). If the process does not shut down after a
94 timeout, C<SIGKILL> is sent instead.
95  
96 The returned future will complete when the process exits.
97  
98 Takes the following named arguments:
99  
100 =over 4
101  
102 =item timeout => NUM
103  
104 Optional. Number of seconds to wait for exit before sending C<SIGKILL>.
105 Defaults to 10 seconds.
106  
107 =item on_kill => CODE
108  
109 Optional. Callback to invoke if the timeout occurs and C<SIGKILL> is going to
110 be sent. Intended for printing or logging purposes; this doesn't have any
111 material effect on the process otherwise.
112  
113 $on_kill->( $process )
114  
115 =back
116  
117 =cut
118  
119 sub shutdown
120 {
121 1 ▶ my $self = shift;
Subroutine shutdown: called CC=2 SCAR=8.8
Pod documented
122 1 my ( $signal, %args ) = @_;
123  
124 1 ▶ my $timeout = $args{timeout} // 10;
Condition50%1/2
$args{"timeout"} // 10
l!l
01
MC/DC0%0/1
$args{"timeout"} // 10
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
125  
126 1 my $pid = $self->pid;
127  
128 1 $self->debug_printf( "KILL signal=%s", $signal );
129 1 $self->kill( $signal );
130  
131 return Future->needs_any(
132 $self->finish_future->then_done(),
133  
134 $self->loop->delay_future( after => $timeout )->then(
135 sub {
136 0 ▶ $args{on_kill}->( $self ) if $args{on_kill};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_kill"} 0 0 0
137  
138 0 $self->debug_printf( "KILL signal=KILL" );
139 0 $self->kill( "KILL" );
140 0 Future->done;
141 })
142 1 );
143 }
144  
145 =head1 AUTHOR
146  
147 Paul Evans <leonerd@leonerd.org.uk>
148  
149 =cut
150  
151 3 0x55AA;
« Build ↑ Summary