blib/lib/Queue/DBI/Element.pm

filter: Statementstmt 85.5% 71 / 83 Branchbran 37.5% 18 / 48 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 14 / 14 Podpod 100% 10 / 10 totaltotal 62.6% 119 / 190 CC 33 SCAR 27.3
CC
33
Cov
62%
success
33.2
requeue
18.6
lock
11.6
CRAP 15.3
↑ Summary blib/lib/Queue/DBI.pm »
1 package Queue::DBI::Element;
2  
3 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 Queue::DBI::Element - An object representing an element pulled from the queue.
13  
14  
15 =head1 VERSION
16  
17 Version 2.6.2
18  
19 =cut
20  
21 39 our $VERSION = '2.6.2';
22  
23  
24 =head1 SYNOPSIS
25  
26 Please refer to the documentation for Queue::DBI.
27  
28 =head1 METHODS
29  
30 =head2 new()
31  
32 Create a new Queue::DBI::Element object.
33  
34 my $element = Queue::DBI::Element->new(
35 'queue' => $queue,
36 'data' => $data,
37 'id' => $id,
38 'requeue_count' => $requeue_count,
39 'created' => $created,
40 );
41  
42 All parameters are mandatory and correspond respectively to the Queue::DBI
43 object used to pull the element's data, the data, the ID of the element
44 in the database and the number of times the element has been requeued before.
45  
46 It is not recommended for direct use. You should be using the following to get
47 Queue::DBI::Element objects:
48  
49 my $queue = $queue->next();
50  
51 =cut
52  
53 sub new
54 {
55 28 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=6 SCAR=20.7
Pod documented
56  
57 # Check parameters
58 28 foreach my $arg ( qw( data id requeue_count created ) )
59 {
60 croak "Argument '$arg' is needed to create the Queue::DBI object"
61 112 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 0 112 112
Condition33%1/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
00112
MC/DC0%0/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table33%1/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
62 }
63 croak 'Pass a Queue::DBI object to create an Queue::DBI::Element object'
64 28 ▶ unless defined( $args{'queue'} ) && $args{'queue'}->isa( 'Queue::DBI' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"queue"} and $args{"queue"}->isa("Queue::DBI") 0 28 28
Condition33%1/3
defined $args{"queue"} and $args{"queue"}->isa("Queue::DBI")
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $args{"queue"} and $args{"queue"}->isa("Queue::DBI")
defined $args{"queue"} $args{"queue"}->isa("Queue::DBI")
Truth table33%1/3
A and B
A= defined $args{"queue"}B= $args{"queue"}->isa("Queue::DBI")
ABresult
0X0
100
111
65  
66 # Create the object
67 my $self = bless(
68 {
69 'queue' => $args{'queue'},
70 'data' => $args{'data'},
71 'id' => $args{'id'},
72 'requeue_count' => $args{'requeue_count'},
73 28 'created' => $args{'created'},
74 },
75 $class
76 );
77  
78 28 return $self;
79 }
80  
81  
82 =head2 lock()
83  
84 Locks the element so that another process acting on the queue cannot get a hold
85 of it
86  
87 if ( $element->lock() )
88 {
89 print "Element successfully locked.\n";
90 }
91 else
92 {
93 print "The element has already been removed or locked.\n";
94 }
95  
96 =cut
97  
98 sub lock ## no critic (Subroutines::ProhibitBuiltinHomonyms)
99 {
100 19 ▶ my ( $self ) = @_;
Subroutine lock: called CC=8 SCAR=24.5
Pod documented
101 19 my $queue = $self->get_queue();
102 19 my $verbose = $queue->get_verbose();
103 19 my $dbh = $queue->get_dbh();
104 19 ▶ carp "Entering lock()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 19 19
105  
106 19 ▶ my $rows = $dbh->do(
Condition33%1/3
$dbh->do(sprintf("\n\t\t\t\tUPDATE %s\n\t\t\t\tSET lock_time = ?\n\t\t\t\tWHERE queue_element_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t", $dbh->quote_identifier($queue->get_queue_elements_table_name)), {}, time, $self->id) || croak('Cannot lock element: ' . $dbh->errstr)
l!l&&r!l&&!r
1900
MC/DC0%0/2
$dbh->do(sprintf("\n\t\t\t\tUPDATE %s\n\t\t\t\tSET lock_time = ?\n\t\t\t\tWHERE queue_element_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t", $dbh->quote_identifier($queue->get_queue_elements_table_name)), {}, time, $self->id) || croak('Cannot lock element: ' . $dbh->errstr)
$dbh->do(sprintf("\n\t\t\t\tUPDATE %s\n\t\t\t\tSET lock_time = ?\n\t\t\t\tWHERE queue_element_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t", $dbh->quote_identifier($queue->get_queue_elements_table_name)), {}, time, $self->id) croak('Cannot lock element: ' . $dbh->errstr)
Truth table33%1/3
A || B
A= $dbh->do(sprintf("\n\t\t\t\tUPDATE %s\n\t\t\t\tSET lock_time = ?\n\t\t\t\tWHERE queue_element_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t", $dbh->quote_identifier($queue->get_queue_elements_table_name)), {}, time, $self->id)B= croak('Cannot lock element: ' . $dbh->errstr)
ABresult
1X1
011
000
107 sprintf(
108 q|
109 UPDATE %s
110 SET lock_time = ?
111 WHERE queue_element_id = ?
112 AND lock_time IS NULL
113 |,
114 $dbh->quote_identifier( $queue->get_queue_elements_table_name() ),
115 ),
116 {},
117 time(),
118 $self->id(),
119 ) || croak 'Cannot lock element: ' . $dbh->errstr;
120  
121 19 ▶ my $success = ( defined( $rows ) && ( $rows == 1 ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $rows && $rows == 1 ? : 19 0 19
Condition33%1/3
defined $rows and $rows == 1
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $rows and $rows == 1
defined $rows $rows == 1
Truth table33%1/3
A and B
A= defined $rowsB= $rows == 1
ABresult
0X0
100
111
122 19 ▶ carp "Element locked: " . ( $success ? 'success' : 'already locked or gone' ) . "." if $verbose;
Branch: 2 branches
BranchTrueFalseTotal
if $verbose 0 19 19
$success ? : 0 0 0
123  
124 19 ▶ carp "Leaving lock()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 19 19
125 19 return $success;
126 }
127  
128  
129 =head2 requeue()
130  
131 In case the processing of an element has failed
132  
133 if ( $element->requeue() )
134 {
135 print "Element successfully requeued.\n";
136 }
137 else
138 {
139 print "The element has already been removed or been requeued.\n";
140 }
141  
142 =cut
143  
144 sub requeue
145 {
146 7 ▶ my ( $self ) = @_;
Subroutine requeue: called CC=10 SCAR=29.2
Pod documented
147 7 my $queue = $self->get_queue();
148 7 my $verbose = $queue->get_verbose();
149 7 my $dbh = $queue->get_dbh();
150 7 ▶ carp "Entering requeue()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 7 7
151  
152 7 my $rows = $dbh->do(
153 sprintf(
154 q|
155 UPDATE %s
156 SET
157 lock_time = NULL,
158 requeue_count = requeue_count + 1
159 WHERE queue_element_id = ?
160 AND lock_time IS NOT NULL
161 |,
162 $dbh->quote_identifier( $queue->get_queue_elements_table_name() ),
163 ),
164 {},
165 $self->id(),
166 );
167  
168 # Since Queue::DBI does not enclose the SELECTing of a queue_element
169 # to be requeued, and this actual requeueing, it is possible for the
170 # element to be requeued by another process in-between. It may even
171 # be requeued, relocked, and successfully removed in-between. In either
172 # case, the number of rows affected would be 0, and do() would return
173 # 0E0, perl's "0 but true" value. This is not an error. However, if
174 # -1 or undef is returned, DBI.pm encountered some sort of error.
175 7 ▶ if ( ! defined( $rows ) || $rows == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rows or $rows == -1) 0 7 7
Condition33%1/3
not defined $rows or $rows == -1
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $rows or $rows == -1
not defined $rows $rows == -1
Truth table33%1/3
not(A) or B
A= not defined $rowsB= $rows == -1
ABresult
1X1
011
000
176 {
177 # Always carp the information, since it is an error that
178 # most likely doesn't come from this module.
179 0 my $error = $dbh->errstr();
180 0 ▶ carp 'Cannot requeue element: ' . ( defined( $error ) ? $error : 'no error returned by DBI' );
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
181 0 return 0;
182 }
183  
184 7 ▶ my $requeued = ( $rows == 1 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rows == 1 ? : 7 0 7
185 7 ▶ carp "Element requeued: " . ( $requeued ? 'done' : 'already requeued or gone' ) . "." if $verbose;
Branch: 2 branches
BranchTrueFalseTotal
if $verbose 0 7 7
$requeued ? : 0 0 0
186  
187 # Update the requeue_count on the object as well if the database update was
188 # successful.
189 7 ▶ $self->{'requeue_count'}++
Branch: 1 branch
BranchTrueFalseTotal
if $requeued 7 0 7
190 if $requeued;
191  
192 7 ▶ carp "Leaving requeue()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 7 7
193 7 return $requeued;
194 }
195  
196  
197 =head2 success()
198  
199 Removes the element from the queue after its processing has successfully been
200 completed.
201  
202 if ( $element->success() )
203 {
204 print "Element successfully removed from queue.\n";
205 }
206 else
207 {
208 print "The element has already been removed.\n";
209 }
210  
211 =cut
212  
213 sub success
214 {
215 13 ▶ my ( $self ) = @_;
Subroutine success: called CC=11 SCAR=35.0
Pod documented
216 13 my $queue = $self->get_queue();
217 13 my $verbose = $queue->get_verbose();
218 13 my $dbh = $queue->get_dbh();
219 13 ▶ carp "Entering success()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 13 13
220  
221 # Possible improvement:
222 # Add $self->{'lock_time'} in lock() and insist that it matches that value
223 # when trying to delete the element here.
224  
225 # First, we try to delete the LOCKED element.
226 13 my $rows = $dbh->do(
227 sprintf(
228 q|
229 DELETE
230 FROM %s
231 WHERE queue_element_id = ?
232 AND lock_time IS NOT NULL
233 |,
234 $dbh->quote_identifier( $queue->get_queue_elements_table_name() ),
235 ),
236 {},
237 $self->id(),
238 );
239  
240 13 ▶ if ( ! defined( $rows ) || $rows == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rows or $rows == -1) 0 13 13
Condition33%1/3
not defined $rows or $rows == -1
l!l&&r!l&&!r
0013
MC/DC0%0/2
not defined $rows or $rows == -1
not defined $rows $rows == -1
Truth table33%1/3
not(A) or B
A= not defined $rowsB= $rows == -1
ABresult
1X1
011
000
241 {
242 0 croak 'Cannot remove element: ' . $dbh->errstr();
243 }
244  
245 13 my $success = 0;
246 13 ▶ if ( $rows == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rows == 1) { } 13 0 13
247 {
248 # A LOCKED element was found and deleted, this is a success.
249 13 ▶ carp "Found a LOCKED element and deleted it. Element successfully processed." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 13 13
250 13 $success = 1;
251 }
252 else
253 {
254 # No LOCKED element found to delete, try to find an UNLOCKED one in case it
255 # got requeued by a parallel process.
256 0 my $deleted_rows = $dbh->do(
257 sprintf(
258 q|
259 DELETE
260 FROM %s
261 WHERE queue_element_id = ?
262 |,
263 $dbh->quote_identifier( $queue->get_queue_elements_table_name() ),
264 ),
265 {},
266 $self->id(),
267 );
268  
269 0 ▶ if ( ! defined( $deleted_rows ) || $deleted_rows == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $deleted_rows or $deleted_rows == -1) 0 0 0
Condition0%0/3
not defined $deleted_rows or $deleted_rows == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $deleted_rows or $deleted_rows == -1
not defined $deleted_rows $deleted_rows == -1
Truth table0%0/3
not(A) or B
A= not defined $deleted_rowsB= $deleted_rows == -1
ABresult
1X1
011
000
270 {
271 0 croak 'Cannot remove element: ' . $dbh->errstr;
272 }
273  
274 0 ▶ if ( $deleted_rows == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted_rows == 1) { } 0 0 0
275 {
276 # An UNLOCKED element was found and deleted. It probably means that
277 # another process is still working on that element as well (possibly
278 # because this element's lock timed-out, got cleaned up and picked by
279 # another process).
280 # Always carp for those, technically we processed the element successfully
281 # so deleting it is the correct step to take, but we still want to throw
282 # some warning for the user.
283 0 carp 'Another process is probably working on the same element, as it was found UNLOCKED when we deleted it. '
284 . 'Check parallelization issues in your code!';
285 0 $success = 1;
286 }
287 else
288 {
289 # No element found at all. It probably means that another process had been
290 # working on that element, but completed successfully its run and deleted
291 # it.
292 0 ▶ carp 'Another process has probably worked on this element and already deleted it after completing its operations. '
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
293 . 'Check parallelization issues in your code!' if $verbose;
294 0 $success = 0;
295 }
296 }
297  
298 13 ▶ carp "Leaving success()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 13 13
299 13 return $success;
300 }
301  
302  
303 =head2 data()
304  
305 Returns the data initially queued.
306  
307 my $data = $element->data();
308  
309 =cut
310  
311 sub data
312 {
313 5 ▶ my ( $self ) = @_;
Subroutine data: called CC=1 SCAR=0.0
Pod documented
314  
315 5 return $self->{'data'};
316 }
317  
318  
319 =head2 requeue_count()
320  
321 Returns the number of times that the current element has been requeued.
322  
323 my $requeue_count = $element->requeue_count();
324  
325 =cut
326  
327 sub requeue_count
328 {
329 6 ▶ my ( $self ) = @_;
Subroutine requeue_count: called CC=1 SCAR=0.0
Pod documented
330  
331 6 return $self->{'requeue_count'};
332 }
333  
334  
335 =head2 id()
336  
337 Returns the ID of the current element
338  
339 my $id = $element->id();
340  
341 =cut
342  
343 sub id
344 {
345 74 ▶ my ( $self ) = @_;
Subroutine id: called CC=1 SCAR=0.0
Pod documented
346  
347 74 return $self->{'id'};
348 }
349  
350  
351 =head2 get_created_time()
352  
353 Returns the unixtime at which the element was originally created.
354  
355 my $created = $element->get_created_time();
356  
357 =cut
358  
359 sub get_created_time
360 {
361 2 ▶ my ( $self ) = @_;
Subroutine get_created_time: called CC=1 SCAR=0.0
Pod documented
362  
363 2 return $self->{'created'};
364 }
365  
366  
367 =head2 is_over_lifetime()
368  
369 Returns a boolean indicating whether the current element is over the lifetime
370 specified when instanciating the queue. This is especially helpful if you
371 retrieve a large batch of elements and do long processing operations on each
372 of them.
373  
374 my $is_over_lifetime = $element->is_over_lifetime();
375  
376 =cut
377  
378 sub is_over_lifetime
379 {
380 2 ▶ my ( $self ) = @_;
Subroutine is_over_lifetime: called CC=2 SCAR=7.0
Pod documented
381 2 my $queue = $self->get_queue();
382 2 my $lifetime = $queue->get_lifetime();
383  
384 # If the queue doesn't a lifetime, an element will never "expire".
385 2 ▶ return 0 if !defined( $lifetime );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lifetime 0 2 2
386  
387 # Check the time the element was created.
388 2 my $created_time = $self->get_created_time();
389 2 return time() - $created_time > $lifetime;
390 }
391  
392  
393 =head1 INTERNAL METHODS
394  
395 =head2 get_queue()
396  
397 Returns the Queue::DBI object used to pull the current element.
398  
399 my $queue = $element->get_queue();
400  
401 =cut
402  
403 sub get_queue
404 {
405 41 ▶ my ( $self ) = @_;
Subroutine get_queue: called CC=1 SCAR=0.0
Pod documented
406  
407 41 return $self->{'queue'};
408 }
409  
410  
411 =head1 BUGS
412  
413 Please report any bugs or feature requests through the web interface at
414 L<https://github.com/guillaumeaubert/Queue-DBI/issues/new>.
415 I will be notified, and then you'll automatically be notified of progress on
416 your bug as I make changes.
417  
418  
419 =head1 SUPPORT
420  
421 You can find documentation for this module with the perldoc command.
422  
423 perldoc Queue::DBI::Element
424  
425  
426 You can also look for information at:
427  
428 =over 4
429  
430 =item * GitHub's request tracker
431  
432 L<https://github.com/guillaumeaubert/Queue-DBI/issues>
433  
434 =item * AnnoCPAN: Annotated CPAN documentation
435  
436 L<http://annocpan.org/dist/Queue-DBI>
437  
438 =item * CPAN Ratings
439  
440 L<http://cpanratings.perl.org/d/Queue-DBI>
441  
442 =item * MetaCPAN
443  
444 L<https://metacpan.org/release/Queue-DBI>
445  
446 =back
447  
448  
449 =head1 AUTHOR
450  
451 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
452 C<< <aubertg at cpan.org> >>.
453  
454  
455 =head1 ACKNOWLEDGEMENTS
456  
457 I originally developed this project for ThinkGeek
458 (L<http://www.thinkgeek.com/>). Thanks for allowing me to open-source it!
459  
460  
461 =head1 COPYRIGHT & LICENSE
462  
463 Copyright 2009-2015 Guillaume Aubert.
464  
465 This program is free software: you can redistribute it and/or modify it under
466 the terms of the GNU General Public License version 3 as published by the Free
467 Software Foundation.
468  
469 This program is distributed in the hope that it will be useful, but WITHOUT ANY
470 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
471 PARTICULAR PURPOSE. See the GNU General Public License for more details.
472  
473 You should have received a copy of the GNU General Public License along with
474 this program. If not, see http://www.gnu.org/licenses/
475  
476 =cut
477  
478 39 1;
↑ Summary blib/lib/Queue/DBI.pm »