| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Resque::Job; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Resque job container | 
| 3 |  |  |  |  |  |  | $Resque::Job::VERSION = '0.42'; | 
| 4 | 9 |  |  | 9 |  | 71 | use Moose; | 
|  | 9 |  |  |  |  | 19 |  | 
|  | 9 |  |  |  |  | 80 |  | 
| 5 | 9 |  |  | 9 |  | 61690 | use Moose::Util::TypeConstraints; | 
|  | 9 |  |  |  |  | 40 |  | 
|  | 9 |  |  |  |  | 2754 |  | 
| 6 |  |  |  |  |  |  | with 'Resque::Encoder'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 9 |  |  | 9 |  | 19879 | use overload '""' => \&stringify; | 
|  | 9 |  |  |  |  | 20 |  | 
|  | 9 |  |  |  |  | 92 |  | 
| 9 | 9 |  |  | 9 |  | 629 | use Class::Load qw(load_class); | 
|  | 9 |  |  |  |  | 19 |  | 
|  | 9 |  |  |  |  | 4389 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has resque  => ( | 
| 12 |  |  |  |  |  |  | is      => 'rw', | 
| 13 |  |  |  |  |  |  | handles => [qw/ redis /], | 
| 14 |  |  |  |  |  |  | default => sub { confess "This Resque::Job isn't associated to any Resque system yet!" } | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has worker  => ( | 
| 18 |  |  |  |  |  |  | is      => 'rw', | 
| 19 |  |  |  |  |  |  | lazy    => 1, | 
| 20 |  |  |  |  |  |  | default   => sub { $_[0]->resque->worker }, | 
| 21 |  |  |  |  |  |  | predicate => 'has_worker' | 
| 22 |  |  |  |  |  |  | ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has class => ( is => 'rw', lazy => 1, default => sub { confess "This job needs a class to do some work." } ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | has queue => ( | 
| 27 |  |  |  |  |  |  | is        => 'rw', lazy => 1, | 
| 28 |  |  |  |  |  |  | default   => \&queue_from_class, | 
| 29 |  |  |  |  |  |  | predicate => 'queued' | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | has args => ( is => 'rw', isa => 'ArrayRef', default => sub {[]} ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | coerce 'HashRef' | 
| 35 |  |  |  |  |  |  | => from 'Str' | 
| 36 |  |  |  |  |  |  | => via { JSON->new->utf8->decode($_) }; | 
| 37 |  |  |  |  |  |  | has payload => ( | 
| 38 |  |  |  |  |  |  | is      => 'ro', | 
| 39 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 40 |  |  |  |  |  |  | coerce  => 1, | 
| 41 |  |  |  |  |  |  | lazy    => 1, | 
| 42 |  |  |  |  |  |  | builder => 'payload_builder', | 
| 43 |  |  |  |  |  |  | trigger => \&_payload_trigger | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub encode { | 
| 47 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 48 | 0 |  |  |  |  |  | $self->encoder->encode( $self->payload ); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub stringify { | 
| 52 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 53 | 0 |  |  |  |  |  | sprintf( "(Job{%s} | %s | %s)", | 
| 54 |  |  |  |  |  |  | $self->queue, | 
| 55 |  |  |  |  |  |  | $self->class, | 
| 56 |  |  |  |  |  |  | $self->encoder->encode( $self->args ) | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub queue_from_class { | 
| 61 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 62 | 0 |  |  |  |  |  | my $class = $self->class; | 
| 63 | 0 |  |  |  |  |  | $class =~ s/://g; | 
| 64 | 0 |  |  |  |  |  | $class; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub perform { | 
| 68 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 69 | 0 |  |  |  |  |  | load_class($self->class); | 
| 70 | 0 | 0 |  |  |  |  | $self->class->can('perform') | 
| 71 |  |  |  |  |  |  | || confess $self->class . " doesn't know how to perform"; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 9 |  |  | 9 |  | 76 | no strict 'refs'; | 
|  | 9 |  |  |  |  | 27 |  | 
|  | 9 |  |  |  |  | 3383 |  | 
| 74 | 0 |  |  |  |  |  | &{$self->class . '::perform'}($self); | 
|  | 0 |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub enqueue { | 
| 78 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 79 | 0 |  |  |  |  |  | $self->resque->push( $self->queue, $self ); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub dequeue { | 
| 83 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 84 | 0 |  |  |  |  |  | $self->resque->mass_dequeue({ | 
| 85 |  |  |  |  |  |  | queue => $self->queue, | 
| 86 |  |  |  |  |  |  | class => $self->class, | 
| 87 |  |  |  |  |  |  | args  => $self->args | 
| 88 |  |  |  |  |  |  | }); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub fail { | 
| 92 | 0 |  |  | 0 | 1 |  | my ( $self, $error ) = @_; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  |  | my $exception = 'Resque::Failure::Job'; | 
| 95 | 0 | 0 | 0 |  |  |  | if ( ref $error && ref $error eq 'ARRAY' ) { | 
| 96 | 0 |  |  |  |  |  | ( $exception, $error ) = @$error; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | $self->resque->throw( | 
| 100 | 0 |  |  |  |  |  | job       => $self, | 
| 101 |  |  |  |  |  |  | worker    => $self->worker, | 
| 102 |  |  |  |  |  |  | queue     => $self->queue, | 
| 103 |  |  |  |  |  |  | payload   => $self->payload, | 
| 104 |  |  |  |  |  |  | exception => $exception, | 
| 105 |  |  |  |  |  |  | error     => $error | 
| 106 |  |  |  |  |  |  | ); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub payload_builder {+{ | 
| 110 | 0 |  |  | 0 | 1 |  | class => $_[0]->class, | 
| 111 |  |  |  |  |  |  | args  => $_[0]->args | 
| 112 |  |  |  |  |  |  | }} | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub payload_reader { | 
| 115 | 0 |  |  | 0 | 1 |  | my ( $self, $hr ) = @_; | 
| 116 | 0 |  |  |  |  |  | $self->class( $hr->{class} ); | 
| 117 | 0 | 0 |  |  |  |  | $self->args( $hr->{args} ) if $hr->{args}; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 |  |  | 0 |  |  | sub _payload_trigger { shift->payload_reader(@_) } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable(); | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | __END__ | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =pod | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =encoding UTF-8 | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head1 NAME | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Resque::Job - Resque job container | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =head1 VERSION | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | version 0.42 | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head2 resque | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Provides 'redis' method, which provides access to our redis subsystem. | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head2 worker | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Worker running this job. | 
| 147 |  |  |  |  |  |  | A new worker will be popped up from resque by default. | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head2 class | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | Class to be performed by this job. | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head2 queue | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Name of the queue this job is or should be. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 args | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Array of arguments for this job. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head2 payload | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | HashRef representation of the job. | 
| 164 |  |  |  |  |  |  | When passed to constructor, this will restore the job from encoded state. | 
| 165 |  |  |  |  |  |  | When passed as a string this will be coerced using JSON decoder. | 
| 166 |  |  |  |  |  |  | This is read-only. | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =head1 METHODS | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =head2 encode | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | String representation(JSON) to be used on the backend. | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | $job->encode(); | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =head2 stringify | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | Returns a string version of the job, like | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | '(Job{queue_name) | ClassName | args_encoded)' | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | my $stringified = $job->stringify(); | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =head2 queue_from_class | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | Normalize class name to be used as queue name. | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | my $queue_name = $job->queue_from_class(); | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | NOTE: future versions will try to get the | 
| 191 |  |  |  |  |  |  | queue name from the real class attr | 
| 192 |  |  |  |  |  |  | or $class::queue global variable. | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head2 perform | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | Load job class and call perform() on it. | 
| 197 |  |  |  |  |  |  | This job object will be passed as the only argument. | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | $job->perform(); | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head2 enqueue | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | Add this job to resque. | 
| 204 |  |  |  |  |  |  | See Rescue::push(). | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | $job->enqueue(); | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =head2 dequeue | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | Remove this job from resque using the most restrictive | 
| 211 |  |  |  |  |  |  | form of Resque::mass_dequeue. | 
| 212 |  |  |  |  |  |  | This method will remove all jobs matching this | 
| 213 |  |  |  |  |  |  | object queue, class and args. | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | See Resque::mass_dequeue() for massive destruction. | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | $job->enqueue(); | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | =head2 fail | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | Store a failure (or exception and failure) on this job. | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | $job->fail( "error message'); # or | 
| 224 |  |  |  |  |  |  | $job->fail( ['exception', 'error message'] ); | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =head2 payload_builder | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | Default payload builder method. This method is public only to be wrapped in the | 
| 229 |  |  |  |  |  |  | context of plugins that adds attributes to this class. | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | =head2 payload_reader | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | Default payload trigger method. This method is public only to be wrapped in the | 
| 234 |  |  |  |  |  |  | context of plugins that adds attributes to this class. | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | This mehtod is only called at construction time to populate job class and args | 
| 237 |  |  |  |  |  |  | attributes from payload. | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | =head1 AUTHOR | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | Diego Kuperman <diego@freekeylabs.com> | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | This software is copyright (c) 2021 by Diego Kuperman. | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 248 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | =cut |