JobQueue/Job.pm

filter: Statementstmt 81.2% 104 / 128 Branchbran 58.7% 47 / 80 Conditioncond 55.5% 10 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 90.9% 20 / 22 Podpod 69.2% 9 / 13 totaltotal 71.1% 192 / 270 CC 48 SCAR 24.5
CC
48
Cov
71%
finished
26.2
start
18.9
failed
12.0
CRAP 11.6
« JobQueue.pm ↑ Summary JobQueue/DependencyTask.pm »
1  
2 package Proc::JobQueue::Job;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Tie::Function::Examples qw(%q_shell);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Proc::Background;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Proc::JobQueue qw(is_remote_host);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $debug = $Proc::JobQueue::debug;
15  
16 sub new
17 139 ▶{
Subroutine new: called CC=6 SCAR=19.5
Pod undocumented
18 139 my ($pkg, %params) = @_;
19 139 my $job = bless {
20 desc => '',
21 priority => 100,
22 procbg => undef,
23 run => undef,
24 command => undef,
25 queue => undef,
26 jobnum => undef,
27 postcb => undef,
28 generate_command => undef,
29 callback => undef,
30 host => undef,
31 jobnum => undef,
32 on_failure => undef,
33 errors => undef,
34 status => 'queued',
35 dependency_graph => undef,
36 force_host => undef,
37 %params
38 }, $pkg;
39 139 lock_keys(%$job);
40 139 ▶ if ($job->{queue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"queue"}) 0 139 139
41 0 $job->{queue}->add($job);
42 }
43 139 ▶ unless ($job->can_command || $job->can_callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($job->can_command or $job->can_callback) 0 139 139
Condition66%2/3
$job->can_command or $job->can_callback
l!l&&r!l&&!r
125140
MC/DC0%0/2
$job->can_command or $job->can_callback
$job->can_command $job->can_callback
Truth table66%2/3
A or B
A= $job->can_commandB= $job->can_callback
ABresult
1X1
011
000
44 0 confess "$pkg job needs a command or a callback";
45 }
46 139 ▶ unless ($job->{desc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($job->{"desc"}) 10 129 139
47 10 ▶ if ($job->{command}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'command'}) { } 0 10 10
48 0 $job->{desc} = $job->{command};
49 } else {
50 10 $job->{desc} = "$job"; # stringify
51 }
52 }
53 139 return $job;
54 }
55  
56 sub can_command
57 194 ▶{
Subroutine can_command: called CC=4 SCAR=13.9
Pod undocumented
58 194 my ($job) = @_;
59 194 ▶ return 1 if $job->{command};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"command"} 60 134 194
60 134 ▶ return 1 if $job->{generate_command};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"generate_command"} 0 134 134
61 134 ▶ return 1 if $job->can('command');
Branch: 1 branch
BranchTrueFalseTotal
if $job->can("command") 120 14 134
62 14 return 0;
63 }
64  
65 sub can_callback
66 14 ▶{
Subroutine can_callback: called CC=3 SCAR=12.5
Pod undocumented
67 14 my ($job) = @_;
68 14 ▶ return 1 if $job->{callback};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"callback"} 0 14 14
69 14 ▶ return 1 if $job->can('startup');
Branch: 1 branch
BranchTrueFalseTotal
if $job->can("startup") 14 0 14
70 0 return 0;
71 }
72  
73 sub start
74 195 ▶{
Subroutine start: called CC=12 SCAR=29.4
Pod documented
75 195 my ($job) = @_;
76  
77 195 my $host = $job->{host};
78 195 my $queue = $job->{queue};
79 195 my $jobnum = $job->{jobnum};
80  
81 195 $job->{status} = 'started';
82  
83 my $command = $job->{command}
84 195 ▶ || ($job->{generate_command} && $job->{generate_command}->($job))
Condition66%8/12
$job->{'command'} || $job->{'generate_command'} && $job->{'generate_command'}->($job) || $job->can('command') && $job->command
l!l&&r!l&&!r
408075
$job->{"command"} or $job->{"generate_command"} and $job->{"generate_command"}->($job)
l!l&&r!l&&!r
400155
$job->{"generate_command"} and $job->{"generate_command"}->($job)
!ll&&!rl&&r
15500
$job->can("command") and $job->command
!ll&&!rl&&r
75080
MC/DC40%2/5
$job->{'command'} || $job->{'generate_command'} && $job->{'generate_command'}->($job) || $job->can('command') && $job->command
$job->{"command"} $job->{"generate_command"} $job->{"generate_command"}->($job) $job->can("command") $job->command
Truth table37%3/8
A or B and C || D and E
A= $job->{"command"}B= $job->{"generate_command"}C= $job->{"generate_command"}->($job)D= $job->can("command")E= $job->command
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
85 || ($job->can('command') && $job->command())
86 ;
87  
88 195 ▶ if ($command) {
Branch: 3 branches
BranchTrueFalseTotal
if ($command) { } 120 75 195
elsif ($job->{'callback'}) { } 0 75 75
elsif ($job->can('startup')) { } 75 0 75
89 $job->{desc} = $command
90 120 ▶ unless $job->{desc};
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"desc"} 0 120 120
91 120 ▶ if (is_remote_host($host)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_remote_host($host)) 0 120 120
92 0 $command = "ssh $host -o BatchMode=yes -o StrictHostKeyChecking=no $q_shell{$command}";
93 }
94 $job->{run} = $command
95 120 ▶ unless $job->{run};
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"run"} 120 0 120
96 120 print "+ $command\n";
97 120 $job->{procbg} = Proc::Background->new($command);
98 } elsif ($job->{callback}) {
99 0 ▶ if (ref($job->{callback}) eq 'Callback') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $job->{'callback'} eq 'Callback') { } 0 0 0
100 0 $job->{callback}->call($job);
101 } else {
102 0 $job->{callback}->($job, $host, $jobnum, $queue);
103 }
104 } elsif ($job->can('startup')) {
105 75 $job->startup($job, $host, $jobnum, $queue);
106 } else {
107 0 die "don't know how to start $job";
108 }
109 }
110  
111 sub host
112 554 ▶{
Subroutine host: called CC=2 SCAR=6.9
Pod documented
113 554 my ($job, $host) = @_;
114 554 ▶ $job->{host} = $host if defined $host;
Branch: 1 branch
BranchTrueFalseTotal
if defined $host 250 304 554
115 554 return $job->{host};
116 }
117  
118 sub jobnum
119 1177 ▶{
Subroutine jobnum: called CC=2 SCAR=6.9
Pod documented
120 1177 my ($job, $jobnum) = @_;
121 1177 ▶ $job->{jobnum} = $jobnum if defined $jobnum;
Branch: 1 branch
BranchTrueFalseTotal
if defined $jobnum 194 983 1177
122 1177 return $job->{jobnum};
123 }
124  
125 sub queue
126 136 ▶{
Subroutine queue: called CC=2 SCAR=6.9
Pod documented
127 136 my ($job, $queue) = @_;
128 136 ▶ if ($queue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue) 135 1 136
129 135 $job->{queue} = $queue;
130 135 weaken $job->{queue};
131 }
132 136 return $job->{queue};
133 }
134  
135 sub runnable
136 95 ▶{
Subroutine runnable: called CC=1 SCAR=0.0
Pod documented
137 95 return 1;
138 }
139  
140 sub checkjob
141 479 ▶{
Subroutine checkjob: called CC=6 SCAR=18.0
Pod documented
142 479 my ($job) = @_;
143 479 ▶ print STDERR "# checking up on $job->{jobnum} $job->{desc} on $job->{host}\n" if $debug > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 6 0 479 479
144 479 ▶ unless ($job->{procbg}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($job->{"procbg"}) 6 473 479
145 6 ▶ print STDERR "# $job->{jobnum} is not a Proc::Background job\n" if $debug > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 9 0 6 6
146 6 return undef;
147 }
148 473 ▶ if ($job->{procbg}->alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"procbg"}->alive) 353 120 473
149 353 ▶ print STDERR "# $job->{jobnum} $job->{desc} is still alive\n" if $debug > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 6 0 353 353
150 353 return undef;
151 }
152 120 my $queue = $job->{queue};
153 120 my $e = $job->{procbg}->wait;
154 120 $e >>= 8;
155 120 print "# $job->{desc} on $job->{host} finished\n";
156 120 $job->finished($e);
157 120 return $e;
158 }
159  
160 sub finished
161 248 ▶{
Subroutine finished: called CC=15 SCAR=32.6
Pod documented
162 248 my ($job, @exit_code) = @_;
163 248 ▶ return if $job->{status} eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"status"} eq "finished" 60 188 248
164 188 $job->{status} = 'finished';
165 188 ▶ die "NO JOBNUM FOR $job" unless $job->{jobnum};
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"jobnum"} 0 188 188
166 188 ▶ print STDERR "# FINISHED $job->{jobnum} $job->{desc} on $job->{host}\n" if $debug > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 7 0 188 188
167 188 ▶ if ($job->{postcb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"postcb"}) 0 188 188
168 $_->call($job, @exit_code)
169 0 for @{$job->{postcb}};
170 0 delete $job->{postcb}; # may clean circular references
171 }
172 188 my $queue = $job->{queue};
173 188 undef $job->{queue};
174 188 ▶ if ($queue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue) 128 60 188
175 128 ▶ if ($job->{jobnum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'jobnum'}) { } 128 0 128
176 128 ▶ print STDERR "# calling JOBDONE for $job->{jobnum} $job->{desc} ($job->{status})\n" if $debug > 5;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 5 0 128 128
177 128 $queue->jobdone($job, 0, @exit_code); # not re-entrant since startmore == 0
178 } else {
179 0 ▶ print STDERR "# NOT calling JOBDONE for $job->{jobnum} $job->{desc} ($job->{status})\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
180 }
181 }
182 188 ▶ if ($exit_code[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code[0]) { } 0 188 188
183 0 ▶ print STDERR "# calling failed(@exit_code) for $job->{jobnum} $job->{desc}\n" if $debug > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 6 0 0 0
184 0 $job->failed(@exit_code);
185 } else {
186 188 ▶ print STDERR "# calling success() for $job->{jobnum} $job->{desc}\n" if $debug > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 7 0 188 188
187 188 $job->success();
188 188 ▶ print STDERR "# done calling success() for $job->{jobnum} $job->{desc}\n" if $debug > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 9 0 188 188
189 }
190 188 ▶ $queue->startmore if $queue; # can be re-entrant
Branch: 1 branch
BranchTrueFalseTotal
if $queue 128 60 188
191 }
192  
193  
194 sub success
195 8 ▶{
Subroutine success: called CC=2 SCAR=7.2
Pod undocumented
196 8 my ($job) = @_;
197 8 ▶ print STDERR "# Empty success on $job->{jobnum} $job->{desc}\n" if $debug > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 8 0 8 8
198 }
199  
200 sub addpostcb
201 0 ▶{
Subroutine addpostcb: not called CC=2 SCAR=17.9
Pod documented
202 0 my ($job, $cb1, @more) = @_;
203 0 my $cb = new Callback($cb1, @more);
204 $job->{postcb} = []
205 0 ▶ unless $job->{postcb};
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"postcb"} 0 0 0
206 0 push(@{$job->{postcb}}, $cb);
207 }
208  
209 sub failed
210 0 ▶{
Subroutine failed: not called CC=3 SCAR=24.8
Pod documented
211 0 my ($job, @exit_code) = @_;
212 0 ▶ if ($job->{queue} && $job->{queue}{on_failure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'queue'} and $job->{'queue'}{'on_failure'}) { } 0 0 0
Condition0%0/3
$job->{"queue"} and $job->{"queue"}{"on_failure"}
!ll&&!rl&&r
000
MC/DC0%0/2
$job->{"queue"} and $job->{"queue"}{"on_failure"}
$job->{"queue"} $job->{"queue"}{"on_failure"}
Truth table0%0/3
A and B
A= $job->{"queue"}B= $job->{"queue"}{"on_failure"}
ABresult
0X0
100
111
213 0 $job->{queue}{on_failure}->($job, @exit_code);
214 } else {
215 0 die "job $job->{desc} failed with @exit_code";
216 }
217 }
218  
219 3 1;
220  
221 __END__
« JobQueue.pm ↑ Summary JobQueue/DependencyTask.pm »