Queue.pm

filter: Statementstmt 12.6% 35 / 277 Branchbran 0.0% 0 / 156 Conditioncond 0.0% 0 / 67 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 13.4% 7 / 52 Podpod 87.5% 14 / 16 totaltotal 9.2% 56 / 604 CC 103 SCAR 40.5
CC
103
Cov
7%
__ANON__
240.0
enqueue
132.0
_consume_blocking
110.0
CRAP 57.5
↑ Summary Queue/Delay.pm »
1 package MangoX::Queue;
2  
3 12 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Mango::BSON ':bson';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use MangoX::Queue::Delay;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use MangoX::Queue::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 our $VERSION = '0.15';
12  
13 # A logger
14 0 ▶has 'log' => sub { Mojo::Log->new->level('error') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15  
16 # The Mango::Collection representing the queue
17 12 has 'collection';
18 0 ▶has 'capped' => sub { $_[0]->stats->{capped} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 0 ▶has 'stats' => sub { $_[0]->collection->stats };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 # A MangoX::Queue::Delay
22 0 ▶has 'delay' => sub { MangoX::Queue::Delay->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
23  
24 # How long to wait before assuming a job has failed
25 0 ▶has 'timeout' => sub { $ENV{MANGOX_QUEUE_JOB_TIMEOUT} // 60 };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{"MANGOX_QUEUE_JOB_TIMEOUT"} // 60
l!l
00
MC/DC0%0/1
$ENV{"MANGOX_QUEUE_JOB_TIMEOUT"} // 60
$ENV{"MANGOX_QUEUE_JOB_TIMEOUT"}
Truth table0%0/2
A
A= $ENV{"MANGOX_QUEUE_JOB_TIMEOUT"}
Aresult
11
00
26  
27 # How many times to retry a job before giving up
28 0 ▶has 'retries' => sub { $ENV{MANGOX_QUEUE_JOB_RETRIES} // 5 };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{"MANGOX_QUEUE_JOB_RETRIES"} // 5
l!l
00
MC/DC0%0/1
$ENV{"MANGOX_QUEUE_JOB_RETRIES"} // 5
$ENV{"MANGOX_QUEUE_JOB_RETRIES"}
Truth table0%0/2
A
A= $ENV{"MANGOX_QUEUE_JOB_RETRIES"}
Aresult
11
00
29  
30 # Prevent binary object IDs escaping MangoX::Queue
31 0 ▶has 'no_binary_oid' => sub { $ENV{MANGOX_QUEUE_NO_BINARY_OID} // 0 };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{"MANGOX_QUEUE_NO_BINARY_OID"} // 0
l!l
00
MC/DC0%0/1
$ENV{"MANGOX_QUEUE_NO_BINARY_OID"} // 0
$ENV{"MANGOX_QUEUE_NO_BINARY_OID"}
Truth table0%0/2
A
A= $ENV{"MANGOX_QUEUE_NO_BINARY_OID"}
Aresult
11
00
32  
33 # Current number of jobs that have been consumed but not yet completed
34 12 has 'job_count' => 0;
35  
36 # Maximum number of jobs allowed to be in a consumed state at any one time
37 12 has 'concurrent_job_limit' => 10;
38  
39 # Store Mojo::IOLoop->timer IDs
40 0 ▶has 'consumers' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
41  
42 # Store plugins
43 0 ▶has 'plugins' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44  
45 sub new {
46 0 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
47  
48 0 ▶ croak qq{No Mango::Collection provided to constructor} unless ref($self->collection) eq 'Mango::Collection';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->collection eq "Mango::Collection" 0 0 0
49  
50 0 return $self;
51 }
52  
53 sub plugin {
54 0 ▶ my ($self, $name, $options) = @_;
Subroutine plugin: not called CC=6 SCAR=34.2
Pod undocumented
55  
56 0 ▶ croak qq{Plugin $name already loaded} if exists $self->plugins->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->plugins->{$name} 0 0 0
57  
58 {
59 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
60 0 ▶ unless($name->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name->can("new")) 0 0 0
61 0 ▶ eval "require $name" or croak qq{Failed to load plugin $name: $@};
Branch: 1 branch
BranchTrueFalseTotal
eval "require $name" or croak("Failed to load plugin ${name}: $@") 0 0 0
62 }
63 }
64  
65 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->plugins->{$name} = $name->new(%$options); return 1 } } or croak("Error calling constructor for plugin ${name}: $@") 0 0 0
66 0 $self->plugins->{$name} = $name->new(%$options);
67 0 return 1;
68 } or croak qq{Error calling constructor for plugin $name: $@};
69  
70 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->plugins->{$name}->register($self); return 1 } } or croak("Error calling register for plugin ${name}: $@") 0 0 0
71 0 $self->plugins->{$name}->register($self);
72 0 return 1;
73 } or croak qq{Error calling register for plugin $name: $@};
74  
75 0 return $self->plugins->{$name};
76 }
77  
78 sub init_status {
79 0 ▶ my ($self) = @_;
Subroutine init_status: not called CC=4 SCAR=30.0
Pod undocumented
80 # Done as late as possible - $self->capped opens a DB connection
81 0 ▶ $self->pending_status($self->capped ? 1 : 'Pending');
Branch: 1 branch
BranchTrueFalseTotal
$self->capped ? : 0 0 0
82 0 ▶ $self->processing_status($self->capped ? 2 : 'Processing');
Branch: 1 branch
BranchTrueFalseTotal
$self->capped ? : 0 0 0
83 0 ▶ $self->failed_status($self->capped ? 3 : 'Failed');
Branch: 1 branch
BranchTrueFalseTotal
$self->capped ? : 0 0 0
84 return {
85 pending_and_processing_status => $self->{pending_and_processing_status},
86 _pending_status => $self->{_pending_status},
87 _processing_status => $self->{_processing_status},
88 _failed_status => $self->{_failed_status},
89 0 };
90 }
91  
92 sub pending_status {
93 0 ▶ my ($self, $new_status) = @_;
Subroutine pending_status: not called CC=2 SCAR=17.9
Pod documented
94  
95 0 ▶ return $self->{_pending_status} unless defined $new_status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_status 0 0 0
96  
97 0 $self->{_pending_status} = $new_status;
98 0 $self->_combine_pending_and_processing_status();
99 }
100  
101 sub processing_status {
102 0 ▶ my ($self, $new_status) = @_;
Subroutine processing_status: not called CC=2 SCAR=17.9
Pod documented
103  
104 0 ▶ return $self->{_processing_status} unless defined $new_status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_status 0 0 0
105  
106 0 $self->{_processing_status} = $new_status;
107 0 $self->_combine_pending_and_processing_status();
108 }
109  
110 sub failed_status {
111 0 ▶ my ($self, $new_status) = @_;
Subroutine failed_status: not called CC=2 SCAR=17.9
Pod documented
112  
113 0 ▶ return $self->{_failed_status} unless defined $new_status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_status 0 0 0
114 0 $self->{_failed_status} = $new_status;
115 }
116  
117 sub _combine_pending_and_processing_status {
118 0 ▶ my ($self) = @_;
Subroutine _combine_pending_and_processing_status: not called CC=2 SCAR=17.9
119  
120 $self->{pending_and_processing_status} = [
121 0 ref($self->{_pending_status}) eq 'ARRAY' ? @{$self->{_pending_status}} : $self->{_pending_status},
122 $self->{_processing_status},
123 0 ▶ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'_pending_status'} eq 'ARRAY' ? : 0 0 0
124 }
125  
126 sub get_options {
127 0 ▶ my ($self) = @_;
Subroutine get_options: not called CC=3 SCAR=24.8
Pod documented
128  
129 return {
130 query => {
131 status => {
132 '$in' => $self->{pending_and_processing_status} // $self->init_status->{pending_and_processing_status},
133 },
134 processing => {
135 '$lt' => time - $self->timeout,
136 },
137 attempt => {
138 '$lte' => $self->retries + 1,
139 },
140 delay_until => {
141 '$lt' => time,
142 }
143 },
144 sort => bson_doc( # Sort by priority, then in order of creation
145 'priority' => 1,
146 'created' => -1,
147 ),
148 update => {
149 '$set' => {
150 processing => time,
151 status => $self->{_processing_status} // $self->init_status->{_processing_status},
152 },
153 0 ▶ '$inc' => {
Condition0%0/6
$self->{"pending_and_processing_status"} // $self->init_status->{"pending_and_processing_status"}
l!l&&r!l&&!r
000
$self->{"_processing_status"} // $self->init_status->{"_processing_status"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pending_and_processing_status"} // $self->init_status->{"pending_and_processing_status"}
$self->{"pending_and_processing_status"} $self->init_status->{"pending_and_processing_status"}
MC/DC0%0/2
$self->{"_processing_status"} // $self->init_status->{"_processing_status"}
$self->{"_processing_status"} $self->init_status->{"_processing_status"}
Truth table0%0/3
A // B
A= $self->{"pending_and_processing_status"}B= $self->init_status->{"pending_and_processing_status"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $self->{"_processing_status"}B= $self->init_status->{"_processing_status"}
ABresult
1X1
011
000
154 attempt => 1,
155 }
156 }
157 };
158 }
159  
160 sub enqueue {
161 0 ▶ my ($self, @args) = @_;
Subroutine enqueue: not called CC=11 SCAR=48.8
Pod documented
162  
163 # args maybe
164 # - 'job_name'
165 # - foo => bar, 'job_name'
166 # - 'job_name', $callback
167 # - foo => bar, 'job_name', $callback
168  
169 0 ▶ my $callback = ref($args[-1]) eq 'CODE' ? pop @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'CODE' ? : 0 0 0
170 0 my $job = pop @args;
171 0 my %args;
172 0 ▶ %args = (@args) if scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 0 0
173  
174 my $db_job = {
175 priority => $args{priority} // 1,
176 created => $args{created} // bson_time,
177 data => $job,
178 status => $args{status} // $self->{_pending_status} // $self->init_status->{_pending_status},
179 0 ▶ attempt => 1,
Condition0%0/11
$args{"priority"} // 1
l!l
00
$args{"created"} // bson_time()
l!l&&r!l&&!r
000
($args{"status"} // $self->{"_pending_status"}) // $self->init_status->{"_pending_status"}
l!l&&r!l&&!r
000
$args{"status"} // $self->{"_pending_status"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$args{"priority"} // 1
$args{"priority"}
MC/DC0%0/2
$args{"created"} // bson_time()
$args{"created"} bson_time()
MC/DC0%0/3
($args{"status"} // $self->{"_pending_status"}) // $self->init_status->{"_pending_status"}
$args{"status"} $self->{"_pending_status"} $self->init_status->{"_pending_status"}
Truth table0%0/2
A
A= $args{"priority"}
Aresult
11
00
Truth table0%0/3
A // B
A= $args{"created"}B= bson_time()
ABresult
1X1
011
000
Truth table0%0/4
A // B // C
A= $args{"status"}B= $self->{"_pending_status"}C= $self->init_status->{"_pending_status"}
ABCresult
1XX1
01X1
0011
0000
180 processing => 0,
181 delay_until => 0,
182 };
183  
184 0 ▶ $db_job->{delay_until} = $args{delay_until} if $args{delay_until};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"delay_until"} 0 0 0
185  
186 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
187 return $self->collection->insert($db_job => sub {
188 0 ▶ my ($collection, $error, $oid) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
189 0 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
190 0 $self->emit_safe(error => qq{Error inserting job into collection: $error}, $db_job, $error);
191 0 $callback->($db_job, $error);
192 0 return;
193 }
194 0 $db_job->{_id} = $oid;
195 0 ▶ $self->emit_safe(enqueued => $db_job) if $self->has_subscribers('enqueued');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("enqueued") 0 0 0
196 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$callback($db_job, undef); return 1 } } or $self->emit_safe("error", "Error in callback: $@", $db_job, $@) 0 0 0
197 0 $callback->($db_job, undef);
198 0 return 1;
199 } or $self->emit_safe(error => qq{Error in callback: $@}, $db_job, $@);
200 0 });
201 } else {
202 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $db_job->{"_id"} = $self->collection->insert($db_job); return 1 } } or croak("Error inserting job into collection: $@") 0 0 0
203 0 $db_job->{_id} = $self->collection->insert($db_job);
204 0 return 1;
205 } or croak qq{Error inserting job into collection: $@};
206 0 ▶ $self->emit_safe(enqueued => $db_job) if $self->has_subscribers('enqueued');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("enqueued") 0 0 0
207 0 return $db_job;
208 }
209 }
210  
211 sub watch {
212 0 ▶ my ($self, $id_or_job, $status, $callback) = @_;
Subroutine watch: not called CC=4 SCAR=30.0
Pod documented
213  
214 0 ▶ my $id = ref($id_or_job) ? $id_or_job->{_id} : $id_or_job;
Branch: 1 branch
BranchTrueFalseTotal
ref $id_or_job ? : 0 0 0
215  
216 0 ▶ $status //= 'Complete';
Condition0%0/2
$status //= "Complete"
l!l
00
MC/DC0%0/1
$status //= "Complete"
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
217  
218 # args
219 # - watch $queue $id, 'Status' => $callback
220  
221 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
222 # Non-blocking
223 0 $self->log->debug("Waiting for $id on status $status in non-blocking mode");
224 0 ▶ return Mojo::IOLoop->timer(0 => sub { $self->_watch_nonblocking($id, $status, $callback) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 } else {
226 # Blocking
227 0 $self->log->debug("Waiting for $id on status $status in blocking mode");
228 0 return $self->_watch_blocking($id, $status);
229 }
230 }
231  
232 sub _watch_blocking {
233 0 ▶ my ($self, $id, $status) = @_;
Subroutine _watch_blocking: not called CC=7 SCAR=40.3
234  
235 0 while(1) {
236 0 my $doc = $self->collection->find_one({'_id' => $id});
237 0 ▶ $self->log->debug("Job found by Mango: " . ($doc ? 'Yes' : 'No'));
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 0 0 0
238  
239 0 ▶ if($doc && ((!ref($status) && $doc->{status} eq $status) || (ref($status) eq 'ARRAY' && grep { $_ =~ $doc->{status} } @$status))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc and !ref($status) && $doc->{'status'} eq $status || ref $status eq 'ARRAY' && grep({$_ =~ /$doc->{'status'}/u;} @$status)) { } 0 0 0
Condition0%0/12
$doc and !ref($status) && $doc->{"status"} eq $status || ref $status eq "ARRAY" && grep({$_ =~ /$doc->{"status"}/u;} @$status)
!ll&&!rl&&r
000
not ref $status and $doc->{"status"} eq $status or ref $status eq "ARRAY" and grep {$_ =~ /$doc->{"status"}/u;} @$status
l!l&&r!l&&!r
000
not ref $status and $doc->{"status"} eq $status
!ll&&!rl&&r
000
ref $status eq "ARRAY" and grep {$_ =~ /$doc->{"status"}/u;} @$status
!ll&&!rl&&r
000
MC/DC0%0/5
$doc and !ref($status) && $doc->{"status"} eq $status || ref $status eq "ARRAY" && grep({$_ =~ /$doc->{"status"}/u;} @$status)
$doc not ref $status $doc->{"status"} eq $status ref $status eq "ARRAY" grep {$_ =~ /$doc->{"status"}/u;} @$status
Truth table0%0/8
A and not(B) and C or D and E
A= $docB= not ref $statusC= $doc->{"status"} eq $statusD= ref $status eq "ARRAY"E= grep {$_ =~ /$doc->{"status"}/u;} @$status
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
240 0 return 1;
241 } else {
242 0 $self->delay->wait;
243 }
244 }
245 }
246  
247 sub _watch_nonblocking {
248 0 ▶ my ($self, $id, $status, $callback) = @_;
Subroutine _watch_nonblocking: not called CC=1 SCAR=6.9
249  
250 $self->collection->find_one({'_id' => $id} => sub {
251 0 ▶ my ($cursor, $err, $doc) = @_;
Subroutine __ANON__: not called CC=7 SCAR=40.3
252 0 ▶ $self->log->debug("Job found by Mango: " . ($doc ? 'Yes' : 'No'));
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 0 0 0
253
254 0 ▶ if($doc && ((!ref($status) && $doc->{status} eq $status) || (ref($status) eq 'ARRAY' && grep { $_ =~ $doc->{status} } @$status))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc and !ref($status) && $doc->{'status'} eq $status || ref $status eq 'ARRAY' && grep({$_ =~ /$doc->{'status'}/u;} @$status)) { } 0 0 0
Condition0%0/12
$doc and !ref($status) && $doc->{"status"} eq $status || ref $status eq "ARRAY" && grep({$_ =~ /$doc->{"status"}/u;} @$status)
!ll&&!rl&&r
000
not ref $status and $doc->{"status"} eq $status or ref $status eq "ARRAY" and grep {$_ =~ /$doc->{"status"}/u;} @$status
l!l&&r!l&&!r
000
not ref $status and $doc->{"status"} eq $status
!ll&&!rl&&r
000
ref $status eq "ARRAY" and grep {$_ =~ /$doc->{"status"}/u;} @$status
!ll&&!rl&&r
000
MC/DC0%0/5
$doc and !ref($status) && $doc->{"status"} eq $status || ref $status eq "ARRAY" && grep({$_ =~ /$doc->{"status"}/u;} @$status)
$doc not ref $status $doc->{"status"} eq $status ref $status eq "ARRAY" grep {$_ =~ /$doc->{"status"}/u;} @$status
Truth table0%0/8
A and not(B) and C or D and E
A= $docB= not ref $statusC= $doc->{"status"} eq $statusD= ref $status eq "ARRAY"E= grep {$_ =~ /$doc->{"status"}/u;} @$status
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
255 0 $self->log->debug("Status is $status");
256 0 $self->delay->reset;
257 0 $callback->($doc, undef);
258 } else {
259 0 $self->log->debug("Job not found or status doesn't match");
260 $self->delay->wait(sub {
261 0 ▶ return unless Mojo::IOLoop->is_running;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
262 0 ▶ Mojo::IOLoop->timer(0 => sub { $self->_watch_nonblocking($id, $status, $callback) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 });
264 0 return undef;
265 }
266 0 });
267 }
268  
269 sub requeue {
270 0 ▶ my ($self, $job, $callback) = @_;
Subroutine requeue: not called CC=3 SCAR=24.8
Pod documented
271  
272 0 ▶ my $pending = $self->{_pending_status} // $self->init_status->{_pending_status};
Condition0%0/3
$self->{"_pending_status"} // $self->init_status->{"_pending_status"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_pending_status"} // $self->init_status->{"_pending_status"}
$self->{"_pending_status"} $self->init_status->{"_pending_status"}
Truth table0%0/3
A // B
A= $self->{"_pending_status"}B= $self->init_status->{"_pending_status"}
ABresult
1X1
011
000
273 0 ▶ $job->{status} = ref($pending) eq 'ARRAY' ? $pending->[0] : $pending;
Branch: 1 branch
BranchTrueFalseTotal
ref $pending eq 'ARRAY' ? : 0 0 0
274 0 return $self->update($job, $callback);
275 }
276  
277 sub dequeue {
278 0 ▶ my ($self, $id_or_job, $callback) = @_;
Subroutine dequeue: not called CC=4 SCAR=30.0
Pod documented
279  
280 # TODO option to not remove on dequeue?
281  
282 0 ▶ my $id = ref($id_or_job) ? $id_or_job->{_id} : $id_or_job;
Branch: 1 branch
BranchTrueFalseTotal
ref $id_or_job ? : 0 0 0
283  
284 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
285 $self->collection->remove({'_id' => $id} => sub {
286 0 ▶ my ($collection, $error, $doc) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
287  
288 0 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
289 0 ▶ $self->emit_safe(error => qq(Error removing job from collection: $error), $id_or_job, $error) if $self->has_subscribers('error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("error") 0 0 0
290 0 $callback->($id_or_job, $error);
291 0 return;
292 }
293  
294 0 $callback->($id_or_job, undef);
295 0 ▶ $self->emit_safe(dequeued => $id_or_job) if $self->has_subscribers('dequeued');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("dequeued") 0 0 0
296 0 });
297 } else {
298 0 $self->collection->remove({'_id' => $id});
299 0 ▶ $self->emit_safe(dequeued => $id_or_job) if $self->has_subscribers('dequeued');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("dequeued") 0 0 0
300 }
301 }
302  
303 sub get {
304 0 ▶ my ($self, $id_or_job, $callback) = @_;
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
305  
306 0 ▶ my $id = ref($id_or_job) ? $id_or_job->{_id} : $id_or_job;
Branch: 1 branch
BranchTrueFalseTotal
ref $id_or_job ? : 0 0 0
307  
308 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
309 return $self->collection->find_one({'_id' => $id} => sub {
310 0 ▶ my ($collection, $error, $doc) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
311  
312 0 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
313 0 ▶ $self->emit_safe(error => qq(Error retrieving job: $error), $id_or_job, $error) if $self->has_subscribers('error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("error") 0 0 0
314 }
315  
316 0 $callback->($doc, $error);
317 0 });
318 } else {
319 0 return $self->collection->find_one({'_id' => $id});
320 }
321 }
322  
323 sub update {
324 0 ▶ my ($self, $job, $callback) = @_;
Subroutine update: not called CC=3 SCAR=24.8
Pod documented
325  
326 # FIXME Temporary fix to remove has_finished indicator from MangoX::Queue::Job
327 0 $job = { map { $_ => $job->{$_} } grep { $_ !~ /^(?:has_finished|events)$/ } keys %$job };
328 0 ▶ $job->{_id} = Mango::BSON::ObjectID->new($job->{_id}) if $self->no_binary_oid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_binary_oid 0 0 0
329  
330 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
331 return $self->collection->update({'_id' => $job->{_id}}, $job => sub {
332 0 ▶ my ($collection, $error, $doc) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
333 0 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
334 0 ▶ $self->emit_safe(error => qq(Error updating job: $error), $job, $error) if $self->has_subscribers('error');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("error") 0 0 0
335 }
336 0 $callback->($doc, $error);
337 0 });
338 } else {
339 0 return $self->collection->update({'_id' => $job->{_id}}, $job, {upsert => 1}) or croak qq{Error updating collection: $@};
340 }
341 }
342  
343 sub fetch {
344 0 ▶ my ($self, @args) = @_;
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
345  
346 # fetch $queue status => 'Complete', sub { my $job = shift; }
347  
348 0 ▶ my $callback = ref($args[-1]) eq 'CODE' ? pop @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'CODE' ? : 0 0 0
349 0 my %args;
350 0 ▶ %args = (@args) if scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 0 0
351  
352 0 $self->log->debug("In fetch");
353  
354 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
355 0 $self->log->debug("Fetching in non-blocking mode");
356 0 my $consumer_id = (scalar keys %{$self->consumers}) + 1;
357 0 ▶ $self->consumers->{$consumer_id} = Mojo::IOLoop->timer(0 => sub { $self->_consume_nonblocking(\%args, $consumer_id, $callback, 1) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
358 0 return $consumer_id;
359 } else {
360 0 $self->log->debug("Fetching in blocking mode");
361 0 return $self->_consume_blocking(\%args, 1);
362 }
363 }
364  
365 sub consume {
366 0 ▶ my ($self, @args) = @_;
Subroutine consume: not called CC=4 SCAR=30.0
Pod documented
367  
368 # consume $queue status => 'Failed', sub { my $job = shift; }
369  
370 0 ▶ my $callback = ref($args[-1]) eq 'CODE' ? pop @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'CODE' ? : 0 0 0
371 0 my %args;
372 0 ▶ %args = (@args) if scalar @args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 0 0
373  
374 0 $self->log->debug("In consume");
375  
376 0 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) { } 0 0 0
377 0 $self->log->debug("consuming in non-blocking mode");
378 0 my $consumer_id = (scalar keys %{$self->consumers}) + 1;
379 0 ▶ $self->consumers->{$consumer_id} = Mojo::IOLoop->timer(0 => sub { $self->_consume_nonblocking(\%args, $consumer_id, $callback, 0) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 $self->log->debug("Timer scheduled, consumer_id $consumer_id has timer ID: " . $self->consumers->{$consumer_id});
381 0 return $consumer_id;
382 } else {
383 0 $self->log->debug("consuming in blocking mode");
384 0 return $self->_consume_blocking(\%args, 0);
385 }
386 }
387  
388 sub release {
389 0 ▶ my ($self, $consumer_id) = @_;
Subroutine release: not called CC=1 SCAR=6.9
Pod documented
390  
391 0 $self->log->debug("Releasing consumer $consumer_id with timer ID: " . $self->consumers->{$consumer_id});
392  
393 0 Mojo::IOLoop->remove($self->consumers->{$consumer_id});
394 0 delete $self->consumers->{$consumer_id};
395  
396 0 return 1;
397 }
398  
399 sub _consume_blocking {
400 0 ▶ my ($self, $args, $fetch) = @_;
Subroutine _consume_blocking: not called CC=10 SCAR=47.0
401  
402 0 while(1) {
403 0 my $opts = $self->get_options;
404 0 ▶ $opts->{query} = $args if scalar keys %$args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$args 0 0 0
405  
406 0 my $doc = $self->collection->find_and_modify($opts);
407 0 ▶ $self->log->debug("Job found by Mango: " . ($doc ? 'Yes' : 'No'));
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 0 0 0
408  
409 0 ▶ if($doc && $doc->{attempt} > $self->retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc and $doc->{"attempt"} > $self->retries) 0 0 0
Condition0%0/3
$doc and $doc->{"attempt"} > $self->retries
!ll&&!rl&&r
000
MC/DC0%0/2
$doc and $doc->{"attempt"} > $self->retries
$doc $doc->{"attempt"} > $self->retries
Truth table0%0/3
A and B
A= $docB= $doc->{"attempt"} > $self->retries
ABresult
0X0
100
111
410 0 ▶ $doc->{status} = $self->{_failed_status} // $self->init_status->{_failed_status};
Condition0%0/3
$self->{"_failed_status"} // $self->init_status->{"_failed_status"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_failed_status"} // $self->init_status->{"_failed_status"}
$self->{"_failed_status"} $self->init_status->{"_failed_status"}
Truth table0%0/3
A // B
A= $self->{"_failed_status"}B= $self->init_status->{"_failed_status"}
ABresult
1X1
011
000
411 0 $self->update($doc);
412 0 $doc = undef;
413 0 $self->log->debug("Job exceeded retries, status set to failed and job abandoned");
414 }
415  
416 0 ▶ if($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) { } 0 0 0
417 0 ▶ $doc->{_id} = $doc->{_id}->to_string if $self->no_binary_oid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_binary_oid 0 0 0
418 0 ▶ $self->emit_safe(consumed => $doc) if $self->has_subscribers('consumed');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("consumed") 0 0 0
419 0 return $doc;
420 } else {
421 0 ▶ last if $fetch;
Branch: 1 branch
BranchTrueFalseTotal
if $fetch 0 0 0
422 0 $self->delay->wait;
423 }
424 }
425 }
426  
427 sub _consume_nonblocking {
428 0 ▶ my ($self, $args, $consumer_id, $callback, $fetch) = @_;
Subroutine _consume_nonblocking: not called CC=9 SCAR=45.0
429  
430 0 ▶ $self->log->debug("Active jobs: " . $self->job_count . '/' . ($self->concurrent_job_limit < 0 ? '*' : $self->concurrent_job_limit));
Branch: 1 branch
BranchTrueFalseTotal
$self->concurrent_job_limit < 0 ? : 0 0 0
431  
432 # Don't allow consumption if job_count has been reached
433 0 ▶ if ($self->concurrent_job_limit > -1 && $self->job_count >= $self->concurrent_job_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->concurrent_job_limit > -1 and $self->job_count >= $self->concurrent_job_limit) 0 0 0
Condition0%0/3
$self->concurrent_job_limit > -1 and $self->job_count >= $self->concurrent_job_limit
!ll&&!rl&&r
000
MC/DC0%0/2
$self->concurrent_job_limit > -1 and $self->job_count >= $self->concurrent_job_limit
$self->concurrent_job_limit > -1 $self->job_count >= $self->concurrent_job_limit
Truth table0%0/3
A and B
A= $self->concurrent_job_limit > -1B= $self->job_count >= $self->concurrent_job_limit
ABresult
0X0
100
111
434 0 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
435 0 ▶ return if $fetch;
Branch: 1 branch
BranchTrueFalseTotal
if $fetch 0 0 0
436 0 ▶ $self->emit_safe(concurrent_job_limit_reached => $self->concurrent_job_limit) if $self->has_subscribers('concurrent_job_limit_reached');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("concurrent_job_limit_reached") 0 0 0
437 0 $self->log->debug("concurrent_job_limit_reached = " . $self->concurrent_job_limit . ", job_count = " . $self->job_count);
438 0 ▶ return unless exists $self->consumers->{$consumer_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->consumers->{$consumer_id} 0 0 0
439  
440 $self->delay->wait(sub {
441 0 ▶ return unless exists $self->consumers->{$consumer_id};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->consumers->{$consumer_id} 0 0 0
442 0 $self->_consume_nonblocking($args, $consumer_id, $callback, 0);
443 0 });
444  
445 0 $self->log->debug("Timer rescheduled (job_count limit reached), consumer_id $consumer_id has timer ID: " . $self->consumers->{$consumer_id});
446  
447 0 return;
448 }
449  
450 0 my $opts = $self->get_options;
451 0 ▶ $opts->{query} = $args if scalar keys %$args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$args 0 0 0
452  
453 $self->collection->find_and_modify($opts => sub {
454 0 ▶ my ($cursor, $err, $doc) = @_;
Subroutine __ANON__: not called CC=15 SCAR=54.8
455 0 ▶ $self->log->debug("Job found by Mango: " . ($doc ? 'Yes' : 'No'));
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 0 0 0
456  
457 0 ▶ if($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
458 0 $self->log->error($err);
459 0 $self->emit_safe(error => $err);
460 }
461
462 0 ▶ if($doc && $doc->{attempt} > $self->retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc and $doc->{"attempt"} > $self->retries) 0 0 0
Condition0%0/3
$doc and $doc->{"attempt"} > $self->retries
!ll&&!rl&&r
000
MC/DC0%0/2
$doc and $doc->{"attempt"} > $self->retries
$doc $doc->{"attempt"} > $self->retries
Truth table0%0/3
A and B
A= $docB= $doc->{"attempt"} > $self->retries
ABresult
0X0
100
111
463 0 ▶ $doc->{status} = $self->{_failed_status} // $self->init_status->{_failed_status};
Condition0%0/3
$self->{"_failed_status"} // $self->init_status->{"_failed_status"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_failed_status"} // $self->init_status->{"_failed_status"}
$self->{"_failed_status"} $self->init_status->{"_failed_status"}
Truth table0%0/3
A // B
A= $self->{"_failed_status"}B= $self->init_status->{"_failed_status"}
ABresult
1X1
011
000
464 0 $self->update($doc);
465 0 $doc = undef;
466 0 $self->log->debug("Job exceeded retries, status set to failed and job abandoned");
467 }
468  
469 0 ▶ if($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) { } 0 0 0
470 0 ▶ $doc->{_id} = $doc->{_id}->to_string if $self->no_binary_oid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_binary_oid 0 0 0
471  
472 0 $self->job_count($self->job_count + 1);
473 0 $self->log->debug("job_count incremented to " . $self->job_count);
474  
475 0 my $job = MangoX::Queue::Job->new($doc);
476 $job->once(finished => sub {
477 0 ▶ $self->job_count($self->job_count - 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
478 0 $self->log->debug('job_count decremented to ' . $self->job_count);
479 0 });
480  
481 0 $self->delay->reset;
482 0 ▶ $self->emit_safe(consumed => $job) if $self->has_subscribers('consumed');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_subscribers("consumed") 0 0 0
483  
484 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$callback($job); return 1 } } or $self->emit_safe("error", "Error in callback: $@") 0 0 0
485 0 $callback->($job);
486 0 return 1;
487 } or $self->emit_safe(error => "Error in callback: $@");
488 0 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
489 0 ▶ return if $fetch;
Branch: 1 branch
BranchTrueFalseTotal
if $fetch 0 0 0
490 0 ▶ return unless exists $self->consumers->{$consumer_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->consumers->{$consumer_id} 0 0 0
491 Mojo::IOLoop->timer(0, sub {
492 0 ▶ $self->_consume_nonblocking($args, $consumer_id, $callback, 0) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
493 0 );
494 0 $self->log->debug("Timer rescheduled (recursive immediate), consumer_id $consumer_id has timer ID: " . $self->consumers->{$consumer_id});
495 } else {
496 0 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
497 0 ▶ return if $fetch;
Branch: 1 branch
BranchTrueFalseTotal
if $fetch 0 0 0
498 $self->delay->wait(sub {
499 0 ▶ return unless exists $self->consumers->{$consumer_id};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->consumers->{$consumer_id} 0 0 0
500 0 $self->_consume_nonblocking($args, $consumer_id, $callback, 0);
501 0 });
502 0 $self->log->debug("Timer rescheduled (recursive delayed), consumer_id $consumer_id has timer ID: " . $self->consumers->{$consumer_id});
503 0 return undef;
504 }
505 0 });
506 }
507  
508 12 1;
509  
510 =encoding utf8
511  
512 =head1 NAME
513  
514 MangoX::Queue - A MongoDB queue implementation using Mango
515  
516 =head1 DESCRIPTION
517  
518 L<MangoX::Queue> is a MongoDB backed queue implementation using L<Mango> to support
519 blocking and non-blocking queues.
520  
521 L<MangoX::Queue> makes no attempt to handle the L<Mango> connection, database or
522 collection - pass in a collection to the constructor and L<MangoX::Queue> will
523 use it. The collection can be plain, capped or sharded.
524  
525 For an introduction to L<MangoX::Queue>, see L<MangoX::Queue::Tutorial>.
526  
527 =head1 SYNOPSIS
528  
529 =head2 Non-blocking
530  
531 Non-blocking mode requires a running L<Mojo::IOLoop>.
532  
533 my $queue = MangoX::Queue->new(collection => $mango_collection);
534  
535 # To add a job
536 enqueue $queue 'test' => sub { my $id = shift; };
537  
538 # To set options
539 enqueue $queue priority => 1, created => bson_time, 'test' => sub { my $id = shift; };
540  
541 # To watch for a specific job status
542 watch $queue $id, 'Complete' => sub {
543 # Job status is 'Complete'
544 };
545  
546 # To fetch a job
547 fetch $queue sub {
548 my ($job) = @_;
549 # ...
550 };
551  
552 # To get a job by id
553 get $queue $id => sub { my $job = shift; };
554  
555 # To requeue a job
556 requeue $queue $job => sub { my $id = shift; };
557  
558 # To dequeue a job
559 dequeue $queue $id => sub { };
560  
561 # To consume a queue
562 my $consumer = consume $queue sub {
563 my ($job) = @_;
564 # ...
565 };
566  
567 # To stop consuming a queue
568 release $queue $consumer;
569  
570 # To listen for errors
571 on $queue error => sub { my ($queue, $error) = @_; };
572  
573 =head2 Blocking
574  
575 my $queue = MangoX::Queue->new(collection => $mango_collection);
576  
577 # To add a job
578 my $id = enqueue $queue 'test';
579  
580 # To set options
581 my $id = enqueue $queue priority => 1, created => bson_time, 'test';
582  
583 # To watch for a specific job status
584 watch $queue $id;
585  
586 # To fetch a job
587 my $job = fetch $queue;
588  
589 # To get a job by id
590 my $job = get $queue $id;
591  
592 # To requeue a job
593 my $id = requeue $queue $job;
594  
595 # To dequeue a job
596 dequeue $queue $id;
597  
598 # To consume a queue
599 while(my $job = consume $queue) {
600 # ...
601 }
602  
603 =head2 Other
604  
605 my $queue = MangoX::Queue->new(collection => $mango_collection);
606  
607 # To listen for events
608 on $queue enqueued => sub ( my ($queue, $job) = @_; };
609 on $queue dequeued => sub ( my ($queue, $job) = @_; };
610 on $queue consumed => sub { my ($queue, $job) = @_; };
611  
612 # To register a plugin
613 plugin $queue 'MangoX::Queue::Plugin::Statsd';
614  
615 =head1 ATTRIBUTES
616  
617 L<MangoX::Queue> implements the following attributes.
618  
619 =head2 collection
620  
621 my $collection = $queue->collection;
622 $queue->collection($mango->db('foo')->collection('bar'));
623  
624 my $queue = MangoX::Queue->new(collection => $collection);
625  
626 The L<Mango::Collection> representing the MongoDB queue collection.
627  
628 =head2 delay
629  
630 my $delay = $queue->delay;
631 $queue->delay(MangoX::Queue::Delay->new);
632  
633 The L<MangoX::Queue::Delay> responsible for dynamically controlling the
634 delay between queue queries.
635  
636 =head2 concurrent_job_limit
637  
638 my $concurrent_job_limit = $queue->concurrent_job_limit;
639 $queue->concurrent_job_limit(20);
640  
641 The maximum number of concurrent jobs (jobs consumed from the queue and unfinished). Defaults to 10.
642  
643 This only applies to jobs on the queue in non-blocking mode. L<MangoX::Queue> has an internal counter
644 that is incremented when a job has been consumed from the queue (in non-blocking mode). The job
645 returned is a L<MangoX::Queue::Job> instance and has a descructor method that is called to decrement
646 the internal counter. See L<MangoX::Queue::Job> for more details.
647  
648 Set to -1 to disable queue concurrency limits. B<Use with caution>, this could result in
649 out of memory errors or an extremely slow event loop.
650  
651 If you need to decrement the job counter early (e.g. to hold on to a reference to the job after you've
652 finished processing it), you can call the C<finished> method on the L<MangoX::Queue::Job> object.
653  
654 $job->finished;
655  
656 =head2 failed_status
657  
658 $self->failed_status('Failed');
659  
660 Set a custom failed status.
661  
662 =head2 no_binary_oid
663  
664 $no_bin = $self->no_binary_oid;
665 $self->no_binary_oid(1);
666  
667 Set to 1 to disable binary ObjectIDs being returned by MangoX::Queue in the Job object.
668  
669 =head2 pending_status
670  
671 $self->pending_status('Pending');
672 $self->pending_status(['Pending', 'pending']);
673  
674 Set a custom pending status, can be an array ref.
675  
676 =head2 plugins
677  
678 my $plugins = $queue->plugins;
679  
680 Returns a hash containing the plugins registered with this queue.
681  
682 =head2 processing_status
683  
684 $self->processing_status('Processing');
685  
686 Set a custom processing status.
687  
688 =head2 retries
689  
690 my $retries = $queue->retries;
691 $queue->retries(5);
692  
693 The number of times a job will be picked up from the queue before it is
694 marked as failed.
695  
696 =head2 timeout
697  
698 my $timeout = $queue->timeout;
699 $queue->timeout(10);
700  
701 The time (in seconds) a job is allowed to stay in Retrieved state before
702 it is released back into Pending state. Defaults to 60 seconds.
703  
704 =head1 EVENTS
705  
706 L<MangoX::Queue> inherits from L<Mojo::EventEmitter> and emits the following events.
707  
708 Events are emitted only for actions on the current queue object, not the entire queue.
709  
710 =head2 consumed
711  
712 on $queue consumed => sub {
713 my ($queue, $job) = @_;
714 # ...
715 };
716  
717 Emitted when an item is consumed (either via consume or fetch)
718  
719 =head2 dequeued
720  
721 on $queue dequeued => sub {
722 my ($queue, $job) = @_;
723 # ...
724 };
725  
726 Emitted when an item is dequeued
727  
728 =head2 enqueued
729  
730 on $queue enqueued => sub {
731 my ($queue, $job) = @_;
732 # ...
733 };
734  
735 Emitted when an item is enqueued
736  
737 =head2 concurrent_job_limit_reached
738  
739 on $queue enqueued => sub {
740 my ($queue, $concurrent_job_limit) = @_;
741 # ...
742 };
743  
744 Emitted when a job is found but the </concurrent_job_limit> limit has been reached.
745  
746 =head1 METHODS
747  
748 L<MangoX::Queue> implements the following methods.
749  
750 =head2 consume
751  
752 # In blocking mode
753 while(my $job = consume $queue) {
754 # ...
755 }
756  
757 # In non-blocking mode
758 consume $queue sub {
759 my ($job) = @_;
760 # ...
761 };
762  
763 Waits for jobs to arrive on the queue, sleeping between queue checks using
764 L<MangoX::Queue::Delay> or L<Mojo::IOLoop>.
765  
766 Currently sets the status to 'Retrieved' before returning the job.
767  
768 =head2 dequeue
769  
770 my $job = fetch $queue;
771 dequeue $queue $job;
772  
773 Dequeues a job. Currently removes it from the collection.
774  
775 =head2 enqueue
776  
777 my $id = enqueue $queue 'job name';
778 my $id = enqueue $queue [ 'some', 'data' ];
779 my $id = enqueue $queue +{ foo => 'bar' };
780  
781 Add an item to the queue in blocking mode. The default priority is 1 and status is 'Pending'.
782  
783 You can set queue options including priority, created and status.
784  
785 my $id = enqueue $queue,
786 priority => 1,
787 created => bson_time,
788 status => 'Pending',
789 +{
790 foo => 'bar'
791 };
792  
793 For non-blocking mode, pass in a coderef as the final argument.
794  
795 my $id = enqueue $queue 'job_name' => sub {
796 # ...
797 };
798  
799 my $id = enqueue $queue priority => 1, +{
800 foo => 'bar',
801 } => sub {
802 # ...
803 };
804  
805 Sets the status to 'Pending' by default.
806  
807 =head2 fetch
808  
809 # In blocking mode
810 my $job = fetch $queue;
811  
812 # In non-blocking mode
813 fetch $queue sub {
814 my ($job) = @_;
815 # ...
816 };
817  
818 Fetch a single job from the queue, returning undef if no jobs are available.
819  
820 Currently sets job status to 'Retrieved'.
821  
822 =head2 get
823  
824 # In non-blocking mode
825 get $queue $id => sub {
826 my ($job) = @_;
827 # ...
828 };
829  
830 # In blocking mode
831 my $job = get $queue $id;
832  
833 Gets a job from the queue by ID. Doesn't change the job status.
834  
835 You can also pass in a job instead of an ID.
836  
837 $job = get $queue $job;
838  
839 =head2 get_options
840  
841 my $options = $queue->get_options;
842  
843 Returns the L<Mango::Collection> options hash used by find_and_modify to
844 identify and update available queue items.
845  
846 =head2 release
847  
848 my $consumer = consume $queue sub {
849 # ...
850 };
851 release $queue $consumer;
852  
853 Releases a non-blocking consumer from watching a queue.
854  
855 =head2 requeue
856  
857 my $job = fetch $queue;
858 requeue $queue $job;
859  
860 Requeues a job. Sets the job status to 'Pending'.
861  
862 =head2 update
863  
864 my $job = fetch $queue;
865 $job->{status} = 'Failed';
866 update $queue $job;
867  
868 Updates a job in the queue.
869  
870 =head2 watch
871  
872 Wait for a job to enter a certain status.
873  
874 # In blocking mode
875 my $id = enqueue $queue 'test';
876 watch $queue $id, 'Complete'; # blocks until job is complete
877  
878 # In non-blocking mode
879 my $id = enqueue $queue 'test';
880 watch $queue $id, 'Complete' => sub {
881 # ...
882 };
883  
884 =head1 FUTURE JOBS
885  
886 Jobs can be queued in advance by setting a delay_until attribute:
887  
888 enqueue $queue delay_until => (time + 20), "job name";
889  
890 =head1 ERRORS
891  
892 Errors are reported by MangoX::Queue using callbacks and L<Mojo::EventEmitter>
893  
894 To listen for all errors on a queue, subscribe to the 'error' event:
895  
896 $queue->on(error => sub {
897 my ($queue, $job, $error) = @_;
898 # ...
899 });
900  
901 To check for errors against an individual update, enqueue or dequeue call,
902 you can check for an error argument to the callback sub:
903  
904 enqueue $queue +$job => sub {
905 my ($job, $error) = @_;
906  
907 if($error) {
908 # ...
909 }
910 }
911  
912 =head1 CONTRIBUTORS
913  
914 =over
915  
916 =item Ben Vinnerd, ben@vinnerd.com
917  
918 =back
919  
920 =head1 SEE ALSO
921  
922 L<MangoX::Queue::Tutorial>, L<Mojolicious>, L<Mango>
923  
924 =cut
↑ Summary Queue/Delay.pm »