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