Fix kill thread db

This commit is contained in:
cmaffio
2016-04-07 10:52:22 +02:00
parent 9bc76f2e18
commit e6e4b5d15b

View File

@@ -52,14 +52,15 @@ while ( $ref = $sts->fetchrow_hashref ) {
my $port = $$ref{'port'}; my $port = $$ref{'port'};
my $nome = $$ref{'nome'}; my $nome = $$ref{'nome'};
$proc{$nome} = threads->create(\&conntrack, $id, $ip, $port); $proc{$nome} = threads->create(\&conntrack, $id, $ip, $port);
print "Attivato processo $nome\n" if ($verbose);
} }
while (1) { while (1) {
sleep 30; sleep 30;
while (($variabile, $puntatore) = each %proc) { while (my ($variabile, $puntatore) = each %proc) {
if(!$puntatore or !$puntatore->is_running ) { if(!$puntatore or !$puntatore->is_running ) {
print "Rilevato termine processo $variabile, riavvio" if ($verbose); print "Rilevato termine processo $variabile\n" if ($verbose);
$puntatore->detach(); #$puntatore->detach();
if ($variabile eq "database") { if ($variabile eq "database") {
$proc{'database'} = threads->create(\&database); $proc{'database'} = threads->create(\&database);
@@ -68,14 +69,13 @@ while (1) {
$sts = $dbmysql->prepare($query); $sts = $dbmysql->prepare($query);
$sts->execute (); $sts->execute ();
$ref = $sts->fetchrow_hashref; $ref = $sts->fetchrow_hashref;
my $id = $$ref{'id'}; my $id = $$ref{'id'};
my $ip = $$ref{'ip'}; my $ip = $$ref{'ip'};
my $port = $$ref{'port'}; my $port = $$ref{'port'};
$proc{$$variabile} = threads->create(\&conntrack, $id, $ip, $port); $proc{$$variabile} = threads->create(\&conntrack, $id, $ip, $port);
} }
} }
} }
} }
exit; exit;
@@ -151,18 +151,30 @@ sub conntrack {
sub database { sub database {
print "Attivato thread DB\n" if ($verbose); print "Attivato thread DB\n" if ($verbose);
my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass) or die ($DBI::errstr); $dbmysql = DBConn ();
$sts = $dbmysql->prepare("use $DBname");
$sts->execute ();
while (1) { while (1) {
print "DB in attesa\n" if ($verbose > 1); print "DB in attesa\n" if ($verbose > 1);
$sem2->down(); $sem2->down();
print "Query arrivata\n" if ($verbose > 1); print "Query arrivata\n" if ($verbose > 1);
if ( not $dbmysql->ping ) {
$dbmysql = DBConn ();
}
my $sts = $dbmysql->prepare($queryDB); my $sts = $dbmysql->prepare($queryDB);
$sts->execute () || die; $sts->execute ();
$sts->finish; $sts->finish;
$sem1->up(); $sem1->up();
} }
} }
sub DBConn {
print "Connessione DB\n" if ($verbose > 1);
my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass, {PrintError => 0, RaiseError => 0, AutoCommit =>1, mysql_auto_reconnect=>1} ) or die ($DBI::errstr);
$sts = $dbmysql->prepare("use $DBname");
$sts->execute ();
return $dbmysql;
}