From 058792486c6d7e1af0bcb76eeae01ecbb0ab4a96 Mon Sep 17 00:00:00 2001 From: Daniel Lysfjord Date: Sun, 6 Jul 2025 15:23:44 +0200 Subject: [PATCH] Make db->init() more sensible, config->db_connect is the only one handling the db connection (remote db->init from config->new) --- lib/db.pm | 8 +++----- lib/parser.pm | 1 - 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/db.pm b/lib/db.pm index fc71862..ca3445e 100644 --- a/lib/db.pm +++ b/lib/db.pm @@ -17,8 +17,6 @@ sub new { sub init { my $self = shift; - my $parser = shift; ## not needed for this module as the db-connection is for all modules - $self->connect() || die "Could not connect to db"; return $self->{'config'}->get_dbh(); } @@ -31,18 +29,18 @@ sub connect { my $db = $config->get_as_single_val('config','db'); my $dbh; if($usr && $pwd && $host && $db) { + my $logstr = 'Connecting to DB...'; my $i = 0; - $self->{'config'}->{'logger'}->log('Connecting to DB'); while(1) { last if($dbh = DBI->connect("DBI:mysql:database=$db;host=$host",$usr,$pwd, { PrintError => 1, mysql_auto_reconnect=>0, AutoCommit => 1 })); sleep($i); $i++; unless ($i % 10) { - $self->{'config'}->{'logger'}->log('Cannot connect to DB, reconnect retry '.$i); + $self->{'config'}->{'logger'}->log($logstr.'timed out, retry #'.$i); } } $config->set_dbh($dbh); - #$self->{'config'}->{'logger'}->log("Sucsessfully connected to db"); FIXME add debug to config? + $self->{'config'}->{'logger'}->log($logstr.'done'); return $config->get_dbh(); } else { $self->{'config'}->{'logger'}->log("Unable to connect to db, not enough parameters"); diff --git a/lib/parser.pm b/lib/parser.pm index 46b0eb2..739fe77 100644 --- a/lib/parser.pm +++ b/lib/parser.pm @@ -20,7 +20,6 @@ sub new { $self->{'localcheck'} = My::parser::localcheck->new; $self->{'block'} = My::parser::block->new($self->{'config'}); $self->{'db'} = My::parser::db->new($self->{'config'}); - $self->{'db'}->init(); $self->{'config'}->set_db($self->{'db'}); return $self; }