lib/DBIx/Retry.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 92.3% 48 / 52 CC 4 SCAR 6.6
CC
4
Cov
95%
__ANON__
4.0
__ANON__
1.1
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package DBIx::Retry;
2 2 ▶use parent qw(DBIx::Connector);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: DBIx::Connector with the ability to retry the run method for a specified amount of time.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #modules
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 has retry_time => (is => 'rw', required => 1);
12 0 ▶has verbose => (is => 'rw', default => sub { return 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
13  
14 2 ▶before run => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
15 2 my $self = shift;
16 2 my $i = 1;
17 2 $self->_try_connect;
18 2 ▶ while (! $self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->connected) 3 1 4
19 3 ▶ warn "DBIx::Retry retry $i\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 3 3
20 3 sleep(1);
21 3 $self->_try_connect;
22 3 ▶ last if $i >= $self->retry_time;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $self->retry_time 1 2 3
23 2 $i++;
24 }
25 2 };
26  
27 5 ▶sub _try_connect {
Subroutine _try_connect: called CC=1 SCAR=0.0
28 5 my $self = shift;
29 5 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 5 $self->dbh; #connect to the database
31 }
32 5 }
33  
34 3 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
35 3 my $self = shift;
36 3 my ($dsn,$user,$pass,$args) = @_;
37 3 return $args;
38 }
39 2 1;
40  
41  
42  
43  
44 =pod
45  
46 =head1 NAME
47  
48 DBIx::Retry - DBIx::Connector with the ability to retry the run method for a specified amount of time.
49  
50 =head1 VERSION
51  
52 version 0.004
53  
54 =head1 SYNOPSIS
55  
56 use DBIx::Retry;
57 my $conn = DBIx::Retry->new($dsn, $user, $tools, {retry_time => 5});
58
59 # all other method are inherited from DBIx::Connector
60 my $dbh = $conn->dbh; #get a database handle
61
62 # Do something with the handle - will retry for specified amount of time, should the database connection be lost
63 $conn->run(fixup => sub {
64 $_->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' );
65 });
66  
67 =head1 DESCRIPTION
68  
69 DBIx::Retry is extended from DBIx::Connector. It adds the ability to keep retrying to connect to a database for a specified amount of time in order to execute DBIx::Connector's run method.
70  
71 =head1 ATTRIBUTES
72  
73 =head2 retry_time
74  
75 Amount of seconds to retry re-connecting to database, should the database become unavailable.
76  
77 =head2 verbose
78  
79 Enable verbose output.
80  
81 =head1 METHODS
82  
83 =head2 new
84  
85 Create a new DBIx::Retry object.
86 my $conn = DBIx::Retry->new($dsn, $user, $tools,{timeout => 5, verbose => 1});
87  
88 =head1 USAGE
89  
90 Simply create a new DBIx::Retry object:
91  
92 my $conn = DBIx::Retry->new($dsn, $user, $tools, {retry_time => 5});
93  
94 Then wrap your operations inside the run method that is inherited from DBIx::Connector:
95  
96 $conn->run(fixup => sub {
97 $_->do('INSERT INTO foo (name) VALUES (?)', undef, 'Fred' );
98 });
99  
100 Should the connection to the database be lost then DBIx::Retry will retry to connect to the database for the amount of seconds specified in the "retry_time" attribute.
101  
102 =head1 SEE ALSO
103  
104 DBIx::Connector
105  
106 =head1 AUTHOR
107  
108 Hartmut Behrens <hartmut.behrens@gmail.com>
109  
110 =head1 COPYRIGHT AND LICENSE
111  
112 This software is copyright (c) 2012 by Hartmut Behrens.
113  
114 This is free software; you can redistribute it and/or modify it under
115 the same terms as the Perl 5 programming language system itself.
116  
117 =cut
118  
119  
120 __END__
↑ Summary