/usr/share/perl5/Minion/Job.pm is in libminion-perl 6.02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | package Minion::Job;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::IOLoop;
use POSIX 'WNOHANG';
has [qw(args id minion retries task)];
sub app { shift->minion->app }
sub fail {
my ($self, $err) = (shift, shift // 'Unknown error');
my $ok = $self->minion->backend->fail_job($self->id, $self->retries, $err);
return $ok ? !!$self->emit(failed => $err) : undef;
}
sub finish {
my ($self, $result) = @_;
my $ok
= $self->minion->backend->finish_job($self->id, $self->retries, $result);
return $ok ? !!$self->emit(finished => $result) : undef;
}
sub info { $_[0]->minion->backend->job_info($_[0]->id) }
sub is_finished {
my $self = shift;
return undef unless waitpid($self->{pid}, WNOHANG) == $self->{pid};
$? ? $self->fail("Non-zero exit status (@{[$? >> 8]})") : $self->finish;
return 1;
}
sub stop { kill 'KILL', shift->{pid} }
sub perform {
my $self = shift;
waitpid $self->start->pid, 0;
$? ? $self->fail("Non-zero exit status (@{[$? >> 8]})") : $self->finish;
}
sub pid { shift->{pid} }
sub remove { $_[0]->minion->backend->remove_job($_[0]->id) }
sub retry {
my $self = shift;
return $self->minion->backend->retry_job($self->id, $self->retries, @_);
}
sub start {
my $self = shift;
# Parent
die "Can't fork: $!" unless defined(my $pid = fork);
return $self->emit(spawn => $pid) if $self->{pid} = $pid;
# Child
eval {
# Reset event loop
Mojo::IOLoop->reset;
$self->minion->tasks->{$self->emit('start')->task}->($self, @{$self->args});
1;
} or $self->fail($@);
POSIX::_exit(0);
}
1;
=encoding utf8
=head1 NAME
Minion::Job - Minion job
=head1 SYNOPSIS
use Minion::Job;
my $job = Minion::Job->new(id => $id, minion => $minion, task => 'foo');
=head1 DESCRIPTION
L<Minion::Job> is a container for L<Minion> jobs.
=head1 EVENTS
L<Minion::Job> inherits all events from L<Mojo::EventEmitter> and can emit the
following new ones.
=head2 failed
$job->on(failed => sub {
my ($job, $err) = @_;
...
});
Emitted in the worker process managing this job or the process performing it,
after it has transitioned to the C<failed> state.
$job->on(failed => sub {
my ($job, $err) = @_;
say "Something went wrong: $err";
});
=head2 finished
$job->on(finished => sub {
my ($job, $result) = @_;
...
});
Emitted in the worker process managing this job or the process performing it,
after it has transitioned to the C<finished> state.
$job->on(finished => sub {
my ($job, $result) = @_;
my $id = $job->id;
say "Job $id is finished.";
});
=head2 spawn
$job->on(spawn => sub {
my ($job, $pid) = @_;
...
});
Emitted in the worker process managing this job, after a new process has been
spawned for processing.
$job->on(spawn => sub {
my ($job, $pid) = @_;
my $id = $job->id;
say "Job $id running in process $pid";
});
=head2 start
$job->on(start => sub {
my $job = shift;
...
});
Emitted in the process performing this job, after it has been spawned.
$job->on(start => sub {
my $job = shift;
$0 = $job->id;
});
=head1 ATTRIBUTES
L<Minion::Job> implements the following attributes.
=head2 args
my $args = $job->args;
$job = $job->args([]);
Arguments passed to task.
=head2 id
my $id = $job->id;
$job = $job->id($id);
Job id.
=head2 minion
my $minion = $job->minion;
$job = $job->minion(Minion->new);
L<Minion> object this job belongs to.
=head2 retries
my $retries = $job->retries;
$job = $job->retries(5);
Number of times job has been retried.
=head2 task
my $task = $job->task;
$job = $job->task('foo');
Task name.
=head1 METHODS
L<Minion::Job> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 app
my $app = $job->app;
Get application from L<Minion/"app">.
# Longer version
my $app = $job->minion->app;
=head2 fail
my $bool = $job->fail;
my $bool = $job->fail('Something went wrong!');
my $bool = $job->fail({whatever => 'Something went wrong!'});
Transition from C<active> to C<failed> state, and if there are attempts
remaining, transition back to C<inactive> with a delay based on
L<Minion/"backoff">.
=head2 finish
my $bool = $job->finish;
my $bool = $job->finish('All went well!');
my $bool = $job->finish({whatever => 'All went well!'});
Transition from C<active> to C<finished> state.
=head2 info
my $info = $job->info;
Get job information.
# Check job state
my $state = $job->info->{state};
# Get job result
my $result = $job->info->{result};
These fields are currently available:
=over 2
=item args
args => ['foo', 'bar']
Job arguments.
=item attempts
attempts => 25
Number of times performing this job will be attempted.
=item children
children => ['10026', '10027', '10028']
Jobs depending on this job.
=item created
created => 784111777
Epoch time job was created.
=item delayed
delayed => 784111777
Epoch time job was delayed to.
=item finished
finished => 784111777
Epoch time job was finished.
=item parents
parents => ['10023', '10024', '10025']
Jobs this job depends on.
=item priority
priority => 3
Job priority.
=item queue
queue => 'important'
Queue name.
=item result
result => 'All went well!'
Job result.
=item retried
retried => 784111777
Epoch time job has been retried.
=item retries
retries => 3
Number of times job has been retried.
=item started
started => 784111777
Epoch time job was started.
=item state
state => 'inactive'
Current job state, usually C<active>, C<failed>, C<finished> or C<inactive>.
=item task
task => 'foo'
Task name.
=item worker
worker => '154'
Id of worker that is processing the job.
=back
=head2 is_finished
my $bool = $job->is_finished;
Check if job performed with L</"start"> is finished.
=head2 perform
$job->perform;
Perform job in new process and wait for it to finish.
=head2 pid
my $pid = $job->pid;
Process id of the process spawned by L</"start"> if available.
=head2 remove
my $bool = $job->remove;
Remove C<failed>, C<finished> or C<inactive> job from queue.
=head2 retry
my $bool = $job->retry;
my $bool = $job->retry({delay => 10});
Transition from C<failed> or C<finished> state back to C<inactive>, already
C<inactive> jobs may also be retried to change options.
These options are currently available:
=over 2
=item delay
delay => 10
Delay job for this many seconds (from now).
=item priority
priority => 5
Job priority.
=item queue
queue => 'important'
Queue to put job in.
=back
=head2 start
$job = $job->start;
Perform job in new process, but do not wait for it to finish.
# Perform two jobs concurrently
$job1->start;
$job2->start;
my ($first, $second);
sleep 1
until $first ||= $job1->is_finished and $second ||= $job2->is_finished;
=head2 stop
$job->stop;
Stop job performed with L</"start"> immediately.
=head1 SEE ALSO
L<Minion>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|