|
NAMEMinion::Job - Minion jobSYNOPSISpackage MyApp::Task::Foo; use Mojo::Base 'Minion::Job', -signatures; sub run ($self, @args) { # Magic here! :) } DESCRIPTIONMinion::Job is a container for Minion jobs.EVENTSMinion::Job inherits all events from Mojo::EventEmitter and can emit the following new ones.cleanup$job->on(cleanup => sub ($job) { ... }); Emitted in the process performing this job right before the process will exit. $job->on(cleanup => sub ($job) { $job->app->log->debug("Process $$ is about to exit"); }); failed$job->on(failed => sub ($job, $err) { ... }); Emitted in the worker process managing this job or the process performing it, after it has transitioned to the "failed" state. $job->on(failed => sub ($job, $err) { say "Something went wrong: $err"; }); finish$job->on(finish => sub ($job) { ... }); Emitted in the process performing this job if the task was successful. $job->on(finish => sub ($job) { my $id = $job->id; my $task = $job->task; $job->app->log->debug(qq{Job "$id" was performed with task "$task"}); }); finished$job->on(finished => sub ($job, $result) { ... }); Emitted in the worker process managing this job or the process performing it, after it has transitioned to the "finished" state. $job->on(finished => sub ($job, $result) { my $id = $job->id; say "Job $id is finished."; }); reap$job->on(reap => sub ($job, $pid) { ... }); Emitted in the worker process managing this job, after the process performing it has exited. $job->on(reap => sub ($job, $pid) { my $id = $job->id; say "Job $id ran in process $pid"; }); spawn$job->on(spawn => sub ($job, $pid) { ... }); Emitted in the worker process managing this job, after a new process has been spawned for processing. $job->on(spawn => sub ($job, $pid) { my $id = $job->id; say "Job $id running in process $pid"; }); start$job->on(start => sub ($job) { ... }); Emitted in the process performing this job, after it has been spawned. $job->on(start => sub ($job) { $0 = $job->id; }); ATTRIBUTESMinion::Job implements the following attributes.argsmy $args = $job->args; $job = $job->args([]); Arguments passed to task. idmy $id = $job->id; $job = $job->id($id); Job id. minionmy $minion = $job->minion; $job = $job->minion(Minion->new); Minion object this job belongs to. retriesmy $retries = $job->retries; $job = $job->retries(5); Number of times job has been retried. taskmy $task = $job->task; $job = $job->task('foo'); Task name. METHODSMinion::Job inherits all methods from Mojo::EventEmitter and implements the following new ones.appmy $app = $job->app; Get application from "app" in Minion. # Longer version my $app = $job->minion->app; executemy $err = $job->execute; Perform job in this process and return "undef" if the task was successful or an exception otherwise. Note that this method should only be used to implement custom workers. # Perform job in foreground if (my $err = $job->execute) { $job->fail($err) } else { $job->finish } failmy $bool = $job->fail; my $bool = $job->fail('Something went wrong!'); my $bool = $job->fail({whatever => 'Something went wrong!'}); Transition from "active" to "failed" state with or without a result, and if there are attempts remaining, transition back to "inactive" with a delay based on "backoff" in Minion. finishmy $bool = $job->finish; my $bool = $job->finish('All went well!'); my $bool = $job->finish({whatever => 'All went well!'}); Transition from "active" to "finished" state with or without a result. infomy $info = $job->info; Get job information. # Check job state my $state = $job->info->{state}; # Get job metadata my $progress = $job->info->{notes}{progress}; # Get job result my $result = $job->info->{result}; These fields are currently available:
is_finishedmy $bool = $job->is_finished; Check if job performed with "start" is finished. Note that this method should only be used to implement custom workers. kill$job->kill('INT'); Send a signal to job performed with "start". Note that this method should only be used to implement custom workers. notemy $bool = $job->note(mojo => 'rocks', minion => 'too'); Change one or more metadata fields for this job. Setting a value to "undef" will remove the field. The new values will get serialized by "backend" in Minion (often with Mojo::JSON), so you shouldn't send objects and be careful with binary data, nested data structures with hash and array references are fine though. # Share progress information $job->note(progress => 95); # Share stats $job->note(stats => {utime => '0.012628', stime => '0.002429'}); parentsmy $parents = $job->parents; Return a Mojo::Collection object containing all jobs this job depends on as Minion::Job objects. # Check parent state for my $parent ($job->parents->each) { my $info = $parent->info; say "$info->{id}: $info->{state}"; } perform$job->perform; Perform job in new process and wait for it to finish. Note that this method should only be used to implement custom workers. pidmy $pid = $job->pid; Process id of the process spawned by "start" if available. Note that this method should only be used to implement custom workers. removemy $bool = $job->remove; Remove "failed", "finished" or "inactive" job from queue. retrymy $bool = $job->retry; my $bool = $job->retry({delay => 10}); Transition job back to "inactive" state, already "inactive" jobs may also be retried to change options. These options are currently available:
run$job->run(@args); Task to perform by this job. Meant to be overloaded in a subclass to create a custom task class. Note that this method is EXPERIMENTAL and might change without warning! start$job = $job->start; Perform job in new process, but do not wait for it to finish. Note that this method should only be used to implement custom workers. # Perform two jobs concurrently $job1->start; $job2->start; my ($first, $second); sleep 1 until $first ||= $job1->is_finished and $second ||= $job2->is_finished; stop$job->stop; Stop job performed with "start" immediately. Note that this method should only be used to implement custom workers. SEE ALSOMinion, Minion::Guide, <https://minion.pm>, Mojolicious::Guides, <https://mojolicious.org>.
Visit the GSP FreeBSD Man Page Interface. |