File Coverage

blib/lib/Minion/Iterator.pm
Criterion Covered Total %
statement 3 23 13.0
branch 0 6 0.0
condition 0 10 0.0
subroutine 1 5 20.0
pod 3 3 100.0
total 7 47 14.8


line stmt bran cond sub pod time code
1             package Minion::Iterator;
2 2     2   12 use Mojo::Base -base;
  2         3  
  2         11  
3              
4             has fetch => 10;
5             has [qw(minion options)];
6              
7             sub each {
8 0     0 1   my ($self, $cb) = @_;
9 0           while ($_ = $self->next) { $cb->($_) }
  0            
10             }
11              
12 0     0 1   sub next { shift @{shift->_fetch(0)->{results}} }
  0            
13              
14 0     0 1   sub total { shift->_fetch(1)->{total} }
15              
16             sub _fetch {
17 0     0     my ($self, $lazy) = @_;
18              
19 0 0 0       return $self if ($lazy && exists $self->{total}) || @{$self->{results} // []};
  0   0        
      0        
20              
21 0 0         my $what = $self->{jobs} ? 'jobs' : 'workers';
22 0           my $method = "list_$what";
23 0           my $options = $self->options;
24 0           my $results = $self->minion->backend->$method(0, $self->fetch, $options);
25              
26 0   0       $self->{total} = $results->{total} + ($self->{count} // 0);
27 0           $self->{count} += my @results = @{$results->{$what}};
  0            
28 0           push @{$self->{results}}, @results;
  0            
29 0 0         $options->{before} = $results[-1]{id} if @results;
30              
31 0           return $self;
32             }
33              
34             1;
35              
36             =encoding utf8
37              
38             =head1 NAME
39              
40             Minion::Iterator - Minion iterator
41              
42             =head1 SYNOPSIS
43              
44             use Minion::Iterator;
45              
46             my $iter = Minion::Iterator->new(minion => $minion, options => {states => ['inactive']});
47              
48             =head1 DESCRIPTION
49              
50             L is an iterator for L listing methods.
51              
52             =head1 ATTRIBUTES
53              
54             L implements the following attributes.
55              
56             =head2 fetch
57              
58             my $fetch = $iter->fetch;
59             $iter = $iter->fetch(2);
60              
61             Number of results to cache, defaults to C<10>.
62              
63             =head2 minion
64              
65             my $minion = $iter->minion;
66             $iter = $iter->minion(Minion->new);
67              
68             L object this job belongs to.
69              
70             =head2 options
71              
72             my $options = $iter->options;
73             $iter = $iter->options({states => ['inactive']});
74              
75             Options to be passed to L or L.
76              
77             =head1 METHODS
78              
79             L inherits all methods from L and implements the following new ones.
80              
81             =head2 each
82              
83             $iter->each(sub {...});
84              
85             Evaluate callback for each element in collection. The element will be the first argument passed to the callback, and is
86             also available as C<$_>.
87              
88             =head2 next
89              
90             my $value = $iter->next;
91              
92             Get next value.
93              
94             =head2 total
95              
96             my $num = $iter->total;
97              
98             Total number of results. If results are removed in the backend while iterating, this number will become an estimate
99             that gets updated every time new results are fetched.
100              
101             =head1 SEE ALSO
102              
103             L, L, L, L, L.
104              
105             =cut