1Minion::Job(3)        User Contributed Perl Documentation       Minion::Job(3)
2
3
4

NAME

6       Minion::Job - Minion job
7

SYNOPSIS

9         package MyApp::Task::Foo;
10         use Mojo::Base 'Minion::Job';
11
12         sub run {
13           my ($self, @args) = @_;
14
15           # Magic here! :)
16         }
17

DESCRIPTION

19       Minion::Job is a container for Minion jobs.
20

EVENTS

22       Minion::Job inherits all events from Mojo::EventEmitter and can emit
23       the following new ones.
24
25   cleanup
26         $job->on(cleanup => sub {
27           my $job = shift;
28           ...
29         });
30
31       Emitted in the process performing this job right before the process
32       will exit.
33
34         $job->on(cleanup => sub {
35           my $job = shift;
36           $job->app->log->debug("Process $$ is about to exit");
37         });
38
39   failed
40         $job->on(failed => sub {
41           my ($job, $err) = @_;
42           ...
43         });
44
45       Emitted in the worker process managing this job or the process
46       performing it, after it has transitioned to the "failed" state.
47
48         $job->on(failed => sub {
49           my ($job, $err) = @_;
50           say "Something went wrong: $err";
51         });
52
53   finish
54         $job->on(finish => sub {
55           my $job = shift;
56           ...
57         });
58
59       Emitted in the process performing this job if the task was successful.
60
61         $job->on(finish => sub {
62           my $job  = shift;
63           my $id   = $job->id;
64           my $task = $job->task;
65           $job->app->log->debug(qq{Job "$id" was performed with task "$task"});
66         });
67
68   finished
69         $job->on(finished => sub {
70           my ($job, $result) = @_;
71           ...
72         });
73
74       Emitted in the worker process managing this job or the process
75       performing it, after it has transitioned to the "finished" state.
76
77         $job->on(finished => sub {
78           my ($job, $result) = @_;
79           my $id = $job->id;
80           say "Job $id is finished.";
81         });
82
83   reap
84         $job->on(reap => sub {
85           my ($job, $pid) = @_;
86           ...
87         });
88
89       Emitted in the worker process managing this job, after the process
90       performing it has exited.
91
92         $job->on(reap => sub {
93           my ($job, $pid) = @_;
94           my $id = $job->id;
95           say "Job $id ran in process $pid";
96         });
97
98   spawn
99         $job->on(spawn => sub {
100           my ($job, $pid) = @_;
101           ...
102         });
103
104       Emitted in the worker process managing this job, after a new process
105       has been spawned for processing.
106
107         $job->on(spawn => sub {
108           my ($job, $pid) = @_;
109           my $id = $job->id;
110           say "Job $id running in process $pid";
111         });
112
113   start
114         $job->on(start => sub {
115           my $job = shift;
116           ...
117         });
118
119       Emitted in the process performing this job, after it has been spawned.
120
121         $job->on(start => sub {
122           my $job = shift;
123           $0 = $job->id;
124         });
125

ATTRIBUTES

127       Minion::Job implements the following attributes.
128
129   args
130         my $args = $job->args;
131         $job     = $job->args([]);
132
133       Arguments passed to task.
134
135   id
136         my $id = $job->id;
137         $job   = $job->id($id);
138
139       Job id.
140
141   minion
142         my $minion = $job->minion;
143         $job       = $job->minion(Minion->new);
144
145       Minion object this job belongs to.
146
147   retries
148         my $retries = $job->retries;
149         $job        = $job->retries(5);
150
151       Number of times job has been retried.
152
153   task
154         my $task = $job->task;
155         $job     = $job->task('foo');
156
157       Task name.
158

METHODS

