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