| 1 | package App::Sandy::InterlaceProcesses; | ||
| 2 | # ABSTRACT: Interlaces the processe id for differents processes, actually for parent, child processes. | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.25'; # VERSION | ||
| 7 | |||
| 8 | has 'foreign_pid' => ( | ||
| 9 | is => 'ro', | ||
| 10 | isa => 'ArrayRef[Int]', | ||
| 11 | required => 1 | ||
| 12 | ); | ||
| 13 | |||
| 14 | has '_signal_stack' => ( | ||
| 15 | traits => ['Array'], | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'ArrayRef[Str]', | ||
| 18 | default => sub { [] }, | ||
| 19 | handles => { | ||
| 20 | _add_signal => 'push', | ||
| 21 | signal_catched => 'count', | ||
| 22 | join_signals_received => 'join' | ||
| 23 | } | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub BUILD { | ||
| 27 | my $self = shift; | ||
| 28 | $SIG{TERM} = $self->_handle_signal; | ||
| 29 | $SIG{INT} = $self->_handle_signal; | ||
| 30 | $SIG{QUIT} = $self->_handle_signal; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _handle_signal { | ||
| 34 | my $self = shift; | ||
| 35 | return sub { | ||
| 36 | my $signame = shift; | ||
| 37 | unless ($self->signal_catched) { | ||
| 38 | $self->_add_signal($signame); | ||
| 39 | my $cnt = kill 'TERM' => @{ $self->foreign_pid }; | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | |||
| 44 | __END__ | ||