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 with or without a result,
169 and if there are attempts remaining, transition back to "inactive" with
170 a delay based on "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 with or without a result.
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 kill
287 $job->kill('INT');
288
289 Send a signal to job performed with "start".
290
291 note
292 my $bool = $job->note(mojo => 'rocks', minion => 'too');
293
294 Change one or more metadata fields for this job. The new values will
295 get serialized by "backend" in Minion (often with Mojo::JSON), so you
296 shouldn't send objects and be careful with binary data, nested data
297 structures with hash and array references are fine though.
298
299 # Share progress information
300 $job->note(progress => 95);
301
302 # Share stats
303 $job->note(stats => {utime => '0.012628', stime => '0.002429'});
304
305 perform
306 $job->perform;
307
308 Perform job in new process and wait for it to finish.
309
310 pid
311 my $pid = $job->pid;
312
313 Process id of the process spawned by "start" if available.
314
315 remove
316 my $bool = $job->remove;
317
318 Remove "failed", "finished" or "inactive" job from queue.
319
320 retry
321 my $bool = $job->retry;
322 my $bool = $job->retry({delay => 10});
323
324 Transition job back to "inactive" state, already "inactive" jobs may
325 also be retried to change options.
326
327 These options are currently available:
328
329 attempts
330 attempts => 25
331
332 Number of times performing this job will be attempted.
333
334 delay
335 delay => 10
336
337 Delay job for this many seconds (from now), defaults to 0.
338
339 parents
340 parents => [$id1, $id2, $id3]
341
342 Jobs this job depends on.
343
344 priority
345 priority => 5
346
347 Job priority.
348
349 queue
350 queue => 'important'
351
352 Queue to put job in.
353
354 start
355 $job = $job->start;
356
357 Perform job in new process, but do not wait for it to finish.
358
359 # Perform two jobs concurrently
360 $job1->start;
361 $job2->start;
362 my ($first, $second);
363 sleep 1
364 until $first ||= $job1->is_finished and $second ||= $job2->is_finished;
365
366 stop
367 $job->stop;
368
369 Stop job performed with "start" immediately.
370
372 Minion, Mojolicious::Guides, <https://mojolicious.org>.
373
374
375
376perl v5.28.1 2019-02-02 Minion::Job(3)