sub dbconn() { my ($cnf,$thread) = @_; if ($thread && !$db_connections{$cnf}{$thread}) { my $control = "dbh".$cnf.$thread; $$control = DBI->connect("dbi:Pg:database=dirotext;host=domain.com", 'user', 'pass' ) or die $DBI::errstr; $db_connections{$cnf}{$thread} = 1; } elsif ($thread && $db_connections{$cnf}{$thread}) { $db_connections{$cnf}{$thread}++; } ############################ elsif (!$db_connections{$cnf}) { my $control = "dbh".$cnf; warn "Connecting DB $control\n"; $$control = DBI->connect("dbi:Pg:database=dirotext;host=domain.com", 'user', 'pass' ) or die $DBI::errstr; $db_connections{$cnf} = 1; } else { $db_connections{$cnf}++; } } sub dbdisconn() { my ($cnf,$thread) = @_; if ($thread && $db_connections{$cnf}{$thread} == 1) { $db_connections{$cnf}{$thread} = 0; my $control = "dbh".$cnf.$thread; $$control->disconnect; } elsif ($thread && $db_connections{$cnf}{$thread} > 1) { $db_connections{$cnf}{$thread}--; } elsif ($db_connections{$cnf} == 1) { $db_connections{$cnf} = 0; my $control = "dbh".$cnf; $$control->disconnect; } elsif ($db_connections{$cnf} > 1) { $db_connections{$cnf}--; } } sub sqldo() { my ($cnf,$sql,$thread) = @_; my $control = "dbh".$cnf.$thread; &dbconn($cnf,$thread); $$control->do($sql); &dbdisconn($cnf,$thread); } sub simple_sql() { my ($cnf,$sql,$conn,$thread) = @_; my $control = "dbh".$cnf; my $exec = "exec".$cnf; if ($thread) { $control = $control . $thread; $exec = $exec . $thread; } if (!$conn) { $conn = 1; } @result_simple = (); if ($conn) { &dbconn($cnf,$thread); } $$exec = $$control->prepare("$sql"); $$exec->execute; while ( @row_simple = $$exec->fetchrow_array()) { @result_simple = @row_simple; } $$exec->finish(); if ($conn) { &dbdisconn($cnf,$thread); } return @result_simple; } use sigtrap 'handler' => \&cleanAndExit, 'INT', 'ABRT', 'QUIT', 'TERM'; sub cleanAndExit(){ my $now = strftime "%Y-%m-%d %H:%M:%S", localtime; warn "\n----------------->[ $now ]<--------------------------\nApplication to be killed:\n"; store \%db_ids_in_progress, $mydir."daemon/cache/db_ids_in_progress"; warn "\tHash db_ids_in_progress saved to ".$mydir."daemon/cache/db_ids_in_progress\n"; store \$process_thread_running, $mydir."daemon/cache/process_thread_running"; warn "\tScallar process_thread_running saved to ".$mydir."daemon/cache/process_thread_running\n"; store \$total_counter, $mydir."daemon/cache/total_counter"; warn "\tScallar total_counter saved to ".$mydir."daemon/cache/total_counter\n"; warn "<-----------------[ $now ]-------------------------->\n"; $pid->release(); system("rm -rf /var/run/local_daemon.pid"); exit(1); }