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