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