160       Minion::Job inherits all methods from Mojo::EventEmitter and implements
161       the following new ones.
162
163   app
164         my $app = $job->app;
165
166       Get application from "app" in Minion.
167
168         # Longer version
169         my $app = $job->minion->app;
170
171   execute
172         my $err = $job->execute;
173
174       Perform job in this process and return "undef" if the task was
175       successful or an exception otherwise.  Note that this method should
176       only be used to implement custom workers.
177
178         # Perform job in foreground
179         if (my $err = $job->execute) { $job->fail($err) }
180         else                         { $job->finish }
181
182   fail
183         my $bool = $job->fail;
184         my $bool = $job->fail('Something went wrong!');
185         my $bool = $job->fail({whatever => 'Something went wrong!'});
186
187       Transition from "active" to "failed" state with or without a result,
188       and if there are attempts remaining, transition back to "inactive" with
189       a delay based on "backoff" in Minion.
190
191   finish
192         my $bool = $job->finish;
193         my $bool = $job->finish('All went well!');
194         my $bool = $job->finish({whatever => 'All went well!'});
195
196       Transition from "active" to "finished" state with or without a result.
197
198   info
199         my $info = $job->info;
200
201       Get job information.
202
203         # Check job state
204         my $state = $job->info->{state};
205
206         # Get job metadata
207         my $progress = $job->info->{notes}{progress};
208
209         # Get job result
210         my $result = $job->info->{result};
211
212       These fields are currently available:
213
214       args
215           args => ['foo', 'bar']
216
217         Job arguments.
218
219       attempts
220           attempts => 25
221
222         Number of times performing this job will be attempted.
223
224       children
225           children => ['10026', '10027', '10028']
226
227         Jobs depending on this job.
228
229       created
230           created => 784111777
231
232         Epoch time job was created.
233
234       delayed
235           delayed => 784111777
236
237         Epoch time job was delayed to.
238
239       expires
240           expires => 784111777
241
242         Epoch time job is valid until before it expires.
243
244       finished
245           finished => 784111777
246
247         Epoch time job was finished.
248
249       lax
250           lax => 0
251
252         Existing jobs this job depends on may also have failed to allow for
253         it to be processed.
254
255       notes
256           notes => {foo => 'bar', baz => [1, 2, 3]}
257
258         Hash reference with arbitrary metadata for this job.
259
260       parents
261           parents => ['10023', '10024', '10025']
262
263         Jobs this job depends on.
264
265       priority
266           priority => 3
267
268         Job priority.
269
270       queue
271           queue => 'important'
272
273         Queue name.
274
275       result
276           result => 'All went well!'
277
278         Job result.
279
280       retried
281           retried => 784111777
282
283         Epoch time job has been retried.
284
285       retries
286           retries => 3
287
288         Number of times job has been retried.
289
290       started
291           started => 784111777
292
293         Epoch time job was started.
294
295       state
296           state => 'inactive'
297
298         Current job state, usually "active", "failed", "finished" or
299         "inactive".
300
301       task
302           task => 'foo'
303
304         Task name.
305
306       time
307           time => 784111777
308
309         Server time.
310
311       worker
312           worker => '154'
313
314         Id of worker that is processing the job.
315
316   is_finished
317         my $bool = $job->is_finished;
318
319       Check if job performed with "start" is finished. Note that this method
320       should only be used to implement custom workers.
321
322   kill
323         $job->kill('INT');
324
325       Send a signal to job performed with "start". Note that this method
326       should only be used to implement custom workers.
327
328   note
329         my $bool = $job->note(mojo => 'rocks', minion => 'too');
330
331       Change one or more metadata fields for this job. Setting a value to
332       "undef" will remove the field. The new values will get serialized by
333       "backend" in Minion (often with Mojo::JSON), so you shouldn't send
334       objects and be careful with binary data, nested data structures with
335       hash and array references are fine though.
336
337         # Share progress information
338         $job->note(progress => 95);
339
340         # Share stats
341         $job->note(stats => {utime => '0.012628', stime => '0.002429'});
342
343   parents
344         my $parents = $job->parents;
345
346       Return a Mojo::Collection object containing all jobs this job depends
347       on as Minion::Job objects.
348
349         # Check parent state
350         for my $parent ($job->parents->each) {
351           my $info = $parent->info;
352           say "$info->{id}: $info->{state}";
353         }
354
355   perform
356         $job->perform;
357
358       Perform job in new process and wait for it to finish. Note that this
359       method should only be used to implement custom workers.
360
361   pid
362         my $pid = $job->pid;
363
364       Process id of the process spawned by "start" if available. Note that
365       this method should only be used to implement custom workers.
366
367   remove
368         my $bool = $job->remove;
369
370       Remove "failed", "finished" or "inactive" job from queue.
371
372   retry
373         my $bool = $job->retry;
374         my $bool = $job->retry({delay => 10});
375
376       Transition job back to "inactive" state, already "inactive" jobs may
377       also be retried to change options.
378
379       These options are currently available:
380
381       attempts
382           attempts => 25
383
384         Number of times performing this job will be attempted.
385
386       delay
387           delay => 10
388
389         Delay job for this many seconds (from now), defaults to 0.
390
391       expire
392           expire => 300
393
394         Job is valid for this many seconds (from now) before it expires. Note
395         that this option is EXPERIMENTAL and might change without warning!
396
397       lax
398           lax => 1
399
400         Existing jobs this job depends on may also have transitioned to the
401         "failed" state to allow for it to be processed, defaults to "false".
402         Note that this option is EXPERIMENTAL and might change without
403         warning!
404
405       parents
406           parents => [$id1, $id2, $id3]
407
408         Jobs this job depends on.
409
410       priority
411           priority => 5
412
413         Job priority.
414
415       queue
416           queue => 'important'
417
418         Queue to put job in.
419
420   run
421         $job->run(@args);
422
423       Task to perform by this job. Meant to be overloaded in a subclass to
424       create a custom task class. Note that this method is EXPERIMENTAL and
425       might change without warning!
426
427   start
428         $job = $job->start;
429
430       Perform job in new process, but do not wait for it to finish. Note that
431       this method should only be used to implement custom workers.
432
433         # Perform two jobs concurrently
434         $job1->start;
435         $job2->start;
436         my ($first, $second);
437         sleep 1
438           until $first ||= $job1->is_finished and $second ||= $job2->is_finished;
439
440   stop
441         $job->stop;
442
443       Stop job performed with "start" immediately. Note that this method
444       should only be used to implement custom workers.
445

SEE ALSO

447       Minion, <https://minion.pm>, Mojolicious::Guides,
448       <https://mojolicious.org>.
449
450
451
452perl v5.32.0                      2020-08-02                    Minion::Job(3)
Impressum