| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Gearman::WorkerSpawner::BaseWorker - Base class to simplify the process of | ||
| 4 | creating a Gearman worker for use with Gearman::WorkerSpawner | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | # create manager as normal. the contents of 'config' can be arbitrary; the | ||
| 9 | # special 'max_jobs' option instructs the worker to exit cleanly after | ||
| 10 | # performing that many jobs. | ||
| 11 | |||
| 12 | my $worker_manager = Gearman::WorkerSpawner->new; | ||
| 13 | $worker_manager->add_worker( | ||
| 14 | class => 'AdditionWorker', | ||
| 15 | config => { | ||
| 16 | left_hand => 5, | ||
| 17 | max_jobs => 100, | ||
| 18 | }, | ||
| 19 | ); | ||
| 20 | |||
| 21 | # invoke run_method instead of add_task for workers derived from | ||
| 22 | # BaseWorker. serialization of options is handled for you, and if you only care | ||
| 23 | # about the success case you can provide only that callback | ||
| 24 | |||
| 25 | $worker_manager->run_method(adder => { right_hand => 3 }, sub { | ||
| 26 | my $return = shift; | ||
| 27 | print $return->{sum}; | ||
| 28 | }); | ||
| 29 | |||
| 30 | Danga::Socket->EventLoop; | ||
| 31 | |||
| 32 | # in the worker: | ||
| 33 | |||
| 34 | package MethodWorker; | ||
| 35 | use base 'Gearman::WorkerSpawner::BaseWorker'; | ||
| 36 | |||
| 37 | # Gearman::WorkerSpawner will instantiate your class; the object will | ||
| 38 | # contain populated 'config' and 'slot' fields | ||
| 39 | sub new { | ||
| 40 | my MethodWorker $self = shift; | ||
| 41 | $self = fields::new($self) unless ref $self; | ||
| 42 | $self->SUPER::new(@_); | ||
| 43 | |||
| 44 | print "I am worker $self->{slot}\n"; | ||
| 45 | $self->register_method(adder => \&add); | ||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub add { | ||
| 50 | my MethodWorker $self = shift; | ||
| 51 | my $args = shift; | ||
| 52 | return { sum => $self->{config}{left_hand} + $args->{right_hand} }; | ||
| 53 | } | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | package Gearman::WorkerSpawner::BaseWorker; | ||
| 58 | |||
| 59 | use strict; | ||
| 60 | use warnings; | ||
| 61 | |||
| 62 | use base 'Gearman::Worker'; | ||
| 63 | |||
| 64 | use fields ( | ||
| 65 | 'config', | ||
| 66 | 'slot', | ||
| 67 | 'max_jobs', | ||
| 68 | 'jobs_done', | ||
| 69 | 'method_suffix', | ||
| 70 | ); | ||
| 71 | |||
| 72 | use Storable qw(nfreeze thaw); | ||
| 73 | |||
| 74 | sub new { | ||
| 75 | my Gearman::WorkerSpawner::BaseWorker $self = shift; | ||
| 76 | my ($slot, $config, $gearmands, $max_jobs) = @_; | ||
| 77 | |||
| 78 | $self = fields::new($self) unless ref $self; | ||
| 79 | $self->SUPER::new(job_servers => $gearmands); | ||
| 80 | |||
| 81 | $self->{slot} = $slot; | ||
| 82 | $self->{config} = $config; | ||
| 83 | $self->{max_jobs} = $config->{max_jobs} || undef; | ||
| 84 | $self->{jobs_done} = 0; | ||
| 85 | $self->{method_suffix} = '_m'; | ||
| 86 | |||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head1 METHODS | ||
| 91 | |||
| 92 | =item register_method($function_name) | ||
| 93 | |||
| 94 | =item register_method($function_name, $method) | ||
| 95 | |||
| 96 | =item register_method($function_name, $method, $timeout) | ||
| 97 | |||
| 98 | Registers a method to be called via Gearman::WorkerSpawner->run_method. A | ||
| 99 | Gearman function named $function_name will be registered with the server. When | ||
| 100 | a client calls that function, the method named $method (may alternatively be a | ||
| 101 | coderef) will be called with the Gearman::WorkerSpawner::BaseWorker object as | ||
| 102 | the first argument. $method defaults to be the same as $function_name if not | ||
| 103 | provided. | ||
| 104 | |||
| 105 | If $timeout is provided, the Gearman server may attempt to retry the function | ||
| 106 | if the job is not finished withint $timeout seconds. | ||
| 107 | |||
| 108 | The parameters to $method and return value (which should be a scalar) from it | ||
| 109 | are marshalled by Storable. | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub register_method { | ||
| 114 | my Gearman::WorkerSpawner::BaseWorker $self = shift; | ||
| 115 | my $name = shift; | ||
| 116 | my $method = shift || $name; | ||
| 117 | my $timeout = shift; | ||
| 118 | |||
| 119 | $name .= $self->{method_suffix}; | ||
| 120 | |||
| 121 | my @timeout; | ||
| 122 | @timeout = ($timeout) if defined $timeout; | ||
| 123 | |||
| 124 | $self->register_function($name, @timeout, sub { | ||
| 125 | $self->{jobs_done}++; | ||
| 126 | |||
| 127 | my $job = shift; | ||
| 128 | my $arg = $job->arg; | ||
| 129 | |||
| 130 | # deserialize argument | ||
| 131 | my $params = thaw($job->arg); | ||
| 132 | |||
| 133 | my @retvals = $self->$method(@$params); | ||
| 134 | |||
| 135 | # serialize return value(s) | ||
| 136 | return \nfreeze(\@retvals); | ||
| 137 | }); | ||
| 138 | } | ||
| 139 | |||
| 140 | =item method_suffix([$suffix]) | ||
| 141 | |||
| 142 | Accessor for the suffix which is appended to the method name. Defaults to '_m'. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub method_suffix { | ||
| 147 | my Gearman::WorkerSpawner::BaseWorker $self = shift; | ||
| 148 | $self->{method_suffix} = shift if @_;; | ||
| 149 | return $self->{method_suffix}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub post_work { | ||
| 153 | my Gearman::WorkerSpawner::BaseWorker $self = shift; | ||
| 154 | return unless $self->{max_jobs}; | ||
| 155 | exit 0 if $self->{jobs_done} >= $self->{max_jobs}; | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; |