DOKK / manpages / debian 10 / libminion-perl / Minion::Job.3pm.en
Minion::Job(3pm) User Contributed Perl Documentation Minion::Job(3pm)

Minion::Job - Minion job

  use Minion::Job;
  my $job = Minion::Job->new(id => $id, minion => $minion, task => 'foo');

Minion::Job is a container for Minion jobs.

Minion::Job inherits all events from Mojo::EventEmitter and can emit the following new ones.

  $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 "failed" state.

  $job->on(failed => sub {
    my ($job, $err) = @_;
    say "Something went wrong: $err";
  });

  $job->on(finish => sub {
    my $job = shift;
    ...
  });

Emitted in the process performing this job if the task was successful. Note that this event is EXPERIMENTAL and might change without warning!

  $job->on(finish => sub {
    my $job  = shift;
    my $id   = $job->id;
    my $task = $job->task;
    $job->app->log->debug(qq{Job "$id" was performed with task "$task"});
  });

  $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 "finished" state.

  $job->on(finished => sub {
    my ($job, $result) = @_;
    my $id = $job->id;
    say "Job $id is finished.";
  });

  $job->on(reap => sub {
    my ($job, $pid) = @_;
    ...
  });

Emitted in the worker process managing this job, after the process performing it has exited.

  $job->on(reap => sub {
    my ($job, $pid) = @_;
    my $id = $job->id;
    say "Job $id ran in process $pid";
  });

  $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";
  });

  $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;
  });

Minion::Job implements the following attributes.

  my $args = $job->args;
  $job     = $job->args([]);

Arguments passed to task.

  my $id = $job->id;
  $job   = $job->id($id);

Job id.

  my $minion = $job->minion;
  $job       = $job->minion(Minion->new);

Minion object this job belongs to.

  my $retries = $job->retries;
  $job        = $job->retries(5);

Number of times job has been retried.

  my $task = $job->task;
  $job     = $job->task('foo');

Task name.

Minion::Job inherits all methods from Mojo::EventEmitter and implements the following new ones.

  my $app = $job->app;

Get application from "app" in Minion.

  # Longer version
  my $app = $job->minion->app;

  my $err = $job->execute;

Perform job in this process and return "undef" if the task was successful or an exception otherwise.

  # Perform job in foreground
  if (my $err = $job->execute) { $job->fail($err) }
  else                         { $job->finish }

  my $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.

  my $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.

  my $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:

  args => ['foo', 'bar']
    

Job arguments.

  attempts => 25
    

Number of times performing this job will be attempted.

  children => ['10026', '10027', '10028']
    

Jobs depending on this job.

  created => 784111777
    

Epoch time job was created.

  delayed => 784111777
    

Epoch time job was delayed to.

  finished => 784111777
    

Epoch time job was finished.

  notes => {foo => 'bar', baz => [1, 2, 3]}
    

Hash reference with arbitrary metadata for this job.

  parents => ['10023', '10024', '10025']
    

Jobs this job depends on.

  priority => 3
    

Job priority.

  queue => 'important'
    

Queue name.

  result => 'All went well!'
    

Job result.

  retried => 784111777
    

Epoch time job has been retried.

  retries => 3
    

Number of times job has been retried.

  started => 784111777
    

Epoch time job was started.

  state => 'inactive'
    

Current job state, usually "active", "failed", "finished" or "inactive".

  task => 'foo'
    

Task name.

  worker => '154'
    

Id of worker that is processing the job.

  my $bool = $job->is_finished;

Check if job performed with "start" is finished.

  $job->kill('INT');

Send a signal to job performed with "start".

  my $bool = $job->note(mojo => 'rocks', minion => 'too');

Change one or more metadata fields for this job. 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'});

  $job->perform;

Perform job in new process and wait for it to finish.

  my $pid = $job->pid;

Process id of the process spawned by "start" if available.

  my $bool = $job->remove;

Remove "failed", "finished" or "inactive" job from queue.

  my $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:

  attempts => 25
    

Number of times performing this job will be attempted.

  delay => 10
    

Delay job for this many seconds (from now), defaults to 0.

  parents => [$id1, $id2, $id3]
    

Jobs this job depends on.

  priority => 5
    

Job priority.

  queue => 'important'
    

Queue to put job in.

  $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;

  $job->stop;

Stop job performed with "start" immediately.

Minion, Mojolicious::Guides, <https://mojolicious.org>.

2019-02-05 perl v5.28.1