1Minion::Backend::Pg(3)User Contributed Perl DocumentationMinion::Backend::Pg(3)
2
3
4

NAME

6       Minion::Backend::Pg - PostgreSQL backend
7

SYNOPSIS

9         use Minion::Backend::Pg;
10
11         my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test');
12

DESCRIPTION

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

ATTRIBUTES

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

METHODS

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       expire
104           expire => 300
105
106         Job is valid for this many seconds (from now) before it expires. Note
107         that this option is EXPERIMENTAL and might change without warning!
108
109       lax
110           lax => 1
111
112         Existing jobs this job depends on may also have transitioned to the
113         "failed" state to allow for it to be processed, defaults to "false".
114         Note that this option is EXPERIMENTAL and might change without
115         warning!
116
117       notes
118           notes => {foo => 'bar', baz => [1, 2, 3]}
119
120         Hash reference with arbitrary metadata for this job.
121
122       parents
123           parents => [$id1, $id2, $id3]
124
125         One or more existing jobs this job depends on, and that need to have
126         transitioned to the state "finished" before it can be processed.
127
128       priority
129           priority => 5
130
131         Job priority, defaults to 0. Jobs with a higher priority get
132         performed first.
133
134       queue
135           queue => 'important'
136
137         Queue to put job in, defaults to "default".
138
139   fail_job
140         my $bool = $backend->fail_job($job_id, $retries);
141         my $bool = $backend->fail_job($job_id, $retries, 'Something went wrong!');
142         my $bool = $backend->fail_job(
143           $job_id, $retries, {whatever => 'Something went wrong!'});
144
145       Transition from "active" to "failed" state with or without a result,
146       and if there are attempts remaining, transition back to "inactive" with
147       a delay based on "backoff" in Minion.
148
149   finish_job
150         my $bool = $backend->finish_job($job_id, $retries);
151         my $bool = $backend->finish_job($job_id, $retries, 'All went well!');
152         my $bool = $backend->finish_job(
153           $job_id, $retries, {whatever => 'All went well!'});
154
155       Transition from "active" to "finished" state with or without a result.
156
157   history
158         my $history = $backend->history;
159
160       Get history information for job queue.
161
162       These fields are currently available:
163
164       daily
165           daily => [{epoch => 12345, finished_jobs => 95, failed_jobs => 2}, ...]
166
167         Hourly counts for processed jobs from the past day.
168
169   list_jobs
170         my $results = $backend->list_jobs($offset, $limit);
171         my $results = $backend->list_jobs($offset, $limit, {states => ['inactive']});
172
173       Returns the information about jobs in batches.
174
175         # Get the total number of results (without limit)
176         my $num = $backend->list_jobs(0, 100, {queues => ['important']})->{total};
177
178         # Check job state
179         my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
180         my $state = $results->{jobs}[0]{state};
181
182         # Get job result
183         my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
184         my $result  = $results->{jobs}[0]{result};
185
186       These options are currently available:
187
188       before
189           before => 23
190
191         List only jobs before this id.
192
193       ids
194           ids => ['23', '24']
195
196         List only jobs with these ids.
197
198       notes
199           notes => ['foo', 'bar']
200
201         List only jobs with one of these notes.
202
203       queues
204           queues => ['important', 'unimportant']
205
206         List only jobs in these queues.
207
208       states
209           states => ['inactive', 'active']
210
211         List only jobs in these states.
212
213       tasks
214           tasks => ['foo', 'bar']
215
216         List only jobs for these tasks.
217
218       These fields are currently available:
219
220       args
221           args => ['foo', 'bar']
222
223         Job arguments.
224
225       attempts
226           attempts => 25
227
228         Number of times performing this job will be attempted.
229
230       children
231           children => ['10026', '10027', '10028']
232
233         Jobs depending on this job.
234
235       created
236           created => 784111777
237
238         Epoch time job was created.
239
240       delayed
241           delayed => 784111777
242
243         Epoch time job was delayed to.
244
245       expires
246           expires => 784111777
247
248         Epoch time job is valid until before it expires.
249
250       finished
251           finished => 784111777
252
253         Epoch time job was finished.
254
255       id
256           id => 10025
257
258         Job id.
259
260       lax
261           lax => 0
262
263         Existing jobs this job depends on may also have failed to allow for
264         it to be processed.
265
266       notes
267           notes => {foo => 'bar', baz => [1, 2, 3]}
268
269         Hash reference with arbitrary metadata for this job.
270
271       parents
272           parents => ['10023', '10024', '10025']
273
274         Jobs this job depends on.
275
276       priority
277           priority => 3
278
279         Job priority.
280
281       queue
282           queue => 'important'
283
284         Queue name.
285
286       result
287           result => 'All went well!'
288
289         Job result.
290
291       retried
292           retried => 784111777
293
294         Epoch time job has been retried.
295
296       retries
297           retries => 3
298
299         Number of times job has been retried.
300
301       started
302           started => 784111777
303
304         Epoch time job was started.
305
306       state
307           state => 'inactive'
308
309         Current job state, usually "active", "failed", "finished" or
310         "inactive".
311
312       task
313           task => 'foo'
314
315         Task name.
316
317       time
318           time => 78411177
319
320         Server time.
321
322       worker
323           worker => '154'
324
325         Id of worker that is processing the job.
326
327   list_locks
328         my $results = $backend->list_locks($offset, $limit);
329         my $results = $backend->list_locks($offset, $limit, {names => ['foo']});
330
331       Returns information about locks in batches.
332
333         # Get the total number of results (without limit)
334         my $num = $backend->list_locks(0, 100, {names => ['bar']})->{total};
335
336         # Check expiration time
337         my $results = $backend->list_locks(0, 1, {names => ['foo']});
338         my $expires = $results->{locks}[0]{expires};
339
340       These options are currently available:
341
342       names
343           names => ['foo', 'bar']
344
345         List only locks with these names.
346
347       These fields are currently available:
348
349       expires
350           expires => 784111777
351
352         Epoch time this lock will expire.
353
354       name
355           name => 'foo'
356
357         Lock name.
358
359   list_workers
360         my $results = $backend->list_workers($offset, $limit);
361         my $results = $backend->list_workers($offset, $limit, {ids => [23]});
362
363       Returns information about workers in batches.
364
365         # Get the total number of results (without limit)
366         my $num = $backend->list_workers(0, 100)->{total};
367
368         # Check worker host
369         my $results = $backend->list_workers(0, 1, {ids => [$worker_id]});
370         my $host    = $results->{workers}[0]{host};
371
372       These options are currently available:
373
374       before
375           before => 23
376
377         List only workers before this id.
378
379       ids
380           ids => ['23', '24']
381
382         List only workers with these ids.
383
384       These fields are currently available:
385
386       id
387           id => 22
388
389         Worker id.
390
391       host
392           host => 'localhost'
393
394         Worker host.
395
396       jobs
397           jobs => ['10023', '10024', '10025', '10029']
398
399         Ids of jobs the worker is currently processing.
400
401       notified
402           notified => 784111777
403
404         Epoch time worker sent the last heartbeat.
405
406       pid
407           pid => 12345
408
409         Process id of worker.
410
411       started
412           started => 784111777
413
414         Epoch time worker was started.
415
416       status
417           status => {queues => ['default', 'important']}
418
419         Hash reference with whatever status information the worker would like
420         to share.
421
422   lock
423         my $bool = $backend->lock('foo', 3600);
424         my $bool = $backend->lock('foo', 3600, {limit => 20});
425
426       Try to acquire a named lock that will expire automatically after the
427       given amount of time in seconds. An expiration time of 0 can be used to
428       check if a named lock already exists without creating one.
429
430       These options are currently available:
431
432       limit
433           limit => 20
434
435         Number of shared locks with the same name that can be active at the
436         same time, defaults to 1.
437
438   new
439         my $backend = Minion::Backend::Pg->new('postgresql://postgres@/test');
440         my $backend = Minion::Backend::Pg->new(Mojo::Pg->new);
441
442       Construct a new Minion::Backend::Pg object.
443
444   note
445         my $bool = $backend->note($job_id, {mojo => 'rocks', minion => 'too'});
446
447       Change one or more metadata fields for a job. Setting a value to
448       "undef" will remove the field.
449
450   receive
451         my $commands = $backend->receive($worker_id);
452
453       Receive remote control commands for worker.
454
455   register_worker
456         my $worker_id = $backend->register_worker;
457         my $worker_id = $backend->register_worker($worker_id);
458         my $worker_id = $backend->register_worker(
459           $worker_id, {status => {queues => ['default', 'important']}});
460
461       Register worker or send heartbeat to show that this worker is still
462       alive.
463
464       These options are currently available:
465
466       status
467           status => {queues => ['default', 'important']}
468
469         Hash reference with whatever status information the worker would like
470         to share.
471
472   remove_job
473         my $bool = $backend->remove_job($job_id);
474
475       Remove "failed", "finished" or "inactive" job from queue.
476
477   repair
478         $backend->repair;
479
480       Repair worker registry and job queue if necessary.
481
482   reset
483         $backend->reset({all => 1});
484
485       Reset job queue.
486
487       These options are currently available:
488
489       all
490           all => 1
491
492         Reset everything.
493
494       locks
495           locks => 1
496
497         Reset only locks.
498
499   retry_job
500         my $bool = $backend->retry_job($job_id, $retries);
501         my $bool = $backend->retry_job($job_id, $retries, {delay => 10});
502
503       Transition job back to "inactive" state, already "inactive" jobs may
504       also be retried to change options.
505
506       These options are currently available:
507
508       attempts
509           attempts => 25
510
511         Number of times performing this job will be attempted.
512
513       delay
514           delay => 10
515
516         Delay job for this many seconds (from now), defaults to 0.
517
518       expire
519           expire => 300
520
521         Job is valid for this many seconds (from now) before it expires. Note
522         that this option is EXPERIMENTAL and might change without warning!
523
524       lax
525           lax => 1
526
527         Existing jobs this job depends on may also have transitioned to the
528         "failed" state to allow for it to be processed, defaults to "false".
529         Note that this option is EXPERIMENTAL and might change without
530         warning!
531
532       parents
533           parents => [$id1, $id2, $id3]
534
535         Jobs this job depends on.
536
537       priority
538           priority => 5
539
540         Job priority.
541
542       queue
543           queue => 'important'
544
545         Queue to put job in.
546
547   stats
548         my $stats = $backend->stats;
549
550       Get statistics for the job queue.
551
552       These fields are currently available:
553
554       active_jobs
555           active_jobs => 100
556
557         Number of jobs in "active" state.
558
559       active_locks
560           active_locks => 100
561
562         Number of active named locks.
563
564       active_workers
565           active_workers => 100
566
567         Number of workers that are currently processing a job.
568
569       delayed_jobs
570           delayed_jobs => 100
571
572         Number of jobs in "inactive" state that are scheduled to run at
573         specific time in the future.
574
575       enqueued_jobs
576           enqueued_jobs => 100000
577
578         Rough estimate of how many jobs have ever been enqueued.
579
580       failed_jobs
581           failed_jobs => 100
582
583         Number of jobs in "failed" state.
584
585       finished_jobs
586           finished_jobs => 100
587
588         Number of jobs in "finished" state.
589
590       inactive_jobs
591           inactive_jobs => 100
592
593         Number of jobs in "inactive" state.
594
595       inactive_workers
596           inactive_workers => 100
597
598         Number of workers that are currently not processing a job.
599
600       uptime
601           uptime => 1000
602
603         Uptime in seconds.
604
605   unlock
606         my $bool = $backend->unlock('foo');
607
608       Release a named lock.
609
610   unregister_worker
611         $backend->unregister_worker($worker_id);
612
613       Unregister worker.
614

SEE ALSO

616       Minion, <https://minion.pm>, Mojolicious::Guides,
617       <https://mojolicious.org>.
618
619
620
621perl v5.32.0                      2020-08-02            Minion::Backend::Pg(3)
Impressum