blib/lib/Queue/DBI.pm

filter: Statementstmt 96.9% 193 / 199 Branchbran 68.3% 97 / 142 Conditioncond 69.5% 57 / 82 MC/DCmcdc 46.1% 24 / 52 Subroutinesub 87.5% 28 / 32 Podpod 100% 24 / 24 totaltotal 79.6% 423 / 531 SCAR 51.0
CC
103
Cov
82%
retrieve_batch
28.4
new
19.0
purge
16.0
CRAP 164.5
↑ Summary blib/lib/Queue/DBI/Admin.pm »
1 package Queue::DBI;
2  
3 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 38 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use Storable qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use MIME::Base64 qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 38 ▶use Queue::DBI::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 =head1 NAME
16  
17 Queue::DBI - A queueing module with an emphasis on safety, using DBI as a storage system for queued data.
18  
19  
20 =head1 VERSION
21  
22 Version 2.6.1
23  
24 =cut
25  
26 38 our $VERSION = '2.6.1';
27  
28 38 our $DEFAULT_QUEUES_TABLE_NAME = 'queues';
29  
30 38 our $DEFAULT_QUEUE_ELEMENTS_TABLE_NAME = 'queue_elements';
31  
32 38 our $MAX_VALUE_SIZE = 65535;
33  
34  
35 =head1 SYNOPSIS
36  
37 This module allows you to safely use a queueing system by preventing
38 backtracking, infinite loops and data loss.
39  
40 An emphasis of this distribution is to provide an extremely reliable dequeueing
41 mechanism without having to use transactions.
42  
43 use Queue::DBI;
44 my $queue = Queue::DBI->new(
45 'queue_name' => $queue_name,
46 'database_handle' => $dbh,
47 'cleanup_timeout' => 3600,
48 'verbose' => 1,
49 );
50  
51 $queue->enqueue( $data );
52  
53 while ( my $queue_element = $queue->next() )
54 {
55 next
56 unless $queue_element->lock();
57  
58 eval {
59 # Do some work
60 process( $queue_element->{'email'} );
61 };
62 if ( $@ )
63 {
64 # Something failed, we clear the lock but don't delete the record in the
65 # queue so that we can try again next time
66 $queue_element->requeue();
67 }
68 else
69 {
70 # All good, remove definitively the element
71 $queue_element->success();
72 }
73 }
74  
75 # Requeue items that have been locked for more than 6 hours
76 $queue->cleanup( 6 * 3600 );
77  
78  
79 =head1 SUPPORTED DATABASES
80  
81 This distribution currently supports:
82  
83 =over 4
84  
85 =item * SQLite
86  
87 =item * MySQL
88  
89 =item * PostgreSQL
90  
91 =back
92  
93 Please contact me if you need support for another database type, I'm always
94 glad to add extensions if you can help me with testing.
95  
96  
97 =head1 METHODS
98  
99 =head2 new()
100  
101 Create a new Queue::DBI object.
102  
103 my $queue = Queue::DBI->new(
104 'queue_name' => $queue_name,
105 'database_handle' => $dbh,
106 'cleanup_timeout' => 3600,
107 'verbose' => 1,
108 'max_requeue_count' => 5,
109 );
110  
111 # Custom table names (optional).
112 my $queue = Queue::DBI->new(
113 'queue_name' => $queue_name,
114 'database_handle' => $dbh,
115 'cleanup_timeout' => 3600,
116 'verbose' => 1,
117 'max_requeue_count' => 5,
118 'queues_table_name' => $custom_queues_table_name,
119 'queue_elements_table_name' => $custom_queue_elements_table_name,
120 );
121  
122 Parameters:
123  
124 =over 4
125  
126 =item * 'queue_name'
127  
128 Mandatory, the name of the queue elements will be added to / removed from.
129  
130 =item * 'database handle'
131  
132 Mandatory, a DBI object.
133  
134 =item * 'cleanup_timeout'
135  
136 Optional, if set to an integer representing a time in seconds, the module will
137 automatically make available again elements that have been locked longuer than
138 that time.
139  
140 =item * 'verbose'
141  
142 Optional, control the verbosity of the warnings in the code. 0 will not display
143 any warning; 1 will only give one line warnings about the current operation;
144 2 will also usually output the SQL queries performed.
145  
146 =item * 'max_requeue_count'
147  
148 By default, Queue:::DBI will retrieve again the queue elements that were
149 requeued without limit to the number of times they have been requeued. Use this
150 option to specify how many times an element can be requeued before it is
151 ignored when retrieving elements.
152  
153 =item * 'queues_table_name'
154  
155 By default, Queue::DBI uses a table named 'queues' to store the queue
156 definitions. This allows using your own name, if you want to support separate
157 queuing systems or legacy systems.
158  
159 =item * 'queue_elements_table_name'
160  
161 By default, Queue::DBI uses a table named 'queue_elements' to store the queued
162 data. This allows using your own name, if you want to support separate queuing
163 systems or legacy systems.
164  
165 =item * 'lifetime'
166  
167 By default, Queue:::DBI will fetch elements regardless of how old they are. Use
168 this option to specify how old (in seconds) an element can be and still be
169 retrieved for processing.
170  
171 =back
172  
173 =cut
174  
175 sub new
176 {
177 50 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=19 SCAR=29.5
Pod documented
178  
179 # Check parameters.
180 50 foreach my $arg ( qw( queue_name database_handle ) )
181 {
182 croak "Argument '$arg' is needed to create the Queue::DBI object"
183 99 ▶ if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{$arg} or $args{$arg} eq "" 2 97 99
Condition66%2/3
not defined $args{$arg} or $args{$arg} eq ""
l!l&&r!l&&!r
2097
MC/DC50%1/2
not defined $args{$arg} or $args{$arg} eq ""
not defined $args{$arg} $args{$arg} eq ""
Truth table66%2/3
not(A) or B
A= not defined $args{$arg}B= $args{$arg} eq ""
ABresult
1X1
011
000
184 }
185 croak 'Argument "cleanup_timeout" must be an integer representing seconds'
186 48 ▶ if defined( $args{'cleanup_timeout'} ) && ( $args{'cleanup_timeout'} !~ m/^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"cleanup_timeout"} and not $args{"cleanup_timeout"} =~ /^\d+$/ 1 47 48
Condition100%3/3
defined $args{"cleanup_timeout"} and not $args{"cleanup_timeout"} =~ /^\d+$/
!ll&&!rl&&r
17301
MC/DC100%2/2
defined $args{"cleanup_timeout"} and not $args{"cleanup_timeout"} =~ /^\d+$/
defined $args{"cleanup_timeout"} not $args{"cleanup_timeout"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= defined $args{"cleanup_timeout"}B= not $args{"cleanup_timeout"} =~ /^\d+$/
ABresult
0X0
100
111
187 croak 'Argument "lifetime" must be an integer representing seconds'
188 47 ▶ if defined( $args{'lifetime'} ) && ( $args{'lifetime'} !~ m/^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"lifetime"} and not $args{"lifetime"} =~ /^\d+$/ 1 46 47
Condition100%3/3
defined $args{"lifetime"} and not $args{"lifetime"} =~ /^\d+$/
!ll&&!rl&&r
4241
MC/DC100%2/2
defined $args{"lifetime"} and not $args{"lifetime"} =~ /^\d+$/
defined $args{"lifetime"} not $args{"lifetime"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= defined $args{"lifetime"}B= not $args{"lifetime"} =~ /^\d+$/
ABresult
0X0
100
111
189 croak 'Argument "serializer_freeze" must be a code reference'
190 46 ▶ if defined( $args{'serializer_freeze'} ) && !Data::Validate::Type::is_coderef( $args{'serializer_freeze'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"serializer_freeze"} and not Data::Validate::Type::is_coderef($args{"serializer_freeze"}) 1 45 46
Condition100%3/3
defined $args{"serializer_freeze"} and not Data::Validate::Type::is_coderef($args{"serializer_freeze"})
!ll&&!rl&&r
4231
MC/DC100%2/2
defined $args{"serializer_freeze"} and not Data::Validate::Type::is_coderef($args{"serializer_freeze"})
defined $args{"serializer_freeze"} not Data::Validate::Type::is_coderef($args{"serializer_freeze"})
Truth table100%3/3
A and not(B)
A= defined $args{"serializer_freeze"}B= not Data::Validate::Type::is_coderef($args{"serializer_freeze"})
ABresult
0X0
100
111
191 croak 'Argument "serializer_thaw" must be a code reference'
192 45 ▶ if defined( $args{'serializer_thaw'} ) && !Data::Validate::Type::is_coderef( $args{'serializer_thaw'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"serializer_thaw"} and not Data::Validate::Type::is_coderef($args{"serializer_thaw"}) 1 44 45
Condition100%3/3
defined $args{"serializer_thaw"} and not Data::Validate::Type::is_coderef($args{"serializer_thaw"})
!ll&&!rl&&r
4131
MC/DC100%2/2
defined $args{"serializer_thaw"} and not Data::Validate::Type::is_coderef($args{"serializer_thaw"})
defined $args{"serializer_thaw"} not Data::Validate::Type::is_coderef($args{"serializer_thaw"})
Truth table100%3/3
A and not(B)
A= defined $args{"serializer_thaw"}B= not Data::Validate::Type::is_coderef($args{"serializer_thaw"})
ABresult
0X0
100
111
193 croak 'Arguments "serializer_freeze" and "serializer_thaw" must be defined together'
194 44 ▶ if defined( $args{'serializer_freeze'} ) xor defined( $args{'serializer_thaw'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"serializer_freeze"} xor defined $args{"serializer_thaw"} 2 42 44
Condition100%4/4
defined $args{"serializer_freeze"} xor defined $args{"serializer_thaw"}
l&&rl&&!r!l&&r!l&&!r
21140
MC/DC100%2/2
defined $args{"serializer_freeze"} xor defined $args{"serializer_thaw"}
defined $args{"serializer_freeze"} defined $args{"serializer_thaw"}
Truth table100%4/4
A xor B
A= defined $args{"serializer_freeze"}B= defined $args{"serializer_thaw"}
ABresult
110
101
011
000
195  
196 # Create the object.
197 42 my $dbh = $args{'database_handle'};
198 my $self = bless(
199 {
200 'dbh' => $dbh,
201 'queue_name' => $args{'queue_name'},
202 'table_names' =>
203 {
204 'queues' => $args{'queues_table_name'},
205 'queue_elements' => $args{'queue_elements_table_name'},
206 },
207 'serializer' =>
208 {
209 'freeze' => $args{'serializer_freeze'},
210 42 'thaw' => $args{'serializer_thaw'},
211 }
212 },
213 $class
214 );
215  
216 # Find the queue id.
217 42 my $queue_id;
218 {
219 42 local $dbh->{'RaiseError'} = 1;
220 my $data = $dbh->selectrow_arrayref(
221 sprintf(
222 q|
223 SELECT queue_id
224 FROM %s
225 WHERE name = ?
226 |,
227 $dbh->quote_identifier( $self->get_queues_table_name() ),
228 ),
229 {},
230 42 $args{'queue_name'},
231 );
232  
233 42 ▶ $queue_id = defined( $data ) && scalar( @$data ) != 0
Branch: 1 branch
BranchTrueFalseTotal
defined $data && scalar @$data != 0 ? : 36 6 42
Condition66%2/3
defined $data and scalar @$data != 0
!ll&&!rl&&r
6036
MC/DC50%1/2
defined $data and scalar @$data != 0
defined $data scalar @$data != 0
Truth table66%2/3
A and B
A= defined $dataB= scalar @$data != 0
ABresult
0X0
100
111
234 ? $data->[0]
235 : undef;
236 }
237  
238 42 ▶ croak "The queue >$args{'queue_name'}< doesn't exist in the lookup table."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queue_id and $queue_id =~ /^\d+$/ 6 36 42
Condition66%2/3
defined $queue_id and $queue_id =~ /^\d+$/
!ll&&!rl&&r
6036
MC/DC50%1/2
defined $queue_id and $queue_id =~ /^\d+$/
defined $queue_id $queue_id =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $queue_idB= $queue_id =~ /^\d+$/
ABresult
0X0
100
111
239 unless defined( $queue_id ) && ( $queue_id =~ m/^\d+$/ );
240 36 $self->{'queue_id'} = $queue_id;
241  
242 # Set optional parameters.
243 36 $self->set_verbose( $args{'verbose'} );
244 36 $self->set_max_requeue_count( $args{'max_requeue_count'} );
245 36 $self->set_lifetime( $args{'lifetime'} );
246  
247 # Perform queue cleanup if a timeout is specified.
248 $self->cleanup( $args{'cleanup_timeout'} )
249 36 ▶ if defined( $args{'cleanup_timeout'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"cleanup_timeout"} 29 7 36
250  
251 36 return $self;
252 }
253  
254  
255 =head2 get_queue_id()
256  
257 Returns the queue ID corresponding to the current queue object.
258  
259 my $queue_id = $queue->get_queue_id();
260  
261 =cut
262  
263 sub get_queue_id
264 {
265 115 ▶ my ( $self ) = @_;
Subroutine get_queue_id: called CC=1 SCAR=0.0
Pod documented
266  
267 115 return $self->{'queue_id'};
268 }
269  
270  
271 =head2 count()
272  
273 Returns the number of elements in the queue.
274  
275 my $elements_count = $queue->count();
276  
277 Optional parameter:
278  
279 =over 4
280  
281 =item * exclude_locked_elements
282  
283 Exclude locked elements from the count. Default 0.
284  
285 =back
286  
287 my $unlocked_elements_count = $queue->count(
288 exclude_locked_elements => 1
289 );
290  
291 =cut
292  
293 sub count
294 {
295 11 ▶ my ( $self, %args ) = @_;
Subroutine count: called CC=8 SCAR=21.9
Pod documented
296 11 ▶ my $exclude_locked_elements = delete( $args{'exclude_locked_elements'} ) || 0;
Condition100%2/2
delete $args{'exclude_locked_elements'} || 0
l!l
38
MC/DC100%1/1
delete $args{'exclude_locked_elements'} || 0
delete $args{'exclude_locked_elements'}
Truth table100%2/2
A
A= delete $args{'exclude_locked_elements'}
Aresult
11
00
297  
298 11 my $verbose = $self->get_verbose();
299 11 my $dbh = $self->get_dbh();
300 11 ▶ carp "Entering count()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 11 11
301  
302 # Prepare optional additional clause to exclude locked elements.
303 11 ▶ my $exclude_locked_elements_sql = $exclude_locked_elements
Branch: 1 branch
BranchTrueFalseTotal
$exclude_locked_elements ? : 3 8 11
304 ? 'AND lock_time IS NULL'
305 : '';
306  
307 # Count elements.
308 11 my $element_count;
309 {
310 11 local $dbh->{'RaiseError'} = 1;
311 11 my $data = $dbh->selectrow_arrayref(
312 sprintf(
313 q|
314 SELECT COUNT(*)
315 FROM %s
316 WHERE queue_id = ?
317 %s
318 |,
319 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
320 $exclude_locked_elements_sql,
321 ),
322 {},
323 $self->get_queue_id(),
324 );
325 11 ▶ $element_count = defined( $data ) && scalar( @$data ) != 0 && defined( $data->[0] )
Branch: 1 branch
BranchTrueFalseTotal
defined $data && scalar @$data != 0 && defined $data->[0] ? : 11 0 11
Condition33%2/6
defined $data and scalar @$data != 0 and defined $data->[0]
!ll&&!rl&&r
0011
defined $data and scalar @$data != 0
!ll&&!rl&&r
0011
MC/DC0%0/3
defined $data and scalar @$data != 0 and defined $data->[0]
defined $data scalar @$data != 0 defined $data->[0]
Truth table25%1/4
A and B and C
A= defined $dataB= scalar @$data != 0C= defined $data->[0]
ABCresult
0XX0
10X0
1100
1111
326 ? $data->[0]
327 : 0;
328 }
329  
330 11 ▶ carp "Found $element_count elements, leaving count()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 11 11
331  
332 11 return $element_count;
333 }
334  
335  
336 =head2 enqueue()
337  
338 Adds a new element at the end of the current queue.
339  
340 my $queue_element_id = $queue->enqueue( $data );
341  
342 The data passed can be a scalar or a reference to a complex data
343 structure. There is no limitation on the type of data that can be stored
344 as it is serialized for storage in the database.
345  
346 =cut
347  
348 sub enqueue
349 {
350 15 ▶ my ( $self, $data ) = @_;
Subroutine enqueue: called CC=6 SCAR=18.7
Pod documented
351 15 my $verbose = $self->get_verbose();
352 15 my $dbh = $self->get_dbh();
353 15 ▶ carp "Entering enqueue()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 15 15
354 15 ▶ carp "Data is: " . Dumper( $data ) if $verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose > 1 0 15 15
355  
356 15 my $encoded_data = $self->freeze( $data );
357 15 ▶ croak 'The size of the data to store exceeds the maximum internal storage size available.'
Branch: 1 branch
BranchTrueFalseTotal
if length $encoded_data > $Queue::DBI::MAX_VALUE_SIZE 0 15 15
358 if length( $encoded_data ) > $MAX_VALUE_SIZE;
359  
360 15 ▶ $dbh->do(
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do(sprintf("\n\t\t\t\tINSERT INTO %s( queue_id, data, created )\n\t\t\t\tVALUES ( ?, ?, ? )\n\t\t\t", $dbh->quote_identifier($self->get_queue_elements_table_name)), {}, $self->get_queue_id, $encoded_data, time) or croak('Cannot execute SQL: ' . $dbh->errstr) 0 15 15
361 sprintf(
362 q|
363 INSERT INTO %s( queue_id, data, created )
364 VALUES ( ?, ?, ? )
365 |,
366 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
367 ),
368 {},
369 $self->get_queue_id(),
370 $encoded_data,
371 time(),
372 ) || croak 'Cannot execute SQL: ' . $dbh->errstr();
373  
374 # We need to reset the internal cached value preventing infinite loops, other-
375 # wise this new element will not be taken into account by the current queue
376 # object.
377 15 $self->{'max_id'} = undef;
378  
379 15 ▶ carp "Element inserted, leaving enqueue()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 15 15
380  
381 15 return $dbh->last_insert_id(
382 undef,
383 undef,
384 $self->get_queue_elements_table_name(),
385 'queue_element_id',
386 );
387 }
388  
389  
390 =head2 next()
391  
392 Retrieves the next element from the queue and returns it in the form of a
393 Queue::DBI::Element object.
394  
395 my $queue_element = $queue->next();
396  
397 while ( my $queue_element = $queue->next() )
398 {
399 # [...]
400 }
401  
402 Additionally, for testing purposes, a list of IDs to use when trying to retrieve
403 elements can be specified using 'search_in_ids':
404  
405 my $queue_item = $queue->next( 'search_in_ids' => [ 123, 124, 125 ] );
406  
407 =cut
408  
409 sub next ## no critic (Subroutines::ProhibitBuiltinHomonyms)
410 {
411 32 ▶ my ( $self, %args ) = @_;
Subroutine next: called CC=6 SCAR=18.2
Pod documented
412 32 my $verbose = $self->get_verbose();
413 32 ▶ carp "Entering next()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 32 32
414  
415 my $elements = $self->retrieve_batch(
416 1,
417 'search_in_ids' => defined( $args{'search_in_ids'} )
418 32 ▶ ? $args{'search_in_ids'}
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'search_in_ids'} ? : 0 32 32
419 : undef,
420 );
421  
422 32 ▶ my $return = defined( $elements ) && ( scalar( @$elements ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
defined $elements && scalar @$elements != 0 ? : 25 7 32
Condition100%3/3
defined $elements and scalar @$elements != 0
!ll&&!rl&&r
2525
MC/DC100%2/2
defined $elements and scalar @$elements != 0
defined $elements scalar @$elements != 0
Truth table100%3/3
A and B
A= defined $elementsB= scalar @$elements != 0
ABresult
0X0
100
111
423 ? $elements->[0]
424 : undef;
425  
426 32 ▶ carp "Leaving next()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 32 32
427 32 return $return;
428 }
429  
430  
431 =head2 retrieve_batch()
432  
433 Retrieves a batch of elements from the queue and returns them in an arrayref.
434  
435 This method requires an integer to be passed as parameter to indicate the
436 maximum size of the batch to be retrieved.
437  
438 my $queue_elements = $queue->retrieve_batch( 500 );
439  
440 foreach ( @$queue_elements )
441 {
442 # [...]
443 }
444  
445 Additionally, for testing purposes, a list of IDs to use when trying to retrieve
446 elements can be specified using 'search_in_ids':
447  
448 my $queue_items = $queue->retrieve_batch(
449 10,
450 'search_in_ids' => [ 123, 124, 125 ],
451 );
452  
453 =cut
454  
455 sub retrieve_batch
456 {
457 32 ▶ my ( $self, $number_of_elements_to_retrieve, %args ) = @_;
Subroutine retrieve_batch: called CC=23 SCAR=33.5
Pod documented
458 32 my $verbose = $self->get_verbose();
459 32 my $dbh = $self->get_dbh();
460 32 ▶ carp "Entering retrieve_batch()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 32 32
461  
462 # Check parameters
463 32 ▶ croak 'The number of elements to retrieve from the queue is not properly formatted'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number_of_elements_to_retrieve and $number_of_elements_to_retrieve =~ /^\d+$/ 0 32 32
Condition33%1/3
defined $number_of_elements_to_retrieve and $number_of_elements_to_retrieve =~ /^\d+$/
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $number_of_elements_to_retrieve and $number_of_elements_to_retrieve =~ /^\d+$/
defined $number_of_elements_to_retrieve $number_of_elements_to_retrieve =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $number_of_elements_to_retrieveB= $number_of_elements_to_retrieve =~ /^\d+$/
ABresult
0X0
100
111
464 unless defined( $number_of_elements_to_retrieve ) && ( $number_of_elements_to_retrieve =~ m/^\d+$/ );
465  
466 # Prevent infinite loops
467 32 ▶ unless ( defined( $self->{'max_id'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"max_id"}) 21 11 32
468 {
469 21 my $max_id;
470 {
471 21 local $dbh->{'RaiseError'} = 1;
472 21 my $data = $dbh->selectrow_arrayref(
473 sprintf(
474 q|
475 SELECT MAX(queue_element_id)
476 FROM %s
477 WHERE queue_id = ?
478 |,
479 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
480 ),
481 {},
482 $self->get_queue_id(),
483 );
484  
485 21 ▶ $max_id = defined( $data ) && scalar( @$data ) != 0
Branch: 1 branch
BranchTrueFalseTotal
defined $data && scalar @$data != 0 ? : 21 0 21
Condition33%1/3
defined $data and scalar @$data != 0
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $data and scalar @$data != 0
defined $data scalar @$data != 0
Truth table33%1/3
A and B
A= defined $dataB= scalar @$data != 0
ABresult
0X0
100
111
486 ? $data->[0]
487 : undef;
488 }
489  
490 21 ▶ if ( defined( $max_id ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_id) { } 19 2 21
491 {
492 19 $self->{'max_id'} = $max_id;
493 }
494 else
495 {
496 # Empty queue
497 2 ▶ carp "Detected empty queue, leaving." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
498 2 return;
499 }
500 }
501  
502 # Prevent backtracking in case elements are requeued
503 $self->{'last_id'} = -1
504 30 ▶ unless defined( $self->{'last_id'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"last_id"} 19 11 30
505  
506 # Detect end of queue quicker
507 30 ▶ if ( $self->{'last_id'} == $self->{'max_id'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_id"} == $self->{"max_id"}) 3 27 30
508 {
509 3 ▶ carp "Finished processing queue, leaving." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3 3
510 3 return [];
511 }
512  
513 # Make sure we don't use requeued elements more times than specified.
514 27 my $max_requeue_count = $self->get_max_requeue_count();
515 27 ▶ my $sql_max_requeue_count = defined( $max_requeue_count )
Branch: 1 branch
BranchTrueFalseTotal
defined $max_requeue_count ? : 12 15 27
516 ? 'AND requeue_count <= ' . $dbh->quote( $max_requeue_count )
517 : '';
518  
519 # Make sure we don't use elements that exceed the specified lifetime.
520 27 my $lifetime = $self->get_lifetime();
521 27 ▶ my $sql_lifetime = defined( $lifetime )
Branch: 1 branch
BranchTrueFalseTotal
defined $lifetime ? : 3 24 27
522 ? 'AND created >= ' . ( time() - $lifetime )
523 : '';
524  
525 # If specified, retrieve only those IDs.
526 my $ids = defined( $args{'search_in_ids'} )
527 27 ▶ ? 'AND queue_element_id IN (' . join( ',', map { $dbh->quote( $_ ) } @{ $args{'search_in_ids' } } ) . ')'
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'search_in_ids'} ? : 0 27 27
528 : '';
529  
530 # Retrieve the first available elements from the queue.
531 27 ▶ carp "Retrieving data." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 27 27
532 27 ▶ carp "Parameters:\n\tLast ID: $self->{'last_id'}\n\tMax ID: $self->{'max_id'}\n" if $verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose > 1 0 27 27
533 my $data = $dbh->selectall_arrayref(
534 sprintf(
535 q|
536 SELECT queue_element_id, data, requeue_count, created
537 FROM %s
538 WHERE queue_id = ?
539 AND lock_time IS NULL
540 AND queue_element_id >= ?
541 AND queue_element_id <= ?
542 %s
543 %s
544 %s
545 ORDER BY queue_element_id ASC
546 LIMIT ?
547 |,
548 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
549 $ids,
550 $sql_max_requeue_count,
551 $sql_lifetime,
552 ),
553 {},
554 $self->get_queue_id(),
555 $self->{'last_id'} + 1,
556 27 $self->{'max_id'},
557 $number_of_elements_to_retrieve,
558 );
559 27 ▶ croak 'Cannot execute SQL: ' . $dbh->errstr() if defined( $dbh->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh->errstr 0 27 27
560  
561 # All the remaining elements are locked
562 27 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data or scalar @$data == 0 2 25 27
Condition66%2/3
not defined $data or scalar @$data == 0
l!l&&r!l&&!r
0225
MC/DC50%1/2
not defined $data or scalar @$data == 0
not defined $data scalar @$data == 0
Truth table66%2/3
not(A) or B
A= not defined $dataB= scalar @$data == 0
ABresult
1X1
011
000
563 if !defined( $data ) || ( scalar( @$data) == 0 );
564  
565 # Create objects
566 25 ▶ carp "Creating new Queue::DBI::Element objects." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 25 25
567 25 my @return = ();
568 25 foreach my $row ( @$data )
569 {
570 25 push(
571 @return,
572 Queue::DBI::Element->new(
573 'queue' => $self,
574 'data' => $self->thaw( $row->[1] ),
575 'id' => $row->[0],
576 'requeue_count' => $row->[2],
577 'created' => $row->[3],
578 )
579 );
580 }
581  
582 # Prevent backtracking in case elements are requeued
583 25 $self->{'last_id'} = $return[-1]->id();
584  
585 25 ▶ carp "Leaving retrieve_batch()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 25 25
586 25 return \@return;
587 }
588  
589  
590 =head2 get_element_by_id()
591  
592 Retrieves a queue element using a queue element ID, ignoring any lock placed on
593 that element.
594  
595 This method is mostly useful when doing a lock on an element and then calling
596 success/requeue asynchroneously.
597  
598 This method requires a queue element ID to be passed as parameter.
599  
600 my $queue_element = $queue->get_element_by_id( 123456 );
601  
602 =cut
603  
604 sub get_element_by_id
605 {
606 2 ▶ my ( $self, $queue_element_id ) = @_;
Subroutine get_element_by_id: called CC=8 SCAR=22.1
Pod documented
607 2 my $verbose = $self->get_verbose();
608 2 my $dbh = $self->get_dbh();
609 2 ▶ carp "Entering get_element_by_id()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
610  
611 # Check parameters.
612 2 ▶ croak 'A queue element ID is required by this method'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queue_element_id 0 2 2
613 unless defined( $queue_element_id );
614  
615 # Retrieve the specified element from the queue.
616 2 ▶ carp "Retrieving data." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
617 2 my $data = $dbh->selectrow_hashref(
618 sprintf(
619 q|
620 SELECT *
621 FROM %s
622 WHERE queue_id = ?
623 AND queue_element_id = ?
624 |,
625 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
626 ),
627 {},
628 $self->get_queue_id(),
629 $queue_element_id,
630 );
631 2 ▶ croak 'Cannot execute SQL: ' . $dbh->errstr() if defined( $dbh->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh->errstr 0 2 2
632  
633 # Queue element ID doesn't exist or belongs to another queue.
634 2 ▶ return unless defined( $data );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 2 2
635  
636 # Create the Queue::DBI::Element object.
637 2 ▶ carp "Creating a new Queue::DBI::Element object." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
638  
639 my $queue_element = Queue::DBI::Element->new(
640 'queue' => $self,
641 'data' => $self->thaw( $data->{'data'} ),
642 'id' => $data->{'queue_element_id'},
643 'requeue_count' => $data->{'requeue_count'},
644 2 'created' => $data->{'created'},
645 );
646  
647 2 ▶ carp "Leaving get_element_by_id()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
648 2 return $queue_element;
649 }
650  
651  
652 =head2 cleanup()
653  
654 Requeue items that have been locked for more than the time in seconds specified
655 as parameter.
656  
657 Returns the items requeued so that a specific action can be taken on them.
658  
659 my $elements = $queue->cleanup( $time_in_seconds );
660 foreach my $element ( @$elements )
661 {
662 # $element is a Queue::DBI::Element object
663 }
664  
665 =cut
666  
667 sub cleanup
668 {
669 30 ▶ my ( $self, $time_in_seconds ) = @_;
Subroutine cleanup: called CC=12 SCAR=26.8
Pod documented
670 30 my $verbose = $self->get_verbose();
671 30 my $dbh = $self->get_dbh();
672 30 ▶ carp "Entering cleanup()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
673  
674 30 ▶ $time_in_seconds ||= '';
Condition50%1/2
$time_in_seconds ||= ""
l!l
300
MC/DC0%0/1
$time_in_seconds ||= ""
$time_in_seconds
Truth table50%1/2
A
A= $time_in_seconds
Aresult
11
00
675 30 ▶ croak 'Time in seconds is not correctly formatted'
Branch: 1 branch
BranchTrueFalseTotal
unless $time_in_seconds =~ /^\d+$/ 0 30 30
676 unless $time_in_seconds =~ m/^\d+$/;
677  
678 # Find all the orphans
679 30 ▶ carp "Retrieving data." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
680 30 my $rows = $dbh->selectall_arrayref(
681 sprintf(
682 q|
683 SELECT queue_element_id, data, requeue_count, created
684 FROM %s
685 WHERE queue_id = ?
686 AND lock_time < ?
687 |,
688 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
689 ),
690 {},
691 $self->get_queue_id(),
692 time() - $time_in_seconds,
693 );
694 30 ▶ croak 'Cannot execute SQL: ' . $dbh->errstr() if defined( $dbh->errstr() );
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh->errstr 0 30 30
695 30 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows 0 30 30
696 unless defined( $rows );
697  
698 # Create objects and requeue them
699 30 ▶ carp "Creating new Queue::DBI::Element objects." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
700 30 my $queue_elements = [];
701 30 foreach my $row ( @$rows )
702 {
703 1 my $queue_element = Queue::DBI::Element->new(
704 'queue' => $self,
705 'data' => $self->thaw( $row->[1] ),
706 'id' => $row->[0],
707 'requeue_count' => $row->[2],
708 'created' => $row->[3],
709 );
710 # If this item was requeued by another process since its
711 # being SELECTed a moment ago, requeue() will return failure
712 # and this process will ignore it.
713 1 ▶ push( @$queue_elements, $queue_element )
Branch: 1 branch
BranchTrueFalseTotal
if $queue_element->requeue 1 0 1
714 if $queue_element->requeue();
715 }
716 30 ▶ carp "Found " . scalar( @$queue_elements ) . " orphaned element(s)." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
717  
718 30 ▶ carp "Leaving cleanup()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 30 30
719 30 return $queue_elements;
720 }
721  
722  
723 =head2 purge()
724  
725 Remove (permanently, caveat emptor!) queue elements based on how many times
726 they've been requeued or how old they are, and return the number of elements
727 deleted.
728  
729 # Remove permanently elements that have been requeued more than 10 times.
730 my $deleted_elements_count = $queue->purge( max_requeue_count => 10 );
731  
732 # Remove permanently elements that were created over an hour ago.
733 my $deleted_elements_count = $queue->purge( lifetime => 3600 );
734  
735 Important: locked elements are not purged even if they match the criteria, as
736 they are presumed to be currently in process and purging them would create
737 unexpected failures in the application processing them.
738  
739 Also note that I<max_requeue_count> and I<lifetime> cannot be combined.
740  
741 =cut
742  
743 sub purge
744 {
745 7 ▶ my ( $self, %args ) = @_;
Subroutine purge: called CC=15 SCAR=27.8
Pod documented
746 7 my $verbose = $self->get_verbose();
747 7 my $dbh = $self->get_dbh();
748 7 ▶ carp "Entering cleanup()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 7 7
749  
750 7 my $max_requeue_count = $args{'max_requeue_count'};
751 7 my $lifetime = $args{'lifetime'};
752  
753 # Check parameters.
754 7 ▶ croak '"max_requeue_count" must be an integer'
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/ 0 7 7
Condition66%2/3
defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/
!ll&&!rl&&r
340
MC/DC0%0/2
defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/
defined $max_requeue_count not $max_requeue_count =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $max_requeue_countB= not $max_requeue_count =~ /^\d+$/
ABresult
0X0
100
111
755 if defined( $max_requeue_count ) && ( $max_requeue_count !~ m/^\d+$/ );
756 7 ▶ croak '"lifetime" must be an integer representing seconds'
Branch: 1 branch
BranchTrueFalseTotal
if defined $lifetime and not $lifetime =~ /^\d+$/ 0 7 7
Condition66%2/3
defined $lifetime and not $lifetime =~ /^\d+$/
!ll&&!rl&&r
430
MC/DC0%0/2
defined $lifetime and not $lifetime =~ /^\d+$/
defined $lifetime not $lifetime =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $lifetimeB= not $lifetime =~ /^\d+$/
ABresult
0X0
100
111
757 if defined( $lifetime ) && ( $lifetime !~ m/^\d+$/ );
758 7 ▶ croak '"max_requeue_count" and "lifetime" cannot be combined, specify one OR the other'
Branch: 1 branch
BranchTrueFalseTotal
if defined $lifetime and defined $max_requeue_count 1 6 7
Condition100%3/3
defined $lifetime and defined $max_requeue_count
!ll&&!rl&&r
421
MC/DC100%2/2
defined $lifetime and defined $max_requeue_count
defined $lifetime defined $max_requeue_count
Truth table100%3/3
A and B
A= defined $lifetimeB= defined $max_requeue_count
ABresult
0X0
100
111
759 if defined( $lifetime ) && defined( $max_requeue_count );
760 6 ▶ croak '"max_requeue_count" or "lifetime" must be specified'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lifetime or defined $max_requeue_count 1 5 6
Condition100%3/3
defined $lifetime or defined $max_requeue_count
l!l&&r!l&&!r
231
MC/DC100%2/2
defined $lifetime or defined $max_requeue_count
defined $lifetime defined $max_requeue_count
Truth table100%3/3
A or B
A= defined $lifetimeB= defined $max_requeue_count
ABresult
1X1
011
000
761 if !defined( $lifetime ) && !defined( $max_requeue_count );
762  
763 # Prepare query clauses.
764 5 ▶ my $sql_lifetime = defined( $lifetime )
Branch: 1 branch
BranchTrueFalseTotal
defined $lifetime ? : 2 3 5
765 ? 'AND created < ' . ( time() - $lifetime )
766 : '';
767 5 ▶ my $sql_max_requeue_count = defined( $max_requeue_count )
Branch: 1 branch
BranchTrueFalseTotal
defined $max_requeue_count ? : 3 2 5
768 ? 'AND requeue_count > ' . $dbh->quote( $max_requeue_count )
769 : '';
770  
771 # Purge the queue.
772 5 ▶ my $rows_deleted = $dbh->do(
Condition33%1/3
$dbh->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t\t\t%s\n\t\t\t\t\t%s\n\t\t\t", $dbh->quote_identifier($self->get_queue_elements_table_name), $sql_lifetime, $sql_max_requeue_count), {}, $self->get_queue_id) || croak('Cannot execute SQL: ' . $dbh->errstr)
l!l&&r!l&&!r
500
MC/DC0%0/2
$dbh->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t\t\t%s\n\t\t\t\t\t%s\n\t\t\t", $dbh->quote_identifier($self->get_queue_elements_table_name), $sql_lifetime, $sql_max_requeue_count), {}, $self->get_queue_id) || croak('Cannot execute SQL: ' . $dbh->errstr)
$dbh->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t\t\t%s\n\t\t\t\t\t%s\n\t\t\t", $dbh->quote_identifier($self->get_queue_elements_table_name), $sql_lifetime, $sql_max_requeue_count), {}, $self->get_queue_id) croak('Cannot execute SQL: ' . $dbh->errstr)
Truth table33%1/3
A || B
A= $dbh->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t\t\tAND lock_time IS NULL\n\t\t\t\t\t%s\n\t\t\t\t\t%s\n\t\t\t", $dbh->quote_identifier($self->get_queue_elements_table_name), $sql_lifetime, $sql_max_requeue_count), {}, $self->get_queue_id)B= croak('Cannot execute SQL: ' . $dbh->errstr)
ABresult
1X1
011
000
773 sprintf(
774 q|
775 DELETE
776 FROM %s
777 WHERE queue_id = ?
778 AND lock_time IS NULL
779 %s
780 %s
781 |,
782 $dbh->quote_identifier( $self->get_queue_elements_table_name() ),
783 $sql_lifetime,
784 $sql_max_requeue_count,
785 ),
786 {},
787 $self->get_queue_id(),
788 ) || croak 'Cannot execute SQL: ' . $dbh->errstr();
789  
790 5 ▶ carp "Leaving cleanup()." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
791 # Account for '0E0' which means no rows affected, and translates into no
792 # rows deleted in our case.
793 5 ▶ return $rows_deleted eq '0E0'
Branch: 1 branch
BranchTrueFalseTotal
$rows_deleted eq '0E0' ? : 3 2 5
794 ? 0
795 : $rows_deleted;
796 }
797  
798  
799 =head1 ACCESSORS
800  
801 =head2 get_max_requeue_count()
802  
803 Return how many times an element can be requeued before it is ignored when
804 retrieving elements.
805  
806 my $max_requeue_count = $queue->get_max_requeue_count();
807  
808 =cut
809  
810 sub get_max_requeue_count
811 {
812 28 ▶ my ( $self ) = @_;
Subroutine get_max_requeue_count: called CC=1 SCAR=0.0
Pod documented
813  
814 28 return $self->{'max_requeue_count'};
815 }
816  
817  
818 =head2 set_max_requeue_count()
819  
820 Set the number of time an element can be requeued before it is ignored when
821 retrieving elements. Set it to C<undef> to disable the limit.
822  
823 # Don't keep pulling the element if it has been requeued more than 5 times.
824 $queue->set_max_requeue_count( 5 );+
825  
826 # Retry without limit.
827 $queue->set_max_requeue_count( undef );
828  
829 =cut
830  
831 sub set_max_requeue_count
832 {
833 36 ▶ my ( $self, $max_requeue_count ) = @_;
Subroutine set_max_requeue_count: called CC=3 SCAR=11.3
Pod documented
834  
835 36 ▶ croak 'max_requeue_count must be an integer or undef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/ 0 36 36
Condition66%2/3
defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/
!ll&&!rl&&r
26100
MC/DC0%0/2
defined $max_requeue_count and not $max_requeue_count =~ /^\d+$/
defined $max_requeue_count not $max_requeue_count =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $max_requeue_countB= not $max_requeue_count =~ /^\d+$/
ABresult
0X0
100
111
836 if defined( $max_requeue_count ) && ( $max_requeue_count !~ /^\d+$/ );
837  
838 36 $self->{'max_requeue_count'} = $max_requeue_count;
839  
840 36 return;
841 }
842  
843  
844 =head2 get_lifetime()
845  
846 Return how old an element can be before it is ignored when retrieving elements.
847  
848 # Find how old an element can be before the queue will stop retrieving it.
849 my $lifetime = $queue->get_lifetime();
850  
851 =cut
852  
853 sub get_lifetime
854 {
855 33 ▶ my ( $self ) = @_;
Subroutine get_lifetime: called CC=1 SCAR=0.0
Pod documented
856  
857 33 return $self->{'lifetime'};
858 }
859  
860  
861 =head2 set_lifetime()
862  
863 Set how old an element can be before it is ignored when retrieving elements.
864  
865 Set it to C<undef> to reset Queue::DBI back to its default behavior of
866 retrieving elements without time limit.
867  
868 # Don't pull queue elements that are more than an hour old.
869 $queue->set_lifetime( 3600 );
870  
871 # Pull elements without time limit.
872 $queue->set_lifetime( undef );
873  
874 =cut
875  
876 sub set_lifetime
877 {
878 38 ▶ my ( $self, $lifetime ) = @_;
Subroutine set_lifetime: called CC=3 SCAR=11.3
Pod documented
879  
880 38 ▶ croak 'lifetime must be an integer or undef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $lifetime and not $lifetime =~ /^\d+$/ 0 38 38
Condition66%2/3
defined $lifetime and not $lifetime =~ /^\d+$/
!ll&&!rl&&r
3260
MC/DC0%0/2
defined $lifetime and not $lifetime =~ /^\d+$/
defined $lifetime not $lifetime =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $lifetimeB= not $lifetime =~ /^\d+$/
ABresult
0X0
100
111
881 if defined( $lifetime ) && ( $lifetime !~ /^\d+$/ );
882  
883 38 $self->{'lifetime'} = $lifetime;
884  
885 38 return;
886 }
887  
888  
889 =head2 get_verbose()
890  
891 Return the verbosity level, which is used in the module to determine when and
892 what type of debugging statements / information should be warned out.
893  
894 See C<set_verbose()> for the possible values this function can return.
895  
896 warn 'Verbose' if $queue->get_verbose();
897  
898 warn 'Very verbose' if $queue->get_verbose() > 1;
899  
900 =cut
901  
902 sub get_verbose
903 {
904 168 ▶ my ( $self ) = @_;
Subroutine get_verbose: called CC=1 SCAR=0.0
Pod documented
905  
906 168 return $self->{'verbose'};
907 }
908  
909  
910 =head2 set_verbose()
911  
912 Control the verbosity of the warnings in the code:
913  
914 =over 4
915  
916 =item * 0 will not display any warning;
917  
918 =item * 1 will only give one line warnings about the current operation;
919  
920 =item * 2 will also usually output the SQL queries performed.
921  
922 =back
923  
924 $queue->set_verbose(1); # turn on verbose information
925  
926 $queue->set_verbose(2); # be extra verbose
927  
928 $queue->set_verbose(0); # quiet now!
929  
930 =cut
931  
932 sub set_verbose
933 {
934 36 ▶ my ( $self, $verbose ) = @_;
Subroutine set_verbose: called CC=2 SCAR=7.1
Pod documented
935  
936 36 ▶ $self->{'verbose'} = ( $verbose || 0 );
Condition50%1/2
$verbose || 0
l!l
036
MC/DC0%0/1
$verbose || 0
$verbose
Truth table50%1/2
A
A= $verbose
Aresult
11
00
937  
938 36 return;
939 }
940  
941  
942 =head1 INTERNAL METHODS
943  
944 =head2 freeze()
945  
946 Serialize an element to store it in a SQL "text" column.
947  
948 my $frozen_data = $queue->freeze( $data );
949  
950 =cut
951  
952 sub freeze
953 {
954 16 ▶ my ( $self, $data ) = @_;
Subroutine freeze: called CC=3 SCAR=11.1
Pod documented
955  
956 return defined( $self->{'serializer'} ) && defined( $self->{'serializer'}->{'freeze'} )
957 16 ▶ ? $self->{'serializer'}->{'freeze'}($data)
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'serializer'} && defined $self->{'serializer'}{'freeze'} ? : 2 14 16
Condition66%2/3
defined $self->{"serializer"} and defined $self->{"serializer"}{"freeze"}
!ll&&!rl&&r
0142
MC/DC50%1/2
defined $self->{"serializer"} and defined $self->{"serializer"}{"freeze"}
defined $self->{"serializer"} defined $self->{"serializer"}{"freeze"}
Truth table66%2/3
A and B
A= defined $self->{"serializer"}B= defined $self->{"serializer"}{"freeze"}
ABresult
0X0
100
111
958 : MIME::Base64::encode_base64( Storable::freeze( $data ) );
959 }
960  
961 =head2 thaw()
962  
963 Deserialize an element which was stored a SQL "text" column.
964  
965 my $thawed_data = $queue->thaw( $frozen_data );
966  
967 =cut
968  
969 sub thaw
970 {
971 29 ▶ my ( $self, $data ) = @_;
Subroutine thaw: called CC=3 SCAR=11.1
Pod documented
972  
973 return defined( $self->{'serializer'} ) && defined( $self->{'serializer'}->{'thaw'} )
974 29 ▶ ? $self->{'serializer'}->{'thaw'}($data)
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'serializer'} && defined $self->{'serializer'}{'thaw'} ? : 2 27 29
Condition66%2/3
defined $self->{"serializer"} and defined $self->{"serializer"}{"thaw"}
!ll&&!rl&&r
0272
MC/DC50%1/2
defined $self->{"serializer"} and defined $self->{"serializer"}{"thaw"}
defined $self->{"serializer"} defined $self->{"serializer"}{"thaw"}
Truth table66%2/3
A and B
A= defined $self->{"serializer"}B= defined $self->{"serializer"}{"thaw"}
ABresult
0X0
100
111
975 : Storable::thaw( MIME::Base64::decode_base64( $data ) );
976 }
977  
978  
979 =head1 DEPRECATED METHODS
980  
981 =head2 create_tables()
982  
983 Please use C<create_tables()> in L<Queue::DBI::Admin> instead.
984  
985 Here is an example that shows how to refactor your call to this deprecated
986 function:
987  
988 # Load the admin module.
989 use Queue::DBI::Admin;
990  
991 # Create the object which will allow managing the queues.
992 my $queues_admin = Queue::DBI::Admin->new(
993 database_handle => $dbh,
994 );
995  
996 # Create the tables required by Queue::DBI to store the queues and data.
997 $queues_admin->create_tables(
998 drop_if_exist => $boolean,
999 );
1000  
1001 =cut
1002  
1003 sub create_tables
1004 {
1005 0 ▶ croak 'create_tables() in Queue::DBI has been deprecated, please use create_tables() in Queue::DBI::Admin instead.';
Subroutine create_tables: not called CC=1 SCAR=6.9
Pod documented
1006 }
1007  
1008  
1009 =head2 lifetime()
1010  
1011 Please use C<get_lifetime()> and C<set_lifetime()> instead.
1012  
1013 =cut
1014  
1015 sub lifetime
1016 {
1017 0 ▶ croak 'lifetime() has been deprecated, please use get_lifetime() / set_lifetime() instead.';
Subroutine lifetime: not called CC=1 SCAR=6.9
Pod documented
1018 }
1019  
1020  
1021 =head2 verbose()
1022  
1023 Please use C<get_verbose()> and C<set_verbose()> instead.
1024  
1025 =cut
1026  
1027 sub verbose
1028 {
1029 0 ▶ croak 'verbose() has been deprecated, please use get_verbose() / set_verbose() instead.';
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
1030 }
1031  
1032  
1033 =head2 max_requeue_count()
1034  
1035 Please use C<get_max_requeue_count()> and C<set_max_requeue_count()> instead.
1036  
1037 =cut
1038  
1039 sub max_requeue_count
1040 {
1041 0 ▶ croak 'max_requeue_count() has been deprecated, please use get_max_requeue_count() / set_max_requeue_count() instead.';
Subroutine max_requeue_count: not called CC=1 SCAR=6.9
Pod documented
1042 }
1043  
1044  
1045 =head1 INTERNAL METHODS
1046  
1047 =head2 get_dbh()
1048  
1049 Returns the database handle used for this queue.
1050  
1051 my $dbh = $queue->get_dbh();
1052  
1053 =cut
1054  
1055 sub get_dbh
1056 {
1057 137 ▶ my ( $self ) = @_;
Subroutine get_dbh: called CC=1 SCAR=0.0
Pod documented
1058  
1059 137 return $self->{'dbh'};
1060 }
1061  
1062  
1063 =head2 get_queues_table_name()
1064  
1065 Returns the name of the table used to store queue definitions.
1066  
1067 my $queues_table_name = $queue->get_queues_table_name();
1068  
1069 =cut
1070  
1071 sub get_queues_table_name
1072 {
1073 42 ▶ my ( $self ) = @_;
Subroutine get_queues_table_name: called CC=3 SCAR=11.1
Pod documented
1074  
1075 return defined( $self->{'table_names'}->{'queues'} ) && ( $self->{'table_names'}->{'queues'} ne '' )
1076 42 ▶ ? $self->{'table_names'}->{'queues'}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'table_names'}{'queues'} && $self->{'table_names'}{'queues'} ne '' ? : 12 30 42
Condition66%2/3
defined $self->{"table_names"}{"queues"} and $self->{"table_names"}{"queues"} ne ""
!ll&&!rl&&r
30012
MC/DC50%1/2
defined $self->{"table_names"}{"queues"} and $self->{"table_names"}{"queues"} ne ""
defined $self->{"table_names"}{"queues"} $self->{"table_names"}{"queues"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"table_names"}{"queues"}B= $self->{"table_names"}{"queues"} ne ""
ABresult
0X0
100
111
1077 : $DEFAULT_QUEUES_TABLE_NAME;
1078 }
1079  
1080  
1081 =head2 get_queue_elements_table_name()
1082  
1083 Returns the name of the table used to store queue definitions.
1084  
1085 my $queue_elements_table_name = $queue->get_queue_elements_table_name();
1086  
1087 =cut
1088  
1089 sub get_queue_elements_table_name
1090 {
1091 165 ▶ my ( $self ) = @_;
Subroutine get_queue_elements_table_name: called CC=3 SCAR=13.1
Pod documented
1092  
1093 return defined( $self->{'table_names'}->{'queue_elements'} ) && ( $self->{'table_names'}->{'queue_elements'} ne '' )
1094 165 ▶ ? $self->{'table_names'}->{'queue_elements'}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'table_names'}{'queue_elements'} && $self->{'table_names'}{'queue_elements'} ne '' ? : 0 165 165
Condition33%1/3
defined $self->{"table_names"}{"queue_elements"} and $self->{"table_names"}{"queue_elements"} ne ""
!ll&&!rl&&r
16500
MC/DC0%0/2
defined $self->{"table_names"}{"queue_elements"} and $self->{"table_names"}{"queue_elements"} ne ""
defined $self->{"table_names"}{"queue_elements"} $self->{"table_names"}{"queue_elements"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"table_names"}{"queue_elements"}B= $self->{"table_names"}{"queue_elements"} ne ""
ABresult
0X0
100
111
1095 : $DEFAULT_QUEUE_ELEMENTS_TABLE_NAME;
1096 }
1097  
1098  
1099 =head1 BUGS
1100  
1101 Please report any bugs or feature requests through the web interface at
1102 L<https://github.com/guillaumeaubert/Queue-DBI/issues/new>.
1103 I will be notified, and then you'll automatically be notified of progress on
1104 your bug as I make changes.
1105  
1106  
1107 =head1 SUPPORT
1108  
1109 You can find documentation for this module with the perldoc command.
1110  
1111 perldoc Queue::DBI
1112  
1113  
1114 You can also look for information at:
1115  
1116 =over 4
1117  
1118 =item * GitHub's request tracker
1119  
1120 L<https://github.com/guillaumeaubert/Queue-DBI/issues>
1121  
1122 =item * AnnoCPAN: Annotated CPAN documentation
1123  
1124 L<http://annocpan.org/dist/Queue-DBI>
1125  
1126 =item * CPAN Ratings
1127  
1128 L<http://cpanratings.perl.org/d/Queue-DBI>
1129  
1130 =item * MetaCPAN
1131  
1132 L<https://metacpan.org/release/Queue-DBI>
1133  
1134 =back
1135  
1136  
1137 =head1 AUTHOR
1138  
1139 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
1140 C<< <aubertg at cpan.org> >>.
1141  
1142  
1143 =head1 ACKNOWLEDGEMENTS
1144  
1145 I originally developed this project for ThinkGeek
1146 (L<http://www.thinkgeek.com/>). Thanks for allowing me to open-source it!
1147  
1148  
1149 =head1 COPYRIGHT & LICENSE
1150  
1151 Copyright 2009-2014 Guillaume Aubert.
1152  
1153 This program is free software: you can redistribute it and/or modify it under
1154 the terms of the GNU General Public License version 3 as published by the Free
1155 Software Foundation.
1156  
1157 This program is distributed in the hope that it will be useful, but WITHOUT ANY
1158 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1159 PARTICULAR PURPOSE. See the GNU General Public License for more details.
1160  
1161 You should have received a copy of the GNU General Public License along with
1162 this program. If not, see http://www.gnu.org/licenses/
1163  
1164 =cut
1165  
1166 38 1;
↑ Summary blib/lib/Queue/DBI/Admin.pm »