File Coverage

blib/lib/Helios/TS/Job.pm
Criterion Covered Total %
statement 11 49 22.4
branch 0 12 0.0
condition 0 15 0.0
subroutine 4 7 57.1
pod 2 2 100.0
total 17 85 20.0


line stmt bran cond sub pod time code
1             package Helios::TS::Job;
2              
3 1     1   20 use 5.008;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         17  
5 1     1   3 use warnings;
  1         1  
  1         25  
6 1     1   3 use base 'TheSchwartz::Job';
  1         1  
  1         432  
7              
8             our $VERSION = "2.80";
9              
10             # FILE CHANGE HISTORY
11             # (This code is modified from the original TheSchwartz::Job.pm where noted.)
12             # [LH] [2013-10-04]: Virtual jobtypes: funcmap entries without actual
13             # TheSchwartz::Worker subclasses to back them up. Changed
14             # set_exit_status(), failed(), _failed() to use $job->{active_worker_class}
15             # instead of $job->funcname if $job->{active_worker_class} is set.
16              
17             sub set_exit_status {
18 0     0 1   my $job = shift;
19 0           my($exit) = @_;
20             # [LH] [2013-10-04]: Use active_worker_class instead of funcname if AWC is set.
21 0   0       my $class = $job->{active_worker_class} || $job->funcname;
22 0 0         my $secs = $class->keep_exit_status_for or return;
23 0           my $status = TheSchwartz::ExitStatus->new;
24 0           $status->jobid($job->jobid);
25 0           $status->funcid($job->funcid);
26 0           $status->completion_time(time);
27 0           $status->delete_after($status->completion_time + $secs);
28 0           $status->status($exit);
29              
30 0           my $driver = $job->driver;
31 0           $driver->insert($status);
32              
33             # and let's lazily clean some exitstatus while we're here. but
34             # rather than doing this query all the time, we do it 1/nth of the
35             # time, and deleting up to n*10 queries while we're at it.
36             # default n is 10% of the time, doing 100 deletes.
37 0   0       my $clean_thres = $TheSchwartz::T_EXITSTATUS_CLEAN_THRES || 0.10;
38 0 0         if (rand() < $clean_thres) {
39 0           my $unixtime = $driver->dbd->sql_for_unixtime;
40 0 0         $driver->remove('TheSchwartz::ExitStatus', {
41             delete_after => \ "< $unixtime",
42             }, {
43             nofetch => 1,
44             limit => $driver->dbd->can_delete_with_limit ? int(1 / $clean_thres * 100) : undef,
45             });
46             }
47              
48 0           return $status;
49             }
50              
51              
52             sub failed {
53 0     0 1   my ($job, $msg, $ex_status) = @_;
54 0 0         if ($job->did_something) {
55 0           $job->debug("can't call 'failed' on already finished job");
56 0           return 0;
57             }
58              
59             ## If this job class specifies that jobs should be retried,
60             ## update the run_after if necessary, but keep the job around.
61              
62             # [LH] [2013-10-04]: Use active_worker_class instead of funcname if AWC is set.
63 0   0       my $class = $job->{active_worker_class} || $job->funcname;
64 0           my $failures = $job->failures + 1; # include this one, since we haven't ->add_failure yet
65 0           my $max_retries = $class->max_retries($job);
66              
67 0           $job->debug("job failed. considering retry. is max_retries of $max_retries >= failures of $failures?");
68 0           $job->_failed($msg, $ex_status, $max_retries >= $failures, $failures);
69             }
70              
71             sub _failed {
72 0     0     my ($job, $msg, $exit_status, $_retry, $failures) = @_;
73 0           $job->did_something(1);
74 0   0       $job->debug("job failed: " . ($msg || ""));
75              
76             ## Mark the failure in the error table.
77 0           $job->add_failure($msg);
78              
79 0 0         if ($_retry) {
80             # [LH] [2013-10-04]: Use active_worker_class instead of funcname if AWC is set.
81 0   0       my $class = $job->{active_worker_class} || $job->funcname;
82 0 0         if (my $delay = $class->retry_delay($failures)) {
83 0           $job->run_after(time() + $delay);
84             }
85 0           $job->grabbed_until(0);
86 0           $job->driver->update($job);
87             } else {
88 0   0       $job->set_exit_status($exit_status || 1);
89 0           $job->driver->remove($job);
90             }
91             }
92              
93              
94             1;
95             __END__