diff options
-rw-r--r-- | lib/MGA/Mirrors/DB.pm | 29 |
1 files 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) = @_; |