File Coverage

blib/lib/Minion/Backend.pm
Criterion Covered Total %
statement 26 30 86.6
branch 0 4 0.0
condition n/a
subroutine 22 23 95.6
pod 21 21 100.0
total 69 78 88.4


line stmt bran cond sub pod time code
1             package Minion::Backend;
2 1     1   146574 use Mojo::Base -base;
  1         2  
  1         9  
3              
4 1     1   353 use Carp qw(croak);
  1         3  
  1         1386  
5              
6             has minion => undef, weak => 1;
7              
8             sub auto_retry_job {
9 0     0 1 0 my ($self, $id, $retries, $attempts) = @_;
10 0 0       0 return 1 if $attempts <= 1;
11 0         0 my $delay = $self->minion->backoff->($retries);
12 0 0       0 return $self->retry_job($id, $retries, {attempts => $attempts > 1 ? $attempts - 1 : 1, delay => $delay});
13             }
14              
15 1     1 1 232250 sub broadcast { croak 'Method "broadcast" not implemented by subclass' }
16 1     1 1 780 sub dequeue { croak 'Method "dequeue" not implemented by subclass' }
17 1     1 1 700 sub enqueue { croak 'Method "enqueue" not implemented by subclass' }
18 1     1 1 629 sub fail_job { croak 'Method "fail_job" not implemented by subclass' }
19 1     1 1 868 sub finish_job { croak 'Method "finish_job" not implemented by subclass' }
20 1     1 1 1095 sub history { croak 'Method "history" not implemented by subclass' }
21 1     1 1 740 sub list_jobs { croak 'Method "list_jobs" not implemented by subclass' }
22 1     1 1 844 sub list_locks { croak 'Method "list_locks" not implemented by subclass' }
23 1     1 1 712 sub list_workers { croak 'Method "list_workers" not implemented by subclass' }
24 1     1 1 691 sub lock { croak 'Method "lock" not implemented by subclass' }
25 1     1 1 1009 sub note { croak 'Method "note" not implemented by subclass' }
26 1     1 1 676 sub receive { croak 'Method "receive" not implemented by subclass' }
27 1     1 1 720 sub register_worker { croak 'Method "register_worker" not implemented by subclass' }
28 1     1 1 853 sub remove_job { croak 'Method "remove_job" not implemented by subclass' }
29 1     1 1 1141 sub repair { croak 'Method "repair" not implemented by subclass' }
30 1     1 1 843 sub reset { croak 'Method "reset" not implemented by subclass' }
31 1     1 1 731 sub retry_job { croak 'Method "retry_job" not implemented by subclass' }
32 1     1 1 966 sub stats { croak 'Method "stats" not implemented by subclass' }
33 1     1 1 691 sub unlock { croak 'Method "unlock" not implemented by subclass' }
34 1     1 1 657 sub unregister_worker { croak 'Method "unregister_worker" not implemented by subclass' }
35              
36             1;
37              
38             =encoding utf8
39              
40             =head1 NAME
41              
42             Minion::Backend - Backend base class
43              
44             =head1 SYNOPSIS
45              
46             package Minion::Backend::MyBackend;
47             use Mojo::Base 'Minion::Backend';
48              
49             sub broadcast {...}
50             sub dequeue {...}
51             sub enqueue {...}
52             sub fail_job {...}
53             sub finish_job {...}
54             sub history {...}
55             sub list_jobs {...}
56             sub list_locks {...}
57             sub list_workers {...}
58             sub lock {...}
59             sub note {...}
60             sub receive {...}
61             sub register_worker {...}
62             sub remove_job {...}
63             sub repair {...}
64             sub reset {...}
65             sub retry_job {...}
66             sub stats {...}
67             sub unlock {...}
68             sub unregister_worker {...}
69              
70             =head1 DESCRIPTION
71              
72             L is an abstract base class for L backends, like L.
73              
74             =head1 ATTRIBUTES
75              
76             L implements the following attributes.
77              
78             =head2 minion
79              
80             my $minion = $backend->minion;
81             $backend = $backend->minion(Minion->new);
82              
83             L object this backend belongs to. Note that this attribute is weakened.
84              
85             =head1 METHODS
86              
87             L inherits all methods from L and implements the following new ones.
88              
89             =head2 auto_retry_job
90              
91             my $bool = $backend->auto_retry_job($job_id, $retries, $attempts);
92              
93             Automatically L job with L if there are attempts left, used to implement backends like
94             L.
95              
96             =head2 broadcast
97              
98             my $bool = $backend->broadcast('some_command');
99             my $bool = $backend->broadcast('some_command', [@args]);
100             my $bool = $backend->broadcast('some_command', [@args], [$id1, $id2, $id3]);
101              
102             Broadcast remote control command to one or more workers. Meant to be overloaded in a subclass.
103              
104             =head2 dequeue
105              
106             my $job_info = $backend->dequeue($worker_id, 0.5);
107             my $job_info = $backend->dequeue($worker_id, 0.5, {queues => ['important']});
108              
109             Wait a given amount of time in seconds for a job, dequeue it and transition from C to C state, or
110             return C if queues were empty. Meant to be overloaded in a subclass.
111              
112             These options are currently available:
113              
114             =over 2
115              
116             =item id
117              
118             id => '10023'
119              
120             Dequeue a specific job.
121              
122             =item min_priority
123              
124             min_priority => 3
125              
126             Do not dequeue jobs with a lower priority.
127              
128             =item queues
129              
130             queues => ['important']
131              
132             One or more queues to dequeue jobs from, defaults to C.
133              
134             =item tasks
135              
136             tasks => ['foo', 'bar']
137              
138             One or more tasks to dequeue jobs for, defaults to all.
139              
140             =back
141              
142             These fields are currently available:
143              
144             =over 2
145              
146             =item args
147              
148             args => ['foo', 'bar']
149              
150             Job arguments.
151              
152             =item id
153              
154             id => '10023'
155              
156             Job ID.
157              
158             =item retries
159              
160             retries => 3
161              
162             Number of times job has been retried.
163              
164             =item task
165              
166             task => 'foo'
167              
168             Task name.
169              
170             =back
171              
172             =head2 enqueue
173              
174             my $job_id = $backend->enqueue('foo');
175             my $job_id = $backend->enqueue(foo => [@args]);
176             my $job_id = $backend->enqueue(foo => [@args] => {priority => 1});
177              
178             Enqueue a new job with C state. Meant to be overloaded in a subclass.
179              
180             These options are currently available:
181              
182             =over 2
183              
184             =item attempts
185              
186             attempts => 25
187              
188             Number of times performing this job will be attempted, with a delay based on L after the first
189             attempt, defaults to C<1>.
190              
191             =item delay
192              
193             delay => 10
194              
195             Delay job for this many seconds (from now), defaults to C<0>.
196              
197             =item expire
198              
199             expire => 300
200              
201             Job is valid for this many seconds (from now) before it expires.
202              
203             =item lax
204              
205             lax => 1
206              
207             Existing jobs this job depends on may also have transitioned to the C state to allow for it to be processed,
208             defaults to C.
209              
210             =item notes
211              
212             notes => {foo => 'bar', baz => [1, 2, 3]}
213              
214             Hash reference with arbitrary metadata for this job.
215              
216             =item parents
217              
218             parents => [$id1, $id2, $id3]
219              
220             One or more existing jobs this job depends on, and that need to have transitioned to the state C before it
221             can be processed.
222              
223             =item priority
224              
225             priority => 5
226              
227             Job priority, defaults to C<0>. Jobs with a higher priority get performed first. Priorities can be positive or negative,
228             but should be in the range between C<100> and C<-100>.
229              
230             =item queue
231              
232             queue => 'important'
233              
234             Queue to put job in, defaults to C.
235              
236             =back
237              
238             =head2 fail_job
239              
240             my $bool = $backend->fail_job($job_id, $retries);
241             my $bool = $backend->fail_job($job_id, $retries, 'Something went wrong!');
242             my $bool = $backend->fail_job(
243             $job_id, $retries, {whatever => 'Something went wrong!'});
244              
245             Transition from C to C state with or without a result, and if there are attempts remaining, transition
246             back to C with a delay based on L. Meant to be overloaded in a subclass.
247              
248             =head2 finish_job
249              
250             my $bool = $backend->finish_job($job_id, $retries);
251             my $bool = $backend->finish_job($job_id, $retries, 'All went well!');
252             my $bool = $backend->finish_job(
253             $job_id, $retries, {whatever => 'All went well!'});
254              
255             Transition from C to C state with or without a result. Meant to be overloaded in a subclass.
256              
257             =head2 history
258              
259             my $history = $backend->history;
260              
261             Get history information for job queue. Meant to be overloaded in a subclass.
262              
263             These fields are currently available:
264              
265             =over 2
266              
267             =item daily
268              
269             daily => [{epoch => 12345, finished_jobs => 95, failed_jobs => 2}, ...]
270              
271             Hourly counts for processed jobs from the past day.
272              
273             =back
274              
275             =head2 list_jobs
276              
277             my $results = $backend->list_jobs($offset, $limit);
278             my $results = $backend->list_jobs($offset, $limit, {states => ['inactive']});
279              
280             Returns the information about jobs in batches. Meant to be overloaded in a subclass.
281              
282             # Get the total number of results (without limit)
283             my $num = $backend->list_jobs(0, 100, {queues => ['important']})->{total};
284              
285             # Check job state
286             my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
287             my $state = $results->{jobs}[0]{state};
288              
289             # Get job result
290             my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
291             my $result = $results->{jobs}[0]{result};
292              
293             These options are currently available:
294              
295             =over 2
296              
297             =item before
298              
299             before => 23
300              
301             List only jobs before this id.
302              
303             =item ids
304              
305             ids => ['23', '24']
306              
307             List only jobs with these ids.
308              
309             =item notes
310              
311             notes => ['foo', 'bar']
312              
313             List only jobs with one of these notes.
314              
315             =item queues
316              
317             queues => ['important', 'unimportant']
318              
319             List only jobs in these queues.
320              
321             =item states
322              
323             states => ['inactive', 'active']
324              
325             List only jobs in these states.
326              
327             =item tasks
328              
329             tasks => ['foo', 'bar']
330              
331             List only jobs for these tasks.
332              
333             =back
334              
335             These fields are currently available:
336              
337             =over 2
338              
339             =item args
340              
341             args => ['foo', 'bar']
342              
343             Job arguments.
344              
345             =item attempts
346              
347             attempts => 25
348              
349             Number of times performing this job will be attempted.
350              
351             =item children
352              
353             children => ['10026', '10027', '10028']
354              
355             Jobs depending on this job.
356              
357             =item created
358              
359             created => 784111777
360              
361             Epoch time job was created.
362              
363             =item delayed
364              
365             delayed => 784111777
366              
367             Epoch time job was delayed to.
368              
369             =item expires
370              
371             expires => 784111777
372              
373             Epoch time job is valid until before it expires.
374              
375             =item finished
376              
377             finished => 784111777
378              
379             Epoch time job was finished.
380              
381             =item id
382              
383             id => 10025
384              
385             Job id.
386              
387             =item lax
388              
389             lax => 0
390              
391             Existing jobs this job depends on may also have failed to allow for it to be processed.
392              
393             =item notes
394              
395             notes => {foo => 'bar', baz => [1, 2, 3]}
396              
397             Hash reference with arbitrary metadata for this job.
398              
399             =item parents
400              
401             parents => ['10023', '10024', '10025']
402              
403             Jobs this job depends on.
404              
405             =item priority
406              
407             priority => 3
408              
409             Job priority.
410              
411             =item queue
412              
413             queue => 'important'
414              
415             Queue name.
416              
417             =item result
418              
419             result => 'All went well!'
420              
421             Job result.
422              
423             =item retried
424              
425             retried => 784111777
426              
427             Epoch time job has been retried.
428              
429             =item retries
430              
431             retries => 3
432              
433             Number of times job has been retried.
434              
435             =item started
436              
437             started => 784111777
438              
439             Epoch time job was started.
440              
441             =item state
442              
443             state => 'inactive'
444              
445             Current job state, usually C, C, C or C.
446              
447             =item task
448              
449             task => 'foo'
450              
451             Task name.
452              
453             =item time
454              
455             time => 78411177
456              
457             Server time.
458              
459             =item worker
460              
461             worker => '154'
462              
463             Id of worker that is processing the job.
464              
465             =back
466              
467             =head2 list_locks
468              
469             my $results = $backend->list_locks($offset, $limit);
470             my $results = $backend->list_locks($offset, $limit, {names => ['foo']});
471              
472             Returns information about locks in batches. Meant to be overloaded in a subclass.
473              
474             # Get the total number of results (without limit)
475             my $num = $backend->list_locks(0, 100, {names => ['bar']})->{total};
476              
477             # Check expiration time
478             my $results = $backend->list_locks(0, 1, {names => ['foo']});
479             my $expires = $results->{locks}[0]{expires};
480              
481             These options are currently available:
482              
483             =over 2
484              
485             =item names
486              
487             names => ['foo', 'bar']
488              
489             List only locks with these names.
490              
491             =back
492              
493             These fields are currently available:
494              
495             =over 2
496              
497             =item expires
498              
499             expires => 784111777
500              
501             Epoch time this lock will expire.
502              
503             =item id
504              
505             id => 1
506              
507             Lock id.
508              
509             =item name
510              
511             name => 'foo'
512              
513             Lock name.
514              
515             =back
516              
517             =head2 list_workers
518              
519             my $results = $backend->list_workers($offset, $limit);
520             my $results = $backend->list_workers($offset, $limit, {ids => [23]});
521              
522             Returns information about workers in batches. Meant to be overloaded in a subclass.
523              
524             # Get the total number of results (without limit)
525             my $num = $backend->list_workers(0, 100)->{total};
526              
527             # Check worker host
528             my $results = $backend->list_workers(0, 1, {ids => [$worker_id]});
529             my $host = $results->{workers}[0]{host};
530              
531             These options are currently available:
532              
533             =over 2
534              
535             =item before
536              
537             before => 23
538              
539             List only workers before this id.
540              
541             =item ids
542              
543             ids => ['23', '24']
544              
545             List only workers with these ids.
546              
547             =back
548              
549             These fields are currently available:
550              
551             =over 2
552              
553             =item id
554              
555             id => 22
556              
557             Worker id.
558              
559             =item host
560              
561             host => 'localhost'
562              
563             Worker host.
564              
565             =item jobs
566              
567             jobs => ['10023', '10024', '10025', '10029']
568              
569             Ids of jobs the worker is currently processing.
570              
571             =item notified
572              
573             notified => 784111777
574              
575             Epoch time worker sent the last heartbeat.
576              
577             =item pid
578              
579             pid => 12345
580              
581             Process id of worker.
582              
583             =item started
584              
585             started => 784111777
586              
587             Epoch time worker was started.
588              
589             =item status
590              
591             status => {queues => ['default', 'important']}
592              
593             Hash reference with whatever status information the worker would like to share.
594              
595             =back
596              
597             =head2 lock
598              
599             my $bool = $backend->lock('foo', 3600);
600             my $bool = $backend->lock('foo', 3600, {limit => 20});
601              
602             Try to acquire a named lock that will expire automatically after the given amount of time in seconds. An expiration
603             time of C<0> can be used to check if a named lock already exists without creating one. Meant to be overloaded in a
604             subclass.
605              
606             These options are currently available:
607              
608             =over 2
609              
610             =item limit
611              
612             limit => 20
613              
614             Number of shared locks with the same name that can be active at the same time, defaults to C<1>.
615              
616             =back
617              
618             =head2 note
619              
620             my $bool = $backend->note($job_id, {mojo => 'rocks', minion => 'too'});
621              
622             Change one or more metadata fields for a job. Setting a value to C will remove the field. Meant to be overloaded
623             in a subclass.
624              
625             =head2 receive
626              
627             my $commands = $backend->receive($worker_id);
628              
629             Receive remote control commands for worker. Meant to be overloaded in a subclass.
630              
631             =head2 register_worker
632              
633             my $worker_id = $backend->register_worker;
634             my $worker_id = $backend->register_worker($worker_id);
635             my $worker_id = $backend->register_worker(
636             $worker_id, {status => {queues => ['default', 'important']}});
637              
638             Register worker or send heartbeat to show that this worker is still alive. Meant to be overloaded in a subclass.
639              
640             These options are currently available:
641              
642             =over 2
643              
644             =item status
645              
646             status => {queues => ['default', 'important']}
647              
648             Hash reference with whatever status information the worker would like to share.
649              
650             =back
651              
652             =head2 remove_job
653              
654             my $bool = $backend->remove_job($job_id);
655              
656             Remove C, C or C job from queue. Meant to be overloaded in a subclass.
657              
658             =head2 repair
659              
660             $backend->repair;
661              
662             Repair worker registry and job queue if necessary. Meant to be overloaded in a subclass.
663              
664             =head2 reset
665              
666             $backend->reset({all => 1});
667              
668             Reset job queue. Meant to be overloaded in a subclass.
669              
670             These options are currently available:
671              
672             =over 2
673              
674             =item all
675              
676             all => 1
677              
678             Reset everything.
679              
680             =item locks
681              
682             locks => 1
683              
684             Reset only locks.
685              
686             =back
687              
688             =head2 retry_job
689              
690             my $bool = $backend->retry_job($job_id, $retries);
691             my $bool = $backend->retry_job($job_id, $retries, {delay => 10});
692              
693             Transition job back to C state, already C jobs may also be retried to change options. Meant to be
694             overloaded in a subclass.
695              
696             These options are currently available:
697              
698             =over 2
699              
700             =item attempts
701              
702             attempts => 25
703              
704             Number of times performing this job will be attempted.
705              
706             =item delay
707              
708             delay => 10
709              
710             Delay job for this many seconds (from now), defaults to C<0>.
711              
712             =item expire
713              
714             expire => 300
715              
716             Job is valid for this many seconds (from now) before it expires.
717              
718             =item lax
719              
720             lax => 1
721              
722             Existing jobs this job depends on may also have transitioned to the C state to allow for it to be processed,
723             defaults to C.
724              
725             =item parents
726              
727             parents => [$id1, $id2, $id3]
728              
729             Jobs this job depends on.
730              
731             =item priority
732              
733             priority => 5
734              
735             Job priority.
736              
737             =item queue
738              
739             queue => 'important'
740              
741             Queue to put job in.
742              
743             =back
744              
745             =head2 stats
746              
747             my $stats = $backend->stats;
748              
749             Get statistics for the job queue. Meant to be overloaded in a subclass.
750              
751             These fields are currently available:
752              
753             =over 2
754              
755             =item active_jobs
756              
757             active_jobs => 100
758              
759             Number of jobs in C state.
760              
761             =item active_locks
762              
763             active_locks => 100
764              
765             Number of active named locks.
766              
767             =item active_workers
768              
769             active_workers => 100
770              
771             Number of workers that are currently processing a job.
772              
773             =item delayed_jobs
774              
775             delayed_jobs => 100
776              
777             Number of jobs in C state that are scheduled to run at specific time in the future or have unresolved
778             dependencies.
779              
780             =item enqueued_jobs
781              
782             enqueued_jobs => 100000
783              
784             Rough estimate of how many jobs have ever been enqueued.
785              
786             =item failed_jobs
787              
788             failed_jobs => 100
789              
790             Number of jobs in C state.
791              
792             =item finished_jobs
793              
794             finished_jobs => 100
795              
796             Number of jobs in C state.
797              
798             =item inactive_jobs
799              
800             inactive_jobs => 100
801              
802             Number of jobs in C state.
803              
804             =item inactive_workers
805              
806             inactive_workers => 100
807              
808             Number of workers that are currently not processing a job.
809              
810             =item uptime
811              
812             uptime => 1000
813              
814             Uptime in seconds.
815              
816             =item workers
817              
818             workers => 200;
819              
820             Number of registered workers.
821              
822             =back
823              
824             =head2 unlock
825              
826             my $bool = $backend->unlock('foo');
827              
828             Release a named lock. Meant to be overloaded in a subclass.
829              
830             =head2 unregister_worker
831              
832             $backend->unregister_worker($worker_id);
833              
834             Unregister worker. Meant to be overloaded in a subclass.
835              
836             =head1 SEE ALSO
837              
838             L, L, L, L, L.
839              
840             =cut