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