
| Current Path : /var/www/web-klick.de/dsh/50_dev2017/1300__perllib/DivBasicF/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/50_dev2017/1300__perllib/DivBasicF/ProcessServer.pm |
package DivBasicF::ProcessServer;
use strict;
use CGI::Session;
use Win32::Process;
use Win32;
use DBI;
use Data::Dumper;
use Try::Tiny;
sub DELIVER { -1 }
#****************************************************************
sub startProcess {
#looks inside a process table whether there already is a process for the
#testproject. if not, it creates one.
my $self = shift;
my $testproj = shift;
my $path_to_exec = shift;
my $params = shift;
print STDERR "---------calling process for $testproj------\n";
#setup sqlite connection
my $dbh = DBI->connect(
"dbi:SQLite:dbname=../processes.db",
"",
"",
{ print STDERR Error => 1,
print STDERR Warn => 1,
RaiseError => 1,
AutoCommit => 1,
}
) or die "Couldn't connect to database: " . $DBI::errstr;
#create table if it doesn't exist
try{
$dbh->do("CREATE TABLE Processes(ProjectName TEXT PRIMARY KEY, PID INT , ProcessObject TEXT)");
print STDERR "table Processes created\n";
}catch{
print STDERR "table Processes already exits, moving on \n";
};
my $ProcessObj;
#find process id from current testproject and check if process is alive
my $create_new_proc_bool=1;
try{
my $sql_string = "SELECT * FROM Processes WHERE ProjectName='".$testproj."'";
my $sth = $dbh->prepare("SELECT * FROM Processes WHERE ProjectName='".$testproj."'");
$sth->execute();
my $row = $sth->fetch();
print STDERR Data::Dumper::Dumper($row)."\n";
my $rows = $sth->rows();
print STDERR "selection result: number of rows: $rows \n";
if ($rows==1){
print STDERR "found saved process \n";
my $pid = @$row[1];
print STDERR "fetched row, pid: $pid \n";
#check if process is alive and reload it if it is;
my $reload_result=0;
$reload_result=Win32::Process::Open($ProcessObj,$pid,0);
print STDERR "reload result: $reload_result\n";
if (!$reload_result) {
$reload_result=0;
}
if ($reload_result==0){
print STDERR "could not reload process \n";
$create_new_proc_bool=1;
}else{
$create_new_proc_bool=0;
# print STDERR "opened process with pid: $ProcessObj->GetProcessID() \n";
}
}
$sth->finish();
}catch{print STDERR "error: ".DBI->errstr."\n $!"};
#start new process if process was not alive
if ($create_new_proc_bool){
my $sth = $dbh->do("DELETE FROM Processes WHERE ProjectName='".$testproj."'");
print STDERR "deleting entry \n";
print STDERR "creating new process \n";
Win32::Process::Create($ProcessObj,
$path_to_exec,
$params,
1,NORMAL_PRIORITY_CLASS,".");
#if a new process was created: dump the process into the database with pid and project name
my $stored = Data::Dumper::Dumper($ProcessObj);
my $insert_string = "INSERT INTO Processes VALUES('".$testproj."','".($ProcessObj->GetProcessID())."','".$ProcessObj."')";
print STDERR "insert string: $insert_string \n";
try{
$dbh->do($insert_string);
} catch{print STDERR "couldn't insert in table. ".DBI->errstr."\n\n";}
}
$ProcessObj->Resume();
$dbh->disconnect();
print STDERR "end\n";
print STDERR "--------------------------------------------\n";
}
1;