| 1 | package Qudo::Worker; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Class::Data::Inheritable'; | ||
| 5 | use Qudo::HookLoader; | ||
| 6 | |||
| 7 | __PACKAGE__->mk_classdata(qw/_hooks/); | ||
| 8 | |||
| 9 | sub max_retries { 0 } | ||
| 10 | sub retry_delay { 0 } | ||
| 11 | sub grab_for { 60*60 } # default setting 1 hour | ||
| 12 | sub set_job_status { 0 } # job process status store for job_status table. | ||
| 13 | sub hooks { | ||
| 14 | my $class = shift; | ||
| 15 | $class->_hooks(+{}) unless $class->_hooks; | ||
| 16 | $class->_hooks; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub register_hooks { | ||
| 20 | my ($class, @hook_modules) = @_; | ||
| 21 | Qudo::HookLoader->register_hooks($class, \@hook_modules); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub unregister_hooks { | ||
| 25 | my ($class, @hook_modules) = @_; | ||
| 26 | Qudo::HookLoader->unregister_hooks($class, \@hook_modules); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub work_safely { | ||
| 30 | my ($class, $manager, $job) = @_; | ||
| 31 | my $res; | ||
| 32 | |||
| 33 | if ($job->funcname->set_job_status) { | ||
| 34 | $job->job_start_time = time; | ||
| 35 | } | ||
| 36 | eval { | ||
| 37 | $res = $class->work($job); | ||
| 38 | }; | ||
| 39 | |||
| 40 | if ( my $e = $@ || ! $job->is_completed ) { | ||
| 41 | if ( $job->retry_cnt < $class->max_retries ) { | ||
| 42 | $job->reenqueue( | ||
| 43 | { | ||
| 44 | retry_cnt => $job->retry_cnt + 1, | ||
| 45 | retry_delay => $class->retry_delay, | ||
| 46 | } | ||
| 47 | ); | ||
| 48 | } else { | ||
| 49 | $manager->dequeue($job); | ||
| 50 | } | ||
| 51 | $manager->job_failed($job, $e || 'Job did not explicitly complete or fail'); | ||
| 52 | } else { | ||
| 53 | $manager->dequeue($job); | ||
| 54 | } | ||
| 55 | |||
| 56 | 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 |