File Coverage

lib/DBIx/Retry.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package DBIx::Retry;
2 2     2   154450 use parent qw(DBIx::Connector);
  2         1936  
  2         14  
3             # ABSTRACT: DBIx::Connector with the ability to retry the run method for a specified amount of time.
4              
5             use strict;
6             use warnings;
7             #modules
8             use Moo;
9             use Try::Tiny;
10              
11             has retry_time => (is => 'rw', required => 1);
12             has verbose => (is => 'rw', default => sub { return 1 });
13              
14             before run => sub {
15             my $self = shift;
16             my $i = 1;
17             $self->_try_connect;
18             while (! $self->connected) {
19             warn "DBIx::Retry retry $i\n" if $self->verbose;
20             sleep(1);
21             $self->_try_connect;
22             last if $i >= $self->retry_time;
23             $i++;
24             }
25             };
26              
27             sub _try_connect {
28             my $self = shift;
29             try {
30             $self->dbh; #connect to the database
31             }
32             }
33              
34             sub BUILDARGS {
35             my $self = shift;
36             my ($dsn,$user,$pass,$args) = @_;
37             return $args;
38             }
39             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
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__