blib/lib/IPC/Concurrency/DBI.pm

filter: Statementstmt 97.0% 66 / 68 Branchbran 63.3% 19 / 30 Conditioncond 56.2% 9 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 93.3% 14 / 15 Podpod 100% 9 / 9 totaltotal 81.0% 120 / 148 SCAR 32.0
CC
22
Cov
82%
create_tables
11.3
register_application
10.6
new
4.0
CRAP 24.6
↑ Summary blib/lib/IPC/Concurrency/DBI/Application.pm »
1 package IPC::Concurrency::DBI;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use IPC::Concurrency::DBI::Application;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 =head1 NAME
14  
15 IPC::Concurrency::DBI - Control how many instances of an application run in parallel, using DBI as the IPC method.
16  
17  
18 =head1 VERSION
19  
20 Version 1.1.3
21  
22 =cut
23  
24 11 our $VERSION = '1.1.3';
25  
26  
27 =head1 SYNOPSIS
28  
29 This module controls how many instances of a given program are allowed to run
30 in parallel. It does not manage forking or starting those instances.
31  
32 You can use this module for example to prevent more than one instance of a
33 program from running at any given time, or to never have more than N instances
34 running in parallel to prevent exhausting all the available resources.
35  
36 It uses DBI as a storage layer for information about instances and applications,
37 which is particularly useful in contexts where Sarbanes-Oxley regulations allow
38 you database access but not file write rights in production environments.
39  
40 # Configure the concurrency object.
41 use IPC::Concurrency::DBI;
42 my $concurrency_manager = IPC::Concurrency::DBI->new(
43 'database_handle' => $dbh,
44 'verbose' => 1,
45 );
46  
47 # Create the tables that the concurrency manager needs to store information
48 # about the applications and instances.
49 $concurrency_manager->create_tables();
50  
51 # Register cron_script.pl as an application we want to limit to 10 parallel
52 # instances. We only need to do this once, obviously.
53 $concurrency_manager->register_application(
54 name => 'cron_script.pl',
55 maximum_instances => 10,
56 );
57  
58 # Retrieve the application.
59 my $application = $concurrency_manager->get_application(
60 name => 'cron_script.pl',
61 );
62  
63 # Count how many instances are currently running.
64 my $instances_count = $application->get_instances_count();
65  
66 # NOT IMPLEMENTED YET: Get a list of what instances are currently running.
67 # my $instances = $application->get_instances_list()
68  
69 # Start a new instance of the application. If this returns undef, we've
70 # reached the limit.
71 unless ( my $instance = $application->start_instance() )
72 {
73 print "Too many instances of $0 are already running.\n";
74 exit;
75 }
76  
77 # [...] Do some work.
78  
79 # Now that the application is about to exit, flag the instance as completed.
80 # (note: this is implicit when $instance is destroyed).
81 $instance->finish();
82  
83  
84 =head1 SUPPORTED DATABASES
85  
86 This distribution currently supports:
87  
88 =over 4
89  
90 =item * SQLite
91  
92 =item * MySQL
93  
94 =item * PostgreSQL
95  
96 =back
97  
98 Please contact me if you need support for another database type, I'm always
99 glad to add extensions if you can help me with testing.
100  
101  
102 =head1 METHODS
103  
104 =head2 new()
105  
106 Create a new IPC::Concurrency::DBI object.
107  
108 my $concurrency_manager = IPC::Concurrency::DBI->new(
109 'database_handle' => $dbh,
110 'verbose' => 1,
111 );
112  
113 Arguments:
114  
115 =over 4
116  
117 =item * database handle
118  
119 Mandatory, a DBI object.
120  
121 =item * verbose
122  
123 Optional, see verbose() for options.
124  
125 =back
126  
127 =cut
128  
129 sub new
130 {
131 10 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
132 10 my $database_handle = delete( $args{'database_handle'} );
133 10 my $verbose = delete( $args{'verbose'} );
134  
135 # Check parameters.
136 10 ▶ croak "Argument 'database_handle' is required to create a new IPC::Concurrency::DBI object"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $database_handle 0 10 10
137 unless defined( $database_handle );
138 10 ▶ croak "Argument 'database_handle' is not a DBI object"
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($database_handle, "class", "DBI::db") 0 10 10
139 if !Data::Validate::Type::is_instance( $database_handle, class => 'DBI::db' );
140  
141 # Create the object.
142 10 my $self = bless(
143 {
144 'database_handle' => $database_handle,
145 'verbose' => 0,
146 },
147 $class,
148 );
149  
150 10 ▶ $self->set_verbose( $verbose )
Branch: 1 branch
BranchTrueFalseTotal
if defined $verbose 8 2 10
151 if defined( $verbose );
152  
153 10 return $self;
154 }
155  
156  
157 =head2 register_application()
158  
159 Register a new application with the concurrency manager and define the maximum
160 number of instances that should be allowed to run in parallel.
161  
162 $concurrency_manager->register_application(
163 name => 'cron_script.pl',
164 maximum_instances => 10,
165 );
166  
167 'name' is a unique name for the application. It can be the name of the script
168 for a cron script, for example.
169  
170 'maximum_instances' is the maximum number of instances that should be allowed to
171 run in parallel.
172  
173 =cut
174  
175 sub register_application
176 {
177 8 ▶ my ( $self, %args ) = @_;
Subroutine register_application: called CC=10 SCAR=23.6
Pod documented
178 8 my $name = delete( $args{'name'} );
179 8 my $maximum_instances = delete( $args{'maximum_instances'} );
180  
181 # Check parameters.
182 8 ▶ croak 'The name of the application must be defined'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 1 7 8
Condition66%2/3
not defined $name or $name eq ""
l!l&&r!l&&!r
107
MC/DC50%1/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table66%2/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
183 if !defined( $name ) || ( $name eq '' );
184 7 ▶ croak 'The application name is longer than 255 characters'
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 255 1 6 7
185 if length( $name ) > 255;
186 6 ▶ croak 'The maximum number of instances must be defined'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $maximum_instances or $maximum_instances eq "" 1 5 6
Condition66%2/3
not defined $maximum_instances or $maximum_instances eq ""
l!l&&r!l&&!r
105
MC/DC50%1/2
not defined $maximum_instances or $maximum_instances eq ""
not defined $maximum_instances $maximum_instances eq ""
Truth table66%2/3
not(A) or B
A= not defined $maximum_instancesB= $maximum_instances eq ""
ABresult
1X1
011
000
187 if !defined( $maximum_instances ) || ( $maximum_instances eq '' );
188 5 ▶ croak 'The maximum number of instances must be a strictly positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_number($maximum_instances, "strictly_positive", 1) 1 4 5
189 if !Data::Validate::Type::is_number( $maximum_instances, strictly_positive => 1 );
190  
191 # Insert the new application.
192 4 my $database_handle = $self->get_database_handle();
193 4 my $time = time();
194 4 my $rows_affected = $database_handle->do(
195 q|
196 INSERT INTO ipc_concurrency_applications( name, current_instances, maximum_instances, created, modified )
197 VALUES( ?, 0, ?, ?, ? )
198 |,
199 {},
200 $name,
201 $maximum_instances,
202 $time,
203 $time,
204 );
205 3 ▶ croak 'Cannot execute SQL: ' . $database_handle->errstr()
Branch: 1 branch
BranchTrueFalseTotal
if defined $database_handle->errstr 0 3 3
206 if defined( $database_handle->errstr() );
207  
208 3 ▶ return defined( $rows_affected ) && $rows_affected == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $rows_affected && $rows_affected == 1 ? : 3 0 3
Condition33%1/3
defined $rows_affected and $rows_affected == 1
!ll&&!rl&&r
003
MC/DC0%0/2
defined $rows_affected and $rows_affected == 1
defined $rows_affected $rows_affected == 1
Truth table33%1/3
A and B
A= defined $rows_affectedB= $rows_affected == 1
ABresult
0X0
100
111
209 }
210  
211  
212 =head2 get_application()
213  
214 Retrieve an application by name or by application ID.
215  
216 # Retrieve the application by name.
217 my $application = $concurrency_manager->get_application(
218 name => 'cron_script.pl',
219 );
220 die 'Application not found'
221 unless defined( $application );
222  
223 # Retrieve the application by ID.
224 my $application = $concurrency_manager->get_application(
225 id => 12345,
226 );
227 die 'Application not found'
228 unless defined( $application );
229  
230 =cut
231  
232 sub get_application
233 {
234 5 ▶ my ( $self, %args ) = @_;
Subroutine get_application: called CC=1 SCAR=0.0
Pod documented
235 5 my $name = delete( $args{'name'} );
236 5 my $application_id = delete( $args{'id'} );
237 5 my $database_handle = $self->get_database_handle();
238  
239 5 return IPC::Concurrency::DBI::Application->new(
240 name => $name,
241 id => $application_id,
242 database_handle => $database_handle,
243 );
244 }
245  
246  
247 =head2 create_tables()
248  
249 Create the tables that the concurrency manager needs to store information about
250 the applications and instances.
251  
252 $concurrency_manager->create_tables(
253 drop_if_exist => $boolean, #default 0
254 );
255  
256 By default, it won't drop any table but you can force that by setting
257 'drop_if_exist' to 1.
258  
259 =cut
260  
261 sub create_tables
262 {
263 1 ▶ my ( $self, %args ) = @_;
Subroutine create_tables: called CC=8 SCAR=24.2
Pod documented
264 1 my $drop_if_exist = delete( $args{'drop_if_exist'} );
265 1 my $database_handle = $self->get_database_handle();
266  
267 # Defaults.
268 1 ▶ $drop_if_exist = 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $drop_if_exist and $drop_if_exist 1 0 1
Condition33%1/3
defined $drop_if_exist and $drop_if_exist
!ll&&!rl&&r
100
MC/DC0%0/2
defined $drop_if_exist and $drop_if_exist
defined $drop_if_exist $drop_if_exist
Truth table33%1/3
A and B
A= defined $drop_if_existB= $drop_if_exist
ABresult
0X0
100
111
269 if !defined( $drop_if_exist ) || !$drop_if_exist;
270  
271 # Check the database type.
272 1 my $database_type = $self->get_database_type();
273 1 ▶ croak "This database type ($database_type) is not supported yet, please email the maintainer of the module for help"
Branch: 1 branch
BranchTrueFalseTotal
unless $database_type =~ /^(?:SQLite|mysql|Pg)$/x 0 1 1
274 if $database_type !~ m/^(?:SQLite|mysql|Pg)$/x;
275  
276 # Table definitions.
277 1 my $tables_sql =
278 {
279 SQLite =>
280 q|
281 CREATE TABLE ipc_concurrency_applications
282 (
283 ipc_concurrency_application_id INTEGER PRIMARY KEY AUTOINCREMENT,
284 name varchar(255) NOT NULL,
285 current_instances INTEGER NOT NULL default '0',
286 maximum_instances INTEGER NOT NULL default '0',
287 created bigint(20) NOT NULL default '0',
288 modified bigint(20) NOT NULL default '0',
289 UNIQUE (name)
290 )
291 |,
292 mysql =>
293 q|
294 CREATE TABLE ipc_concurrency_applications
295 (
296 ipc_concurrency_application_id BIGINT(20) UNSIGNED NOT NULL auto_increment,
297 name VARCHAR(255) NOT NULL,
298 current_instances INT(10) UNSIGNED NOT NULL default '0',
299 maximum_instances INT(10) UNSIGNED NOT NULL default '0',
300 created bigint(20) UNSIGNED NOT NULL default '0',
301 modified bigint(20) UNSIGNED NOT NULL default '0',
302 PRIMARY KEY (ipc_concurrency_application_id),
303 UNIQUE KEY idx_ipc_concurrency_applications_name (name)
304 )
305 ENGINE=InnoDB
306 |,
307 Pg =>
308 q|
309 CREATE TABLE ipc_concurrency_applications
310 (
311 ipc_concurrency_application_id BIGSERIAL,
312 name VARCHAR(255) NOT NULL,
313 current_instances INT NOT NULL default '0',
314 maximum_instances INT NOT NULL default '0',
315 created BIGINT NOT NULL default '0',
316 modified BIGINT NOT NULL default '0',
317 PRIMARY KEY (ipc_concurrency_application_id),
318 CONSTRAINT idx_ipc_concurrency_applications_name UNIQUE (name)
319 )
320 |,
321 };
322 croak "No table definition found for database type '$database_type'"
323 1 ▶ if !defined( $tables_sql->{ $database_type } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tables_sql->{$database_type} 0 1 1
324  
325 # Create the table that will hold the list of applications as well as
326 # a summary of the information about instances.
327 1 ▶ if ( $drop_if_exist )
Branch: 1 branch
BranchTrueFalseTotal
if ($drop_if_exist) 0 1 1
328 {
329 0 ▶ $database_handle->do( q|DROP TABLE IF EXISTS ipc_concurrency_applications| )
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do("DROP TABLE IF EXISTS ipc_concurrency_applications") or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
330 || croak 'Cannot execute SQL: ' . $database_handle->errstr();
331 }
332 $database_handle->do(
333 1 ▶ $tables_sql->{ $database_type }
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do($tables_sql->{$database_type}) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 1 1
334 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
335  
336 # TODO: create a separate table to hold information about what instances
337 # are currently running.
338  
339 1 return 1;
340 }
341  
342  
343 =head1 ACCESSORS
344  
345 =head2 get_database_handle()
346  
347 Returns the database handle used for this object.
348  
349 my $database_handle = $concurrency_manager->get_database_handle();
350  
351 =cut
352  
353 sub get_database_handle
354 {
355 13 ▶ my ( $self ) = @_;
Subroutine get_database_handle: called CC=1 SCAR=0.0
Pod documented
356  
357 13 return $self->{'database_handle'};
358 }
359  
360  
361 =head2 get_database_type()
362  
363 Return the database type corresponding to the database handle associated
364 with the L<IPC::Concurrency::DBI> object.
365  
366 my $database_type = $concurrency_manager->get_database_type();
367  
368 =cut
369  
370 sub get_database_type
371 {
372 2 ▶ my ( $self ) = @_;
Subroutine get_database_type: called CC=2 SCAR=7.1
Pod documented
373  
374 2 my $database_handle = $self->get_database_handle();
375  
376 2 ▶ return $database_handle->{'Driver'}->{'Name'} || '';
Condition50%1/2
$database_handle->{'Driver'}{'Name'} || ''
l!l
20
MC/DC0%0/1
$database_handle->{'Driver'}{'Name'} || ''
$database_handle->{'Driver'}{'Name'}
Truth table50%1/2
A
A= $database_handle->{'Driver'}{'Name'}
Aresult
11
00
377 }
378  
379  
380 =head2 get_verbose()
381  
382 Return the verbosity level, which is used in the module to determine when and
383 what type of debugging statements / information should be warned out.
384  
385 See C<set_verbose()> for the possible values this function can return.
386  
387 warn 'Verbose' if $queue->get_verbose();
388  
389 warn 'Very verbose' if $queue->get_verbose() > 1;
390  
391 =cut
392  
393 sub get_verbose
394 {
395 4 ▶ my ( $self ) = @_;
Subroutine get_verbose: called CC=1 SCAR=0.0
Pod documented
396  
397 4 return $self->{'verbose'};
398 }
399  
400  
401 =head2 set_verbose()
402  
403 Control the verbosity of the warnings in the code:
404  
405 =over 4
406  
407 =item * 0 will not display any warning;
408  
409 =item * 1 will only give one line warnings about the current operation;
410  
411 =item * 2 will also usually output the SQL queries performed.
412  
413 =back
414  
415 $queue->set_verbose(1); # turn on verbose information
416  
417 $queue->set_verbose(2); # be extra verbose
418  
419 $queue->set_verbose(0); # quiet now!
420  
421 =cut
422  
423 sub set_verbose
424 {
425 10 ▶ my ( $self, $verbose ) = @_;
Subroutine set_verbose: called CC=2 SCAR=6.9
Pod documented
426  
427 10 ▶ $self->{'verbose'} = ( $verbose || 0 );
Condition100%2/2
$verbose || 0
l!l
37
MC/DC100%1/1
$verbose || 0
$verbose
Truth table100%2/2
A
A= $verbose
Aresult
11
00
428  
429 10 return;
430 }
431  
432  
433 =head1 DEPRECATED METHODS
434  
435 =head2 verbose()
436  
437 Please use C<get_verbose()> and C<set_verbose()> instead.
438  
439 =cut
440  
441 sub verbose
442 {
443 0 ▶ croak 'verbose() has been deprecated, please use get_verbose() / set_verbose() instead.';
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
444 }
445  
446  
447 =head1 BUGS
448  
449 Please report any bugs or feature requests through the web interface at
450 L<https://github.com/guillaumeaubert/IPC-Concurrency-DBI/issues/new>.
451 I will be notified, and then you'll automatically be notified of progress on
452 your bug as I make changes.
453  
454  
455 =head1 SUPPORT
456  
457 You can find documentation for this module with the perldoc command.
458  
459 perldoc IPC::Concurrency::DBI
460  
461  
462 You can also look for information at:
463  
464 =over 4
465  
466 =item * GitHub's request tracker
467  
468 L<https://github.com/guillaumeaubert/IPC-Concurrency-DBI/issues>
469  
470 =item * AnnoCPAN: Annotated CPAN documentation
471  
472 L<http://annocpan.org/dist/IPC-Concurrency-DBI>
473  
474 =item * CPAN Ratings
475  
476 L<http://cpanratings.perl.org/d/IPC-Concurrency-DBI>
477  
478 =item * MetaCPAN
479  
480 L<https://metacpan.org/release/IPC-Concurrency-DBI>
481  
482 =back
483  
484  
485 =head1 AUTHOR
486  
487 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
488 C<< <aubertg at cpan.org> >>.
489  
490  
491 =head1 ACKNOWLEDGEMENTS
492  
493 I originally developed this project for ThinkGeek
494 (L<http://www.thinkgeek.com/>). Thanks for allowing me to open-source it!
495  
496 Thanks to Jacob Rose C<< <jacob at thinkgeek.com> >> for suggesting the idea of
497 this module and brainstorming with me about the features it should offer.
498  
499  
500 =head1 COPYRIGHT & LICENSE
501  
502 Copyright 2011-2014 Guillaume Aubert.
503  
504 This program is free software: you can redistribute it and/or modify it under
505 the terms of the GNU General Public License version 3 as published by the Free
506 Software Foundation.
507  
508 This program is distributed in the hope that it will be useful, but WITHOUT ANY
509 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
510 PARTICULAR PURPOSE. See the GNU General Public License for more details.
511  
512 You should have received a copy of the GNU General Public License along with
513 this program. If not, see http://www.gnu.org/licenses/
514  
515 =cut
516  
517 11 1;
↑ Summary blib/lib/IPC/Concurrency/DBI/Application.pm »