File Coverage

lib/Qudo/Worker.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 8 0.0
condition 0 5 0.0
subroutine 4 12 33.3
pod 3 8 37.5
total 19 69 27.5


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