1Minion::Backend::Pg(3)User Contributed Perl DocumentationMinion::Backend::Pg(3)
2
3
4
6 Minion::Backend::Pg - PostgreSQL backend
7
9 use Minion::Backend::Pg;
10
11 my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test');
12
14 Minion::Backend::Pg is a backend for Minion based on Mojo::Pg. All
15 necessary tables will be created automatically with a set of migrations
16 named "minion". Note that this backend uses many bleeding edge
17 features, so only the latest, stable version of PostgreSQL is fully
18 supported.
19
21 Minion::Backend::Pg inherits all attributes from Minion::Backend and
22 implements the following new ones.
23
24 pg
25 my $pg = $backend->pg;
26 $backend = $backend->pg(Mojo::Pg->new);
27
28 Mojo::Pg object used to store all data.
29
31 Minion::Backend::Pg inherits all methods from Minion::Backend and
32 implements the following new ones.
33
34 broadcast
35 my $bool = $backend->broadcast('some_command');
36 my $bool = $backend->broadcast('some_command', [@args]);
37 my $bool = $backend->broadcast('some_command', [@args], [$id1, $id2, $id3]);
38
39 Broadcast remote control command to one or more workers.
40
41 dequeue
42 my $job_info = $backend->dequeue($worker_id, 0.5);
43 my $job_info = $backend->dequeue($worker_id, 0.5, {queues => ['important']});
44
45 Wait a given amount of time in seconds for a job, dequeue it and
46 transition from "inactive" to "active" state, or return "undef" if
47 queues were empty.
48
49 These options are currently available:
50
51 id
52 id => '10023'
53
54 Dequeue a specific job.
55
56 queues
57 queues => ['important']
58
59 One or more queues to dequeue jobs from, defaults to "default".
60
61 These fields are currently available:
62
63 args
64 args => ['foo', 'bar']
65
66 Job arguments.
67
68 id
69 id => '10023'
70
71 Job ID.
72
73 retries
74 retries => 3
75
76 Number of times job has been retried.
77
78 task
79 task => 'foo'
80
81 Task name.
82
83 enqueue
84 my $job_id = $backend->enqueue('foo');
85 my $job_id = $backend->enqueue(foo => [@args]);
86 my $job_id = $backend->enqueue(foo => [@args] => {priority => 1});
87
88 Enqueue a new job with "inactive" state.
89
90 These options are currently available:
91
92 attempts
93 attempts => 25
94
95 Number of times performing this job will be attempted, with a delay
96 based on "backoff" in Minion after the first attempt, defaults to 1.
97
98 delay
99 delay => 10
100
101 Delay job for this many seconds (from now), defaults to 0.
102
103 notes
104 notes => {foo => 'bar', baz => [1, 2, 3]}
105
106 Hash reference with arbitrary metadata for this job.
107
108 parents
109 parents => [$id1, $id2, $id3]
110
111 One or more existing jobs this job depends on, and that need to have
112 transitioned to the state "finished" before it can be processed.
113
114 priority
115 priority => 5
116
117 Job priority, defaults to 0. Jobs with a higher priority get
118 performed first.
119
120 queue
121 queue => 'important'
122
123 Queue to put job in, defaults to "default".
124
125 fail_job
126 my $bool = $backend->fail_job($job_id, $retries);
127 my $bool = $backend->fail_job($job_id, $retries, 'Something went wrong!');
128 my $bool = $backend->fail_job(
129 $job_id, $retries, {whatever => 'Something went wrong!'});
130
131 Transition from "active" to "failed" state with or without a result,
132 and if there are attempts remaining, transition back to "inactive" with
133 a delay based on "backoff" in Minion.
134
135 finish_job
136 my $bool = $backend->finish_job($job_id, $retries);
137 my $bool = $backend->finish_job($job_id, $retries, 'All went well!');
138 my $bool = $backend->finish_job(
139 $job_id, $retries, {whatever => 'All went well!'});
140
141 Transition from "active" to "finished" state with or without a result.
142
143 history
144 my $history = $backend->history;
145
146 Get history information for job queue. Note that this method is
147 EXPERIMENTAL and might change without warning!
148
149 These fields are currently available:
150
151 daily
152 daily => [{epoch => 12345, finished_jobs => 95, failed_jobs => 2}, ...]
153
154 Hourly counts for processed jobs from the past day.
155
156 list_jobs
157 my $results = $backend->list_jobs($offset, $limit);
158 my $results = $backend->list_jobs($offset, $limit, {states => ['inactive']});
159
160 Returns the information about jobs in batches.
161
162 # Get the total number of results (without limit)
163 my $num = $backend->list_jobs(0, 100, {queues => ['important']})->{total};
164
165 # Check job state
166 my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
167 my $state = $results->{jobs}[0]{state};
168
169 # Get job result
170 my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
171 my $result = $results->{jobs}[0]{result};
172
173 These options are currently available:
174
175 ids
176 ids => ['23', '24']
177
178 List only jobs with these ids.
179
180 notes
181 notes => ['foo', 'bar']
182
183 List only jobs with one of these notes. Note that this option is
184 EXPERIMENTAL and might change without warning!
185
186 queues
187 queues => ['important', 'unimportant']
188
189 List only jobs in these queues.
190
191 states
192 states => ['inactive', 'active']
193
194 List only jobs in these states.
195
196 tasks
197 tasks => ['foo', 'bar']
198
199 List only jobs for these tasks.
200
201 These fields are currently available:
202
203 args
204 args => ['foo', 'bar']
205
206 Job arguments.
207
208 attempts
209 attempts => 25
210
211 Number of times performing this job will be attempted.
212
213 children
214 children => ['10026', '10027', '10028']
215
216 Jobs depending on this job.
217
218 created
219 created => 784111777
220
221 Epoch time job was created.
222
223 delayed
224 delayed => 784111777
225
226 Epoch time job was delayed to.
227
228 finished
229 finished => 784111777
230
231 Epoch time job was finished.
232
233 id
234 id => 10025
235
236 Job id.
237
238 notes
239 notes => {foo => 'bar', baz => [1, 2, 3]}
240
241 Hash reference with arbitrary metadata for this job.
242
243 parents
244 parents => ['10023', '10024', '10025']
245
246 Jobs this job depends on.
247
248 priority
249 priority => 3
250
251 Job priority.
252
253 queue
254 queue => 'important'
255
256 Queue name.
257
258 result
259 result => 'All went well!'
260
261 Job result.
262
263 retried
264 retried => 784111777
265
266 Epoch time job has been retried.
267
268 retries
269 retries => 3
270
271 Number of times job has been retried.
272
273 started
274 started => 784111777
275
276 Epoch time job was started.
277
278 state
279 state => 'inactive'
280
281 Current job state, usually "active", "failed", "finished" or
282 "inactive".
283
284 task
285 task => 'foo'
286
287 Task name.
288
289 time
290 time => 78411177
291
292 Server time.
293
294 worker
295 worker => '154'
296
297 Id of worker that is processing the job.
298
299 list_locks
300 my $results = $backend->list_locks($offset, $limit);
301 my $results = $backend->list_locks($offset, $limit, {names => ['foo']});
302
303 Returns information about locks in batches.
304
305 # Get the total number of results (without limit)
306 my $num = $backend->list_locks(0, 100, {names => ['bar']})->{total};
307
308 # Check expiration time
309 my $results = $backend->list_locks(0, 1, {names => ['foo']});
310 my $expires = $results->{locks}[0]{expires};
311
312 These options are currently available:
313
314 names
315 names => ['foo', 'bar']
316
317 List only locks with these names.
318
319 These fields are currently available:
320
321 expires
322 expires => 784111777
323
324 Epoch time this lock will expire.
325
326 name
327 name => 'foo'
328
329 Lock name.
330
331 list_workers
332 my $results = $backend->list_workers($offset, $limit);
333 my $results = $backend->list_workers($offset, $limit, {ids => [23]});
334
335 Returns information about workers in batches.
336
337 # Get the total number of results (without limit)
338 my $num = $backend->list_workers(0, 100)->{total};
339
340 # Check worker host
341 my $results = $backend->list_workers(0, 1, {ids => [$worker_id]});
342 my $host = $results->{workers}[0]{host};
343
344 These options are currently available:
345
346 ids
347 ids => ['23', '24']
348
349 List only workers with these ids.
350
351 These fields are currently available:
352
353 id
354 id => 22
355
356 Worker id.
357
358 host
359 host => 'localhost'
360
361 Worker host.
362
363 jobs
364 jobs => ['10023', '10024', '10025', '10029']
365
366 Ids of jobs the worker is currently processing.
367
368 notified
369 notified => 784111777
370
371 Epoch time worker sent the last heartbeat.
372
373 pid
374 pid => 12345
375
376 Process id of worker.
377
378 started
379 started => 784111777
380
381 Epoch time worker was started.
382
383 status
384 status => {queues => ['default', 'important']}
385
386 Hash reference with whatever status information the worker would like
387 to share.
388
389 lock
390 my $bool = $backend->lock('foo', 3600);
391 my $bool = $backend->lock('foo', 3600, {limit => 20});
392
393 Try to acquire a named lock that will expire automatically after the
394 given amount of time in seconds. An expiration time of 0 can be used to
395 check if a named lock already exists without creating one.
396
397 These options are currently available:
398
399 limit
400 limit => 20
401
402 Number of shared locks with the same name that can be active at the
403 same time, defaults to 1.
404
405 new
406 my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test');
407 my $backend = Minion::Backend::Pg->new(Mojo::Pg->new);
408
409 Construct a new Minion::Backend::Pg object.
410
411 note
412 my $bool = $backend->note($job_id, {mojo => 'rocks', minion => 'too'});
413
414 Change one or more metadata fields for a job. Setting a value to
415 "undef" will remove the field.
416
417 receive
418 my $commands = $backend->receive($worker_id);
419
420 Receive remote control commands for worker.
421
422 register_worker
423 my $worker_id = $backend->register_worker;
424 my $worker_id = $backend->register_worker($worker_id);
425 my $worker_id = $backend->register_worker(
426 $worker_id, {status => {queues => ['default', 'important']}});
427
428 Register worker or send heartbeat to show that this worker is still
429 alive.
430
431 These options are currently available:
432
433 status
434 status => {queues => ['default', 'important']}
435
436 Hash reference with whatever status information the worker would like
437 to share.
438
439 remove_job
440 my $bool = $backend->remove_job($job_id);
441
442 Remove "failed", "finished" or "inactive" job from queue.
443
444 repair
445 $backend->repair;
446
447 Repair worker registry and job queue if necessary.
448
449 reset
450 $backend->reset;
451
452 Reset job queue.
453
454 retry_job
455 my $bool = $backend->retry_job($job_id, $retries);
456 my $bool = $backend->retry_job($job_id, $retries, {delay => 10});
457
458 Transition job back to "inactive" state, already "inactive" jobs may
459 also be retried to change options.
460
461 These options are currently available:
462
463 attempts
464 attempts => 25
465
466 Number of times performing this job will be attempted.
467
468 delay
469 delay => 10
470
471 Delay job for this many seconds (from now), defaults to 0.
472
473 parents
474 parents => [$id1, $id2, $id3]
475
476 Jobs this job depends on.
477
478 priority
479 priority => 5
480
481 Job priority.
482
483 queue
484 queue => 'important'
485
486 Queue to put job in.
487
488 stats
489 my $stats = $backend->stats;
490
491 Get statistics for the job queue.
492
493 These fields are currently available:
494
495 active_jobs
496 active_jobs => 100
497
498 Number of jobs in "active" state.
499
500 active_locks
501 active_locks => 100
502
503 Number of active named locks.
504
505 active_workers
506 active_workers => 100
507
508 Number of workers that are currently processing a job.
509
510 delayed_jobs
511 delayed_jobs => 100
512
513 Number of jobs in "inactive" state that are scheduled to run at
514 specific time in the future. Note that this field is EXPERIMENTAL and
515 might change without warning!
516
517 enqueued_jobs
518 enqueued_jobs => 100000
519
520 Rough estimate of how many jobs have ever been enqueued. Note that
521 this field is EXPERIMENTAL and might change without warning!
522
523 failed_jobs
524 failed_jobs => 100
525
526 Number of jobs in "failed" state.
527
528 finished_jobs
529 finished_jobs => 100
530
531 Number of jobs in "finished" state.
532
533 inactive_jobs
534 inactive_jobs => 100
535
536 Number of jobs in "inactive" state.
537
538 inactive_workers
539 inactive_workers => 100
540
541 Number of workers that are currently not processing a job.
542
543 uptime
544 uptime => 1000
545
546 Uptime in seconds.
547
548 unlock
549 my $bool = $backend->unlock('foo');
550
551 Release a named lock.
552
553 unregister_worker
554 $backend->unregister_worker($worker_id);
555
556 Unregister worker.
557
559 Minion, Mojolicious::Guides, <https://mojolicious.org>.
560
561
562
563perl v5.30.0 2019-09-01 Minion::Backend::Pg(3)