From b6a1e038f23f29127bfc97589b5663cebf4dcd76 Mon Sep 17 00:00:00 2001 From: Olivier Thauvin Date: Fri, 5 Nov 2010 18:30:58 +0000 Subject: - reconnect automatically to database --- lib/MGA/Mirrors/DB.pm | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/MGA/Mirrors/DB.pm b/lib/MGA/Mirrors/DB.pm index ac37783..da0534a 100644 --- a/lib/MGA/Mirrors/DB.pm +++ b/lib/MGA/Mirrors/DB.pm @@ -20,18 +20,7 @@ sub new { : Config::IniFiles->new(-file => configfile()) or return; - my $db = DBI->connect( - 'dbi:Pg:' . $conf->val('db', 'pgconn', ''), - $conf->val('db', 'user') || undef, - $conf->val('db', 'password') || undef, - { - AutoCommit => 0, - PrintError => 1, - } - ) or return; - bless { - db => $db, conf => $conf, }, $class; } @@ -51,7 +40,23 @@ sub host_ips { @addresses; } -sub db { $_[0]->{db} } +sub db { + my ($self) = @_; + if ($self->{db} && $self->{db}->ping) { + return $self->{db}; + } else { + my $db = DBI->connect( + 'dbi:Pg:' . $self->{conf}->val('db', 'pgconn', ''), + $self->{conf}->val('db', 'user') || undef, + $self->{conf}->val('db', 'password') || undef, + { + AutoCommit => 0, + PrintError => 1, + } + ) or return; + return $self->{db} = $db; + } +} sub locate_ips { my ($self, @ips) = @_; -- cgit v1.2.1