1Minion::Job(3) User Contributed Perl Documentation Minion::Job(3)
2
3
4
6 Minion::Job - Minion job
7
9 package MyApp::Task::Foo;
10 use Mojo::Base 'Minion::Job', -signatures;
11
12 sub run ($self, @args) {
13
14 # Magic here! :)
15 }
16
18 Minion::Job is a container for Minion jobs.
19
21 Minion::Job inherits all events from Mojo::EventEmitter and can emit
22 the following new ones.
23
24 cleanup
25 $job->on(cleanup => sub ($job) {
26 ...
27 });
28
29 Emitted in the process performing this job right before the process
30 will exit.
31
32 $job->on(cleanup => sub ($job) {
33 $job->app->log->debug("Process $$ is about to exit");
34 });
35
36 failed
37 $job->on(failed => sub ($job, $err) {
38 ...
39 });
40
41 Emitted in the worker process managing this job or the process
42 performing it, after it has transitioned to the "failed" state.
43
44 $job->on(failed => sub ($job, $err) {
45 say "Something went wrong: $err";
46 });
47
48 finish
49 $job->on(finish => sub ($job) {
50 ...
51 });
52
53 Emitted in the process performing this job if the task was successful.
54
55 $job->on(finish => sub ($job) {
56 my $id = $job->id;
57 my $task = $job->task;
58 $job->app->log->debug(qq{Job "$id" was performed with task "$task"});
59 });
60
61 finished
62 $job->on(finished => sub ($job, $result) {
63 ...
64 });
65
66 Emitted in the worker process managing this job or the process
67 performing it, after it has transitioned to the "finished" state.
68
69 $job->on(finished => sub ($job, $result) {
70 my $id = $job->id;
71 say "Job $id is finished.";
72 });
73
74 reap
75 $job->on(reap => sub ($job, $pid) {
76 ...
77 });
78
79 Emitted in the worker process managing this job, after the process
80 performing it has exited.
81
82 $job->on(reap => sub ($job, $pid) {
83 my $id = $job->id;
84 say "Job $id ran in process $pid";
85 });
86
87 spawn
88 $job->on(spawn => sub ($job, $pid) {
89 ...
90 });
91
92 Emitted in the worker process managing this job, after a new process
93 has been spawned for processing.
94
95 $job->on(spawn => sub ($job, $pid) {
96 my $id = $job->id;
97 say "Job $id running in process $pid";
98 });
99
100 start
101 $job->on(start => sub ($job) {
102 ...
103 });
104
105 Emitted in the process performing this job, after it has been spawned.
106
107 $job->on(start => sub ($job) {
108 $0 = $job->id;
109 });
110
112 Minion::Job implements the following attributes.
113
114 args
115 my $args = $job->args;
116 $job = $job->args([]);
117
118 Arguments passed to task.
119
120 id
121 my $id = $job->id;
122 $job = $job->id($id);
123
124 Job id.
125
126 minion
127 my $minion = $job->minion;
128 $job = $job->minion(Minion->new);
129
130 Minion object this job belongs to.
131
132 retries
133 my $retries = $job->retries;
134 $job = $job->retries(5);
135
136 Number of times job has been retried.
137
138 task
139 my $task = $job->task;
140 $job = $job->task('foo');
141
142 Task name.
143
145 Minion::Job inherits all methods from Mojo::EventEmitter and implements
146 the following new ones.
147
148 app
149 my $app = $job->app;
150
151 Get application from "app" in Minion.
152
153 # Longer version
154 my $app = $job->minion->app;
155
156 execute
157 my $err = $job->execute;
158
159 Perform job in this process and return "undef" if the task was
160 successful or an exception otherwise. Note that this method should
161 only be used to implement custom workers.
162
163 # Perform job in foreground
164 if (my $err = $job->execute) { $job->fail($err) }
165 else { $job->finish }
166
167 fail
168 my $bool = $job->fail;
169 my $bool = $job->fail('Something went wrong!');
170 my $bool = $job->fail({whatever => 'Something went wrong!'});
171
172 Transition from "active" to "failed" state with or without a result,
173 and if there are attempts remaining, transition back to "inactive" with
174 a delay based on "backoff" in Minion.
175
176 finish
177 my $bool = $job->finish;
178 my $bool = $job->finish('All went well!');
179 my $bool = $job->finish({whatever => 'All went well!'});
180
181 Transition from "active" to "finished" state with or without a result.
182
183 info
184 my $info = $job->info;
185
186 Get job information.
187
188 # Check job state
189 my $state = $job->info->{state};
190
191 # Get job metadata
192 my $progress = $job->info->{notes}{progress};
193
194 # Get job result
195 my $result = $job->info->{result};
196
197 These fields are currently available:
198
199 args
200 args => ['foo', 'bar']
201
202 Job arguments.
203
204 attempts
205 attempts => 25
206
207 Number of times performing this job will be attempted.
208
209 children
210 children => ['10026', '10027', '10028']
211
212 Jobs depending on this job.
213
214 created
215 created => 784111777
216
217 Epoch time job was created.
218
219 delayed
220 delayed => 784111777
221
222 Epoch time job was delayed to.
223
224 expires
225 expires => 784111777
226
227 Epoch time job is valid until before it expires.
228
229 finished
230 finished => 784111777
231
232 Epoch time job was finished.
233
234 lax
235 lax => 0
236
237 Existing jobs this job depends on may also have failed to allow for
238 it to be processed.
239
240 notes
241 notes => {foo => 'bar', baz => [1, 2, 3]}
242
243 Hash reference with arbitrary metadata for this job.
244
245 parents
246 parents => ['10023', '10024', '10025']
247
248 Jobs this job depends on.
249
250 priority
251 priority => 3
252
253 Job priority.
254
255 queue
256 queue => 'important'
257
258 Queue name.
259
260 result
261 result => 'All went well!'
262
263 Job result.
264
265 retried
266 retried => 784111777
267
268 Epoch time job has been retried.
269
270 retries
271 retries => 3
272
273 Number of times job has been retried.
274
275 started
276 started => 784111777
277
278 Epoch time job was started.
279
280 state
281 state => 'inactive'
282
283 Current job state, usually "active", "failed", "finished" or
284 "inactive".
285
286 task
287 task => 'foo'
288
289 Task name.
290
291 time
292 time => 784111777
293
294 Server time.
295
296 worker
297 worker => '154'
298
299 Id of worker that is processing the job.
300
301 is_finished
302 my $bool = $job->is_finished;
303
304 Check if job performed with "start" is finished. Note that this method
305 should only be used to implement custom workers.
306
307 kill
308 $job->kill('INT');
309
310 Send a signal to job performed with "start". Note that this method
311 should only be used to implement custom workers.
312
313 note
314 my $bool = $job->note(mojo => 'rocks', minion => 'too');
315
316 Change one or more metadata fields for this job. Setting a value to
317 "undef" will remove the field. The new values will get serialized by
318 "backend" in Minion (often with Mojo::JSON), so you shouldn't send
319 objects and be careful with binary data, nested data structures with
320 hash and array references are fine though.
321
322 # Share progress information
323 $job->note(progress => 95);
324
325 # Share stats
326 $job->note(stats => {utime => '0.012628', stime => '0.002429'});
327
328 parents
329 my $parents = $job->parents;
330
331 Return a Mojo::Collection object containing all jobs this job depends
332 on as Minion::Job objects.
333
334 # Check parent state
335 for my $parent ($job->parents->each) {
336 my $info = $parent->info;
337 say "$info->{id}: $info->{state}";
338 }
339
340 perform
341 $job->perform;
342
343 Perform job in new process and wait for it to finish. Note that this
344 method should only be used to implement custom workers.
345
346 pid
347 my $pid = $job->pid;
348
349 Process id of the process spawned by "start" if available. Note that
350 this method should only be used to implement custom workers.
351
352 remove
353 my $bool = $job->remove;
354
355 Remove "failed", "finished" or "inactive" job from queue.
356
357 retry
358 my $bool = $job->retry;
359 my $bool = $job->retry({delay => 10});
360
361 Transition job back to "inactive" state, already "inactive" jobs may
362 also be retried to change options.
363
364 These options are currently available:
365
366 attempts
367 attempts => 25
368
369 Number of times performing this job will be attempted.
370
371 delay
372 delay => 10
373
374 Delay job for this many seconds (from now), defaults to 0.
375
376 expire
377 expire => 300
378
379 Job is valid for this many seconds (from now) before it expires.
380
381 lax
382 lax => 1
383
384 Existing jobs this job depends on may also have transitioned to the
385 "failed" state to allow for it to be processed, defaults to "false".
386 Note that this option is EXPERIMENTAL and might change without
387 warning!
388
389 parents
390 parents => [$id1, $id2, $id3]
391
392 Jobs this job depends on.
393
394 priority
395 priority => 5
396
397 Job priority.
398
399 queue
400 queue => 'important'
401
402 Queue to put job in.
403
404 run
405 $job->run(@args);
406
407 Task to perform by this job. Meant to be overloaded in a subclass to
408 create a custom task class. Note that this method is EXPERIMENTAL and
409 might change without warning!
410
411 start
412 $job = $job->start;
413
414 Perform job in new process, but do not wait for it to finish. Note that
415 this method should only be used to implement custom workers.
416
417 # Perform two jobs concurrently
418 $job1->start;
419 $job2->start;
420 my ($first, $second);
421 sleep 1
422 until $first ||= $job1->is_finished and $second ||= $job2->is_finished;
423
424 stop
425 $job->stop;
426
427 Stop job performed with "start" immediately. Note that this method
428 should only be used to implement custom workers.
429
431 Minion, Minion::Guide, <https://minion.pm>, Mojolicious::Guides,
432 <https://mojolicious.org>.
433
434
435
436perl v5.34.0 2021-07-22 Minion::Job(3)