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

NAME

6       Minion::Backend - Backend base class
7

SYNOPSIS

9         package Minion::Backend::MyBackend;
10         use Mojo::Base 'Minion::Backend';
11
12         sub broadcast         {...}
13         sub dequeue           {...}
14         sub enqueue           {...}
15         sub fail_job          {...}
16         sub finish_job        {...}
17         sub history           {...}
18         sub list_jobs         {...}
19         sub list_locks        {...}
20         sub list_workers      {...}
21         sub lock              {...}
22         sub note              {...}
23         sub receive           {...}
24         sub register_worker   {...}
25         sub remove_job        {...}
26         sub repair            {...}
27         sub reset             {...}
28         sub retry_job         {...}
29         sub stats             {...}
30         sub unlock            {...}
31         sub unregister_worker {...}
32

DESCRIPTION

34       Minion::Backend is an abstract base class for Minion backends, like
35       Minion::Backend::Pg.
36

ATTRIBUTES

38       Minion::Backend implements the following attributes.
39
40   minion
41         my $minion = $backend->minion;
42         $backend   = $backend->minion(Minion->new);
43
44       Minion object this backend belongs to. Note that this attribute is
45       weakened.
46

METHODS

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

SEE ALSO

646       Minion, <https://minion.pm>, Mojolicious::Guides,
647       <https://mojolicious.org>.
648
649
650
651perl v5.32.0                      2020-08-02                Minion::Backend(3)
Impressum