| 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 | 3 | IO::Async::Process->VERSION( '0.75' ); # ->finish_future | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | C<IO::Async::Process::GracefulShutdown> - controlled shutdown of a process | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | use IO::Async::Process::GracefulShutdown; | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | use IO::Async::Loop; | ||||||||||||||||||||
| 25 | my $loop = IO::Async::Loop; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | my $process = IO::Async::Process::GracefulShutdown->new( | ||||||||||||||||||||
| 28 | command => [ "my-proc" ], | ||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | $loop->add( $process ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | ... | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | $process->shutdown( "TERM" )->get; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | This subclass of L<IO::Async::Process> adds a method to perform a shutdown of | ||||||||||||||||||||
| 40 | the invoked process by sending a signal. If after some delay the process has | ||||||||||||||||||||
| 41 | not yet exited, it is sent a C<SIGKILL> instead. | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub _init | ||||||||||||||||||||
| 46 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 47 | 4 | my $self = shift; | |||||||||||||||||||
| 48 | 4 | my ( $params ) = @_; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 4 | ▶ | $params->{on_finish} //= sub {}; # back-compat did not require this | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$params->{"on_finish"} //= sub {
}
MC/DC0%0/2
$params->{"on_finish"} //= sub {
}
$params->{"on_finish"}
sub {
}
Truth table66%2/3
A //= B
A= $params->{"on_finish"}B= sub {
}
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 4 | $self->SUPER::_init( $params ); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 METHODS | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head2 shutdown | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | $process->shutdown( $signal, %args )->get | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Requests the process shut down by sending it the given signal (either | ||||||||||||||||||||
| 64 | specified by name or number). If the process does not shut down after a | ||||||||||||||||||||
| 65 | timeout, C<SIGKILL> is sent instead. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | The returned future will complete when the process exits. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | Takes the following named arguments: | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =over 4 | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =item timeout => NUM | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Optional. Number of seconds to wait for exit before sending C<SIGKILL>. | ||||||||||||||||||||
| 76 | Defaults to 10 seconds. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =item on_kill => CODE | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Optional. Callback to invoke if the timeout occurs and C<SIGKILL> is going to | ||||||||||||||||||||
| 81 | be sent. Intended for printing or logging purposes; this doesn't have any | ||||||||||||||||||||
| 82 | material effect on the process otherwise. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | $on_kill->( $process ) | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =back | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | sub shutdown | ||||||||||||||||||||
| 91 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
shutdown: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 92 | 2 | my $self = shift; | |||||||||||||||||||
| 93 | 2 | my ( $signal, %args ) = @_; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | ▶ | my $timeout = $args{timeout} // 10; | ||||||||||||||||||
|
Condition100%2/2
$args{"timeout"} // 10
MC/DC100%1/1
$args{"timeout"} // 10
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | my $pid = $self->pid; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | $self->debug_printf( "KILL signal=%s", $signal ); | |||||||||||||||||||
| 100 | 2 | $self->kill( $signal ); | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | return Future->needs_any( | ||||||||||||||||||||
| 103 | $self->finish_future->then_done(), | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | $self->loop->delay_future( after => $timeout )->then( | ||||||||||||||||||||
| 106 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 107 | 0 | ▶ | $args{on_kill}->( $self ) if $args{on_kill}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | $self->debug_printf( "KILL signal=KILL" ); | |||||||||||||||||||
| 110 | 0 | $self->kill( "KILL" ); | |||||||||||||||||||
| 111 | 0 | Future->done; | |||||||||||||||||||
| 112 | }) | ||||||||||||||||||||
| 113 | 2 | ); | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 AUTHOR | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 3 | 0x55AA; | |||||||||||||||||||