File Coverage

lib/Qudo/Worker.pm
Criterion Covered Total %
statement 6 26 23.0
branch 0 8 0.0
condition 0 5 0.0
subroutine 2 7 28.5
pod 4 5 80.0
total 12 51 23.5


line stmt bran cond sub pod time code
1             package Qudo::Worker;
2 9     9   9462 use strict;
  9         16  
  9         280  
3 9     9   43 use warnings;
  9         16  
  9         2559  
4              
5 0     0 1   sub max_retries { 0 }
6 0     0 1   sub retry_delay { 0 }
7 0     0 1   sub grab_for { 60*60 } # default setting 1 hour
8 0     0 1   sub set_job_status { 0 } # job process status store for job_status table.
9              
10             sub work_safely {
11 0     0 0   my ($class, $job) = @_;
12              
13 0 0         if ($job->funcname->set_job_status) {
14 0           $job->job_start_time = time;
15             }
16              
17 0           my $res;
18 0           eval {
19 0           $res = $class->work($job);
20             };
21              
22 0 0         if ($job->is_aborted) {
23 0           $job->dequeue;
24 0           return $res;
25             }
26              
27 0 0 0       if ( (my $e = $@) || ! $job->is_completed ) {
28 0 0         if ( $job->retry_cnt < $class->max_retries ) {
29 0           $job->reenqueue(
30             {
31             grabbed_until => 0,
32             retry_cnt => $job->retry_cnt + 1,
33             retry_delay => $class->retry_delay,
34             }
35             );
36             } else {
37 0           $job->dequeue;
38             }
39 0   0       $job->failed("$e" || 'Job did not explicitly complete or fail');
40             } else {
41 0           $job->dequeue;
42             }
43              
44 0           return $res;
45             }
46              
47             =head1 NAME
48              
49             Qudo::Worker - superclass for defining task behavior of Qudo's work
50              
51             =head1 SYNOPSIS
52              
53             package Myworker;
54             use base qw/ Qudo::Worker /;
55              
56             sub work {
57             my ($self , $job ) = @_;
58              
59             my $job_arg = $job->arg();
60             print "This is Myworker's work. job has argument == $job_arg \n";
61              
62             $job->completed(); # or $job->abort
63             }
64             1;
65              
66             =head1 DESCRIPTION
67              
68             Qudo::Worker is based on all your work class of using Qudo.
69              
70             Your application have to inherit Qudo::Worker anyway.
71             And it has to have 'work' method too.
72              
73             'work' method accept Qudo::Job object at parameter.
74             If your work complete , you may call Qudo::Job->complete() method.
75              
76             =head1 WORKER SETTING
77              
78             =head2 max_retries
79              
80             package Your::Worker;
81             use base 'Qudo::Worker';
82             sub max_retries { 2 }
83             sub work { ... }
84              
85             How many times it retries if worker doesn't succeed is set.
86             It is retried two times in this example.
87             By default, return 0. no retry.
88              
89             =head2 retry_delay
90              
91             package Your::Worker;
92             use base 'Qudo::Worker';
93             sub retry_delay { 10 }
94             sub work { ... }
95              
96             returns the number of seconds after a failure workers should wait until
97             retry a job that has already failed retry_delay times.
98             By default,return 0 seconds
99              
100             =head2 grab_for
101              
102             package Your::Worker;
103             use base 'Qudo::Worker';
104             sub grab_for { 3600 }
105             sub work { ... }
106              
107             Returns the number of seconds workers of this class will claim a grabbed a job.
108             By default,return 3600 seconds.
109              
110             =head2 set_job_status
111              
112             package Your::Worker;
113             use base 'Qudo::Worker';
114             sub set_job_status { 1 }
115             sub work { ... }
116              
117             set the flag.
118             When flag is the truth, the processing result of worker is preserved in DB.
119              
120             =cut
121              
122             1;
123