| 1 | package Schedule::Pluggable::Run; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use FileHandle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use File::Temp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use POSIX qw/ strftime/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | $Data::Dumper::Sortkeys = 1; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | sub run_in_series { | ||||||||||||
|
Subroutine
run_in_series: called CC=9 SCAR=29.0
Pod
documented
| |||||||||||||||
| 12 | 1 | my $self = shift; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | ▶ | my @jobs = ref $_[0] eq 'ARRAY' ? @{ $_[0] } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 15 | : @_; | ||||||||||||||
| 16 | 1 | my $job_no = 1; | |||||||||||||
| 17 | 1 | my $last_job = undef; | |||||||||||||
| 18 | 1 | foreach my $job (@jobs) { | |||||||||||||
| 19 | 1 | ▶ | if (ref($job) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 20 | 0 | ▶ | $job->{name} ||= "Job".$job_no++; | ||||||||||||
|
Condition0%0/2
$job->{"name"} ||= 'Job' . $job_no++
MC/DC0%0/1
$job->{"name"} ||= 'Job' . $job_no++
$job->{"name"}
Truth table0%0/2
A
A= $job->{"name"}
| |||||||||||||||
| 21 | 0 | ▶ | delete $job->{dependencies} if $job->{dependencies}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 0 | ▶ | delete $job->{prerequisites} if $job->{prerequisites}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | } | ||||||||||||||
| 24 | elsif (ref($job) eq 'ARRAY') { | ||||||||||||||
| 25 | 0 | my $command = join(" ", @{ $job }); | |||||||||||||
| 26 | 0 | $job = { name => "Job".$job_no++, | |||||||||||||
| 27 | command => $command }; | ||||||||||||||
| 28 | |||||||||||||||
| 29 | } | ||||||||||||||
| 30 | else { | ||||||||||||||
| 31 | 1 | my $command = $job; | |||||||||||||
| 32 | 1 | $job = { name => "Job".$job_no++, | |||||||||||||
| 33 | command => $command | ||||||||||||||
| 34 | }; | ||||||||||||||
| 35 | } | ||||||||||||||
| 36 | 1 | ▶ | $job->{prerequisites} = $last_job->{name} if $last_job; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 1 | $last_job= { %{ $job } }; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | 1 | $self->run_schedule({ Jobs => [ @jobs ]}); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | 0 | ▶ | sub run_in_parallel { | ||||||||||||
|
Subroutine
run_in_parallel: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||
| 43 | 0 | ▶ | my @jobs = ref $_[0] eq 'ARRAY' ? @{ $_[0] } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | : @_; | ||||||||||||||
| 45 | 0 | my $job_no = 1; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | foreach my $job (@jobs) { | |||||||||||||
| 48 | 0 | ▶ | if (ref($job) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 49 | 0 | ▶ | $job->{name} ||= "Job".$job_no++; | ||||||||||||
|
Condition0%0/2
$job->{"name"} ||= 'Job' . $job_no++
MC/DC0%0/1
$job->{"name"} ||= 'Job' . $job_no++
$job->{"name"}
Truth table0%0/2
A
A= $job->{"name"}
| |||||||||||||||
| 50 | } | ||||||||||||||
| 51 | elsif (ref($job) eq 'ARRAY') { | ||||||||||||||
| 52 | 0 | my $command = join(" ", @{ $job }); | |||||||||||||
| 53 | 0 | $job = { name => "Job".$job_no++, | |||||||||||||
| 54 | command => $command }; | ||||||||||||||
| 55 | |||||||||||||||
| 56 | } | ||||||||||||||
| 57 | else { | ||||||||||||||
| 58 | 0 | my $command = $job; | |||||||||||||
| 59 | 0 | $job = { name => "Job".$job_no++, | |||||||||||||
| 60 | command => $command | ||||||||||||||
| 61 | }; | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | 0 | ▶ | delete $job->{dependencies} if $job->{dependencies}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | ▶ | delete $job->{prerequisites} if $job->{prerequisites}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | } | ||||||||||||||
| 66 | 0 | $self->run_schedule({ Jobs => [ @jobs ]}); | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | 1 | ▶ | sub run_schedule { | ||||||||||||
|
Subroutine
run_schedule: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 69 | 1 | my ($self, $params) = @_; | |||||||||||||
| 70 | 1 | my $status = $self->_validate_config($params); | |||||||||||||
| 71 | # warn Data::Dumper->Dump([$status],[qw/$status/]); | ||||||||||||||
| 72 | 0 | my @jobs_ready_to_run = sort { $a->{name} cmp $b->{name} } values %{ $status->{Ready_to_Run} }; | |||||||||||||
| 73 | 1 | delete $status->{Ready_to_Run}; | |||||||||||||
| 74 | 1 | $status->{EndOfScheduleFile} = File::Temp->new->filename; | |||||||||||||
| 75 | 1 | $self->_set_status($status); | |||||||||||||
| 76 | 0 | ▶ | my ($id, $pid) = $self->enqueue( MooseX::Workers::Job->new(name => 'MonitorJobs', command => sub { _monitor_jobs($self); } )); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 77 | 1 | $status->{Pids}{MonitorJobs} = $pid; | |||||||||||||
| 78 | 1 | $status->{Ids}{$id} = 'MonitorJobs'; | |||||||||||||
| 79 | 1 | $self->_set_status($status); | |||||||||||||
| 80 | 1 | $self->_queue_jobs(@jobs_ready_to_run); | |||||||||||||
| 81 | 1 | POE::Kernel->run; | |||||||||||||
| 82 | 1 | $status = $self->_get_status; | |||||||||||||
| 83 | 1 | delete $status->{$_} foreach qw/ Queued Running TotalQueued TotalStarted Ids Pids Groups Config EndOfScheduleFile LastUpdate/; | |||||||||||||
| 84 | 1 | delete $status->{Jobs}{MonitorJobs}; | |||||||||||||
| 85 | 1 | foreach my $k (qw/groups pending_prerequisites prerequisites Id Queued group/) { | |||||||||||||
| 86 | 6 | delete $status->{Jobs}{$_}{$k} foreach keys %{ $status->{Jobs} }; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 1 | return $status->{TotalFailed}, $status; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | 0 | ▶ | sub _monitor_jobs { | ||||||||||||
|
Subroutine
_monitor_jobs: not called CC=2 SCAR=17.9
| |||||||||||||||
| 92 | 0 | my $self = shift; | |||||||||||||
| 93 | 0 | my $status = $self->_get_status(); | |||||||||||||
| 94 | 0 | my $tempfile = $status->{EndOfScheduleFile}; | |||||||||||||
| 95 | 0 | while (1) { | |||||||||||||
| 96 | 0 | sleep(10); | |||||||||||||
| 97 | 0 | ▶ | last if -f $tempfile; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | } | ||||||||||||||
| 99 | 0 | unlink( $tempfile ); | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | 1 | ▶ | sub _queue_jobs { | ||||||||||||
|
Subroutine
_queue_jobs: called CC=4 SCAR=19.4
| |||||||||||||||
| 102 | 1 | my ($self, @jobs) = @_; | |||||||||||||
| 103 | 1 | foreach my $job (@jobs) { | |||||||||||||
| 104 | 1 | ▶ | if ($job->{params}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | ▶ | if (ref $job->{params} eq 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | $job->{command} = join(" ", $job->{command}, @{ $job->{params} }); | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | else { | ||||||||||||||
| 109 | 0 | $job->{command} = join(" ", $job->{command}, $job->{params}); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | } | ||||||||||||||
| 112 | my $j = MooseX::Workers::Job->new( | ||||||||||||||
| 113 | name => $job->{name}, | ||||||||||||||
| 114 | command => $job->{command} | ||||||||||||||
| 115 | 1 | ); | |||||||||||||
| 116 | $self->event_handler(Event => 'JobQueued', | ||||||||||||||
| 117 | JobName => $job->{name}, | ||||||||||||||
| 118 | Command => $job->{command}, | ||||||||||||||
| 119 | 1 | ); | |||||||||||||
| 120 | 1 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 121 | 1 | $self->enqueue( $j ); | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 124 | # warn "oops - can't queue $job->{name} - $job->{command}: $_"; | ||||||||||||||
| 125 | 0 | my $error = $_; | |||||||||||||
| 126 | 0 | $error =~ s! at\s+\S+\s+line\s+\d+.!!; | |||||||||||||
| 127 | $self->event_handler(Event => 'JobFailed', | ||||||||||||||
| 128 | JobName => $job->{name}, | ||||||||||||||
| 129 | Command => $job->{command}, | ||||||||||||||
| 130 | 0 | ReturnValue => -1, | |||||||||||||
| 131 | Stderr => "<$error>", | ||||||||||||||
| 132 | ); | ||||||||||||||
| 133 | |||||||||||||||
| 134 | $self->_update_status({name => $job->{name}}, | ||||||||||||||
| 135 | { | ||||||||||||||
| 136 | command => $job->{command}, | ||||||||||||||
| 137 | 0 | status => -1, | |||||||||||||
| 138 | stderr => "<$error>", | ||||||||||||||
| 139 | }); | ||||||||||||||
| 140 | 0 | return; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | 1 | ▶ | finally { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 143 | $self->_update_status({name => $job->{name}}, | ||||||||||||||
| 144 | 1 | {Queued => strftime('%d/%m/%Y %H:%M:%S', localtime(time())), | |||||||||||||
| 145 | }); | ||||||||||||||
| 146 | 1 | }; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 3 | 1; | |||||||||||||
| 151 | __END__ | ||||||||||||||