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

SEE ALSO

580       Minion, Mojolicious::Guides, <https://mojolicious.org>.
581
582
583
584perl v5.30.1                      2020-02-02            Minion::Backend::Pg(3)
Impressum