| 1 | package Cron::RunJob; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'refaddr'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mail::Mailer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IPC::Open3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 18 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | |||||||||||||||||||||||||||
| 19 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 20 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 21 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | my ($job_pid, %_data); | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | $SIG{TERM} = $SIG{INT} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | kill 9, $job_pid if $job_pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | exit; | |||||||||||||||||||||||||||
| 27 | 1 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($self, %opt) = @_; | |||||||||||||||||||||||||||
| 31 | 0 | $self = bless {}, $self; | |||||||||||||||||||||||||||
| 32 | $_data{refaddr $self}{uc $_} = $opt{$_} | ||||||||||||||||||||||||||||
| 33 | 0 | for keys %opt; | |||||||||||||||||||||||||||
| 34 | 0 | return $self; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub runfile_name { | ||||||||||||||||||||||||||
|
Subroutine
runfile_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 0 | my ($self, $cmd) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | $cmd =~ s/.*\///; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $dir = $self->runfile_dir || '/var/run'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->runfile_dir || '/var/run'
MC/DC0%0/1
$self->runfile_dir || '/var/run'
$self->runfile_dir
Truth table0%0/2
A
A= $self->runfile_dir
| |||||||||||||||||||||||||||||
| 41 | 0 | return $dir .'/'. $cmd . ".pid"; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub create_runfile { | ||||||||||||||||||||||||||
|
Subroutine
create_runfile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($self, $runfile) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | my $fh = new IO::File; | |||||||||||||||||||||||||||
| 47 | 0 | $fh->open("> $runfile"); | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | die $! unless defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | print $fh $self->pid; | |||||||||||||||||||||||||||
| 50 | 0 | $fh->close; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub unlink_runfile { | ||||||||||||||||||||||||||
|
Subroutine
unlink_runfile: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($self, $runfile) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (-e $runfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | unlink $runfile or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub is_running { | ||||||||||||||||||||||||||
|
Subroutine
is_running: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($self, $runfile) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (-e $runfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | my $fh = new IO::File; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | die $! unless defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $fh->open($runfile) or die "Open run file $runfile $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | my $pid = <$fh>; | |||||||||||||||||||||||||||
| 67 | 0 | $fh->close; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | return if $pid == $$; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->pid($pid); | |||||||||||||||||||||||||||
| 70 | 0 | return kill 0, $pid; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | return 0; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | my ($self, $cmd, @argv) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($self->only_me and $self->is_running($self->runfile_name($cmd))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->only_me and $self->is_running($self->runfile_name($cmd))
MC/DC0%0/2
$self->only_me and $self->is_running($self->runfile_name($cmd))
$self->only_me
$self->is_running($self->runfile_name($cmd))
Truth table0%0/3
A and B
A= $self->only_meB= $self->is_running($self->runfile_name($cmd))
| |||||||||||||||||||||||||||||
| 79 | 0 | $self->stderr("Proccess is already running"); | |||||||||||||||||||||||||||
| 80 | 0 | $self->failed(1); | |||||||||||||||||||||||||||
| 81 | 0 | return 0; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $select = IO::Select->new(); | |||||||||||||||||||||||||||
| 85 | 0 | my $chld_stderr = new IO::File; | |||||||||||||||||||||||||||
| 86 | 0 | my $chld_stdin = new IO::File; | |||||||||||||||||||||||||||
| 87 | 0 | my $chld_stdout = new IO::File; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $job_pid = open3($chld_stdin, $chld_stdout, $chld_stderr, $cmd, @argv); | |||||||||||||||||||||||||||
| 90 | 0 | $self->pid($job_pid); | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $self->create_runfile($self->runfile_name($cmd)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | if $self->only_me; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | $select->add($chld_stderr); | |||||||||||||||||||||||||||
| 95 | 0 | $select->add($chld_stdout); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my ($buff, $std_error, $std_out); | |||||||||||||||||||||||||||
| 98 | 0 | foreach my $fh ($select->can_read) { | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | while (my $buff = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($fh == $chld_stderr) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 101 | 0 | $std_error .= $buff; | |||||||||||||||||||||||||||
| 102 | } elsif ($fh == $chld_stdout) { | ||||||||||||||||||||||||||||
| 103 | 0 | $std_out .= $buff; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $chld_stderr->close; | |||||||||||||||||||||||||||
| 109 | 0 | $chld_stdout->close; | |||||||||||||||||||||||||||
| 110 | 0 | $chld_stdin->close; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | waitpid($job_pid, 0); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($std_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $self->stderr($std_error); | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($self->mail_stderr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | my $mailer = new Mail::Mailer 'sendmail'; | |||||||||||||||||||||||||||
| 118 | 0 | $mailer->open({ | |||||||||||||||||||||||||||
| 119 | To => $self->mail_to, | ||||||||||||||||||||||||||||
| 120 | From => $self->mail_from, | ||||||||||||||||||||||||||||
| 121 | Subject => 'STDERR: '. $self->mail_subject, | ||||||||||||||||||||||||||||
| 122 | }); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | print $mailer "Error: $cmd failed with error(s): ".($std_error ? $std_error:'unknown errors')."\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $mailer->close; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | $self->failed(1); | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 0 | $self->failed(0); | |||||||||||||||||||||||||||
| 130 | 0 | $self->stdout($std_out); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($self->mail_stdout) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my $mailer = new Mail::Mailer 'sendmail'; | |||||||||||||||||||||||||||
| 133 | 0 | $mailer->open({ | |||||||||||||||||||||||||||
| 134 | From => $self->mail_from, | ||||||||||||||||||||||||||||
| 135 | To => $self->mail_to, | ||||||||||||||||||||||||||||
| 136 | Subject => 'STDOUT: '. $self->mail_subject, | ||||||||||||||||||||||||||||
| 137 | }); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | print $mailer $self->stdout; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $self->unlink_runfile($self->runfile_name($cmd)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | if $self->only_me; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | 0 | (my $attr = $AUTOLOAD) =~ s/^.*:://; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (exists $_data{refaddr $self}->{uc $attr} and $_data{refaddr $self}->{uc $attr}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $_data{builtin::refaddr($self)}{uc $attr} and $_data{builtin::refaddr($self)}{uc $attr}
MC/DC0%0/2
exists $_data{builtin::refaddr($self)}{uc $attr} and $_data{builtin::refaddr($self)}{uc $attr}
exists $_data{builtin::refaddr($self)}{uc $attr}
$_data{builtin::refaddr($self)}{uc $attr}
Truth table0%0/3
A and B
A= exists $_data{builtin::refaddr($self)}{uc $attr}B= $_data{builtin::refaddr($self)}{uc $attr}
| |||||||||||||||||||||||||||||
| 151 | 0 | return $_data{refaddr $self}->{uc $attr}; | |||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | $_data{refaddr $self}->{uc $attr} = $value if $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | 1; | |||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||