| 1 | package Gearman::Driver::Job::Method; | ||||||||||
| 2 | |||||||||||
| 3 | 29 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Gearman::Driver::Job::Method - Wraps a single job method | ||||||||||
| 8 | |||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||
| 10 | |||||||||||
| 11 | =head1 ATTRIBUTES | ||||||||||
| 12 | |||||||||||
| 13 | =head2 name | ||||||||||
| 14 | |||||||||||
| 15 | Name of the job method. | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 29 | has 'name' => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | isa => 'Str', | ||||||||||
| 22 | required => 1, | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | =head2 body | ||||||||||
| 26 | |||||||||||
| 27 | Code reference which is called by L<Gearman::XS::Worker>. | ||||||||||
| 28 | Actually it's not called directly by it, but in a wrapped coderef. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | 29 | has 'body' => ( | |||||||||
| 33 | is => 'rw', | ||||||||||
| 34 | isa => 'CodeRef', | ||||||||||
| 35 | required => 1, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | =head2 worker | ||||||||||
| 39 | |||||||||||
| 40 | Reference to the worker object. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 29 | has 'worker' => ( | |||||||||
| 45 | is => 'rw', | ||||||||||
| 46 | isa => 'Any', | ||||||||||
| 47 | required => 1, | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | =head2 encode | ||||||||||
| 51 | |||||||||||
| 52 | This may be set to a method name which is implemented in the worker | ||||||||||
| 53 | class or any subclass. If the method is not available, it will fail. | ||||||||||
| 54 | The returned value of the job method is passed to this method and | ||||||||||
| 55 | the return value of this method is sent back to the Gearman server. | ||||||||||
| 56 | |||||||||||
| 57 | See also: L<Gearman::Driver::Worker/Encode>. | ||||||||||
| 58 | |||||||||||
| 59 | =cut | ||||||||||
| 60 | |||||||||||
| 61 | 29 | has 'encode' => ( | |||||||||
| 62 | default => '', | ||||||||||
| 63 | is => 'rw', | ||||||||||
| 64 | isa => 'Str', | ||||||||||
| 65 | required => 1, | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | =head2 decode | ||||||||||
| 69 | |||||||||||
| 70 | This may be set to a method name which is implemented in the worker | ||||||||||
| 71 | class or any subclass. If the method is not available, it will fail. | ||||||||||
| 72 | The workload from L<Gearman::XS::Job> is passed to this method and | ||||||||||
| 73 | the return value is passed as argument C<$workload> to the job | ||||||||||
| 74 | method. | ||||||||||
| 75 | |||||||||||
| 76 | See also: L<Gearman::Driver::Worker/Decode>. | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | 29 | has 'decode' => ( | |||||||||
| 81 | default => '', | ||||||||||
| 82 | is => 'rw', | ||||||||||
| 83 | isa => 'Str', | ||||||||||
| 84 | required => 1, | ||||||||||
| 85 | ); | ||||||||||
| 86 | |||||||||||
| 87 | 29 | has 'wrapper' => ( | |||||||||
| 88 | is => 'rw', | ||||||||||
| 89 | isa => 'CodeRef', | ||||||||||
| 90 | ); | ||||||||||
| 91 | |||||||||||
| 92 | 42 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 93 | 42 | my ($self) = @_; | |||||||||
| 94 | |||||||||||
| 95 | 42 | ▶ | my $decoder = sub { shift }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 42 | ▶ | my $encoder = sub { shift }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 97 | |||||||||||
| 98 | 42 | ▶ | if ( my $decoder_method = $self->decode ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 12 | ▶ | $decoder = sub { return $self->worker->$decoder_method(@_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 100 | } | ||||||||||
| 101 | 42 | ▶ | if ( my $encoder_method = $self->encode ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 11 | ▶ | $encoder = sub { return $self->worker->$encoder_method(@_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | $self->wrapper( | ||||||||||
| 106 | 6 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||
| 107 | 6 | my ($job) = @_; | |||||||||
| 108 | |||||||||||
| 109 | 6 | my @args = ($job); | |||||||||
| 110 | |||||||||||
| 111 | 6 | push @args, $decoder->( $job->workload ); | |||||||||
| 112 | |||||||||||
| 113 | 6 | $self->worker->begin(@args); | |||||||||
| 114 | |||||||||||
| 115 | 6 | my $error; | |||||||||
| 116 | my $result; | ||||||||||
| 117 | 6 | eval { $result = $self->body->( $self->worker, @args ); }; | |||||||||
| 118 | 5 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | $error = $@; | |||||||||
| 120 | 0 | printf "lasterror %d\n", time; | |||||||||
| 121 | 0 | printf "lasterror_msg %s\n", $error; | |||||||||
| 122 | 0 | $self->worker->on_exception( @args, $error ); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 5 | printf "lastrun %d\n", time; | |||||||||
| 126 | |||||||||||
| 127 | 5 | $self->worker->end(@args, $error); | |||||||||
| 128 | |||||||||||
| 129 | 5 | ▶ | die $error if $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | |||||||||||
| 131 | 5 | return $encoder->($result); | |||||||||
| 132 | } | ||||||||||
| 133 | 42 | ); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | =head1 AUTHOR | ||||||||||
| 137 | |||||||||||
| 138 | See L<Gearman::Driver>. | ||||||||||
| 139 | |||||||||||
| 140 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 141 | |||||||||||
| 142 | See L<Gearman::Driver>. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 SEE ALSO | ||||||||||
| 145 | |||||||||||
| 146 | =over 4 | ||||||||||
| 147 | |||||||||||
| 148 | =item * L<Gearman::Driver> | ||||||||||
| 149 | |||||||||||
| 150 | =item * L<Gearman::Driver::Adaptor> | ||||||||||
| 151 | |||||||||||
| 152 | =item * L<Gearman::Driver::Console> | ||||||||||
| 153 | |||||||||||
| 154 | =item * L<Gearman::Driver::Console::Basic> | ||||||||||
| 155 | |||||||||||
| 156 | =item * L<Gearman::Driver::Console::Client> | ||||||||||
| 157 | |||||||||||
| 158 | =item * L<Gearman::Driver::Job> | ||||||||||
| 159 | |||||||||||
| 160 | =item * L<Gearman::Driver::Loader> | ||||||||||
| 161 | |||||||||||
| 162 | =item * L<Gearman::Driver::Observer> | ||||||||||
| 163 | |||||||||||
| 164 | =item * L<Gearman::Driver::Worker> | ||||||||||
| 165 | |||||||||||
| 166 | =back | ||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | 29 | 1; | |||||||||