| 1 | package Sys::ForkAsync; | ||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||
| 3 | 1 | $Sys::ForkAsync::VERSION = '0.14'; | |||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | $Sys::ForkAsync::AUTHORITY = 'cpan:TEX'; | |||||||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Simple async one-time job | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use 5.010_000; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use feature ':5.10'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | # use IO::Handle; | ||||||||||||||||||||||||||||||||
| 18 | # use autodie; | ||||||||||||||||||||||||||||||||
| 19 | # use MooseX::Params::Validate; | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # for fork() | ||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Errno qw(EAGAIN); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use POSIX qw(WNOHANG); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 1 | has 'chdir' => ( | |||||||||||||||||||||||||||||||
| 26 | 'is' => 'rw', | ||||||||||||||||||||||||||||||||
| 27 | 'isa' => 'Str', | ||||||||||||||||||||||||||||||||
| 28 | 'default' => 0, | ||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 1 | has 'redirect_output' => ( | |||||||||||||||||||||||||||||||
| 32 | 'is' => 'rw', | ||||||||||||||||||||||||||||||||
| 33 | 'isa' => 'Bool', | ||||||||||||||||||||||||||||||||
| 34 | 'default' => 1, | ||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 1 | has 'close_fhs' => ( | |||||||||||||||||||||||||||||||
| 38 | 'is' => 'rw', | ||||||||||||||||||||||||||||||||
| 39 | 'isa' => 'Bool', | ||||||||||||||||||||||||||||||||
| 40 | 'default' => 1, | ||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 1 | has 'setsid' => ( | |||||||||||||||||||||||||||||||
| 44 | 'is' => 'rw', | ||||||||||||||||||||||||||||||||
| 45 | 'isa' => 'Bool', | ||||||||||||||||||||||||||||||||
| 46 | 'default' => 0, | ||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 1 | has 'name' => ( | |||||||||||||||||||||||||||||||
| 50 | 'is' => 'ro', | ||||||||||||||||||||||||||||||||
| 51 | 'isa' => 'Str', | ||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub dispatch { | ||||||||||||||||||||||||||||||
|
Subroutine
dispatch: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 0 | my $code_ref = shift; | |||||||||||||||||||||||||||||||
| 58 | 0 | my $arg_ref = shift; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # fork() - see Programming Perl p. 737 | ||||||||||||||||||||||||||||||||
| 61 | FORK: | ||||||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( my $pid = fork ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | # This is the parent process, child pid is in $pid | ||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | elsif ( defined $pid ) { | ||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | POSIX::setsid() if $self->setsid(); # create own process group | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $self->chdir() && -d $self->chdir() ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$self->chdir and -d $self->chdir
MC/DC0%0/2
$self->chdir and -d $self->chdir
$self->chdir
-d $self->chdir
Truth table0%0/3
A and B
A= $self->chdirB= -d $self->chdir
| |||||||||||||||||||||||||||||||||
| 70 | 0 | chdir( $self->chdir() ); | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | elsif ( $self->chdir() ) { | ||||||||||||||||||||||||||||||||
| 73 | 0 | chdir(q{/}); | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | # DGR: what should i do? just ignore it ... | ||||||||||||||||||||||||||||||||
| 76 | ## no critic (RequireCheckedClose) | ||||||||||||||||||||||||||||||||
| 77 | 0 | close(STDIN); | |||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( $self->redirect_output() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | close(STDOUT); | |||||||||||||||||||||||||||||||
| 80 | 0 | close(STDERR); | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | ## use critic | ||||||||||||||||||||||||||||||||
| 83 | ## no critic (RequireCheckedOpen ProhibitUnixDevNull) | ||||||||||||||||||||||||||||||||
| 84 | 0 | open( STDIN, '<', '/dev/null' ); | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $self->redirect_output() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 0 | open( STDOUT, '>', '/dev/null' ); | |||||||||||||||||||||||||||||||
| 87 | 0 | open( STDERR, '>', '/dev/null' ); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | ## use critic | ||||||||||||||||||||||||||||||||
| 90 | # close any other filehandles (DBI, etc.) | ||||||||||||||||||||||||||||||||
| 91 | # STDIN - 0 | ||||||||||||||||||||||||||||||||
| 92 | # STDOUT - 1 | ||||||||||||||||||||||||||||||||
| 93 | # STDERR - 2 | ||||||||||||||||||||||||||||||||
| 94 | # those were handled above ... now take care of the rest | ||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( $self->close_fhs() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | ## no critic (ProhibitMagicNumbers) | ||||||||||||||||||||||||||||||||
| 97 | 0 | foreach my $i ( 3 .. 255 ) { | |||||||||||||||||||||||||||||||
| 98 | 0 | POSIX::close($i); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | ## use critic | ||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # $pid is null, if defined | ||||||||||||||||||||||||||||||||
| 104 | # This is the child process | ||||||||||||||||||||||||||||||||
| 105 | # get the pid of the parent via getppid | ||||||||||||||||||||||||||||||||
| 106 | ## no critic (ProhibitPunctuationVars) | ||||||||||||||||||||||||||||||||
| 107 | 0 | my $pid = $$; | |||||||||||||||||||||||||||||||
| 108 | ## use critic | ||||||||||||||||||||||||||||||||
| 109 | 0 | my $ppid = getppid(); | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $0 = $self->name() if $self->name(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 0 | my $t0 = time(); # starttime | |||||||||||||||||||||||||||||||
| 114 | 0 | my $status = &{$code_ref}( 'ForkAsync', $arg_ref ); | |||||||||||||||||||||||||||||||
| 115 | 0 | my $d0 = time() - $t0; # duration | |||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($status) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 0 | exit 0; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||||||
| 120 | 0 | exit 1; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # end of fork(). The child _must_ exit here! | ||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | ## no critic (ProhibitPunctuationVars ProhibitMagicNumbers) | ||||||||||||||||||||||||||||||||
| 126 | elsif ( $! == EAGAIN ) { | ||||||||||||||||||||||||||||||||
| 127 | # EAGAIN, probably temporary fork error | ||||||||||||||||||||||||||||||||
| 128 | 0 | sleep 5; | |||||||||||||||||||||||||||||||
| 129 | 0 | redo FORK; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | ## use critic | ||||||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | # Strange fork error | ||||||||||||||||||||||||||||||||
| 135 | ## no critic (ProhibitPunctuationVars) | ||||||||||||||||||||||||||||||||
| 136 | 0 | warn 'Can not exec fork: '.$!."\n"; | |||||||||||||||||||||||||||||||
| 137 | ## use critic | ||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | } # FORK | ||||||||||||||||||||||||||||||||
| 140 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 144 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 1 | 1; | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||