sub init {
my ($logger_input,$xmlFile_input,$ini_file_input,$run_all_input,$gui_sync_file_input,$dump_input) = @_;
#Setting and reading params from run_me
readParamsFromRunme($logger_input,$xmlFile_input,$ini_file_input,$run_all_input,$gui_sync_file_input,$dump_input);
#Handle Ctrl+c if not running via GUI
$SIG{INT}=\&ctrl_c_handler if (!$gui_sync_file);
##kill -10 from UI
$SIG{USR1}=\&usr_kill_handler;
}
# FUNCTION NAME: ctrl_c_handler
# WRITTEN BY: Sagi
# DATE: 1.11.2011
# PURPOSE: Exit the test when user press Ctrl+c
# IN PARAMS: None
# RETURNED VALUES: None
# Sample call:
sub ctrl_c_handler
{
#Wait for to end
$SIG{'INT'}='IGNORE';
my @procs = keys (%{fatherProcess::framework_child_procs});
kill ALRM => @procs;
kill SIGUSR1 => @procs;
foreach my $child (@procs) {
my $tmp = waitpid($child, 0);
}
$father_self->finishRun("user stoped");
}
sub usr_kill_handler {
my $signame = shift;
printer::Logger_screen_message("info","got signal $signame",0,$logger);
$father_self->finishRun("user stoped");
}
####
sub start_fork_run{
my $self = shift;
my ($xml_ojects_ref,$night_run_flag) = @_;
my @childs = ();
#Get father PID
my $father_pid=$$;
#Get father SN
my $fatherSN = $self->getSN();
if (!$fatherSN){
printer::Logger_screen_message("error","can't get father's SN from DB",0,$logger);
exit 1;
}
#Start fork
foreach my $xml_obj (@{$xml_ojects_ref}){
my $pid = fork();
if ($pid) {
# parent
push(@childs, $pid);
$framework_child_procs{$pid} = 1;
}elsif($pid == 0) {
# child
my $results = new testProcess($xml_obj,$fatherSN,$gui_sync_file,$father_pid,$night_run_flag);
exit 0;
}else {
printer::Logger_screen_message("error","couldnt fork: $!",0,$logger);
exit 1;
}
}
#Wait for to end
foreach my $child (@childs) {
my $tmp = waitpid($child, 0);
delete $framework_child_procs{$child};
printer::Logger_screen_message("info","done with pid $tmp",0,$logger);
}
#End the whole topology run
$self->finishRun("finish");
}
##
##
package testProcess;
#use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use File::Basename;
use File::Temp;
use Storable ('dclone');
use logger;
use reporter;
my $logger;
our $local_xml_obj;
my $gui_sync_file;
my $night_run;
my $packageReporter;
sub new {
my $class = shift;
my ($xml_obj,$fatherSN,$gui_sync_file_input,$father_pid,$night_run_input) = @_;
#kill -10 from UI
$SIG{'USR1'}=\&prc_kill_handler;
#Reseting signals so only father will catch them
$SIG{'INT'}='IGNORE';
$SIG{'ALRM'}=sub {foreach my $thr (@parallel::threads){
$thr->kill('ALRM');
}
};
}
sub prc_kill_handler {
my $signame = shift;
printer::colorized_msg("\n got to prc_kill_handler \n","green");
$logger->info("usr_kill_handler: got signal $signame line: ".__LINE__);
testProcess::finishPackage("user stoped");
testProcess::exit_run();
}
##
##
sub Functions_Run_in_threads_windows
{
my (%hash)=@_;
printer::Logger_screen_message ("Info", "Start", "", $logger);
my @temp_file;
if(!%hash)
{
printer::Logger_screen_message("Error","Functions_Run_in_threads: The hash is empty",1,$logger);
return %hash;
}
my ($functions_name,@temp_params,@parameters,%hash_results);
@threads = ();
for( my $i=0 ; $hash{"params_name_$i"} ; $i++)
{
@temp_params=$hash{"params_name_$i"};
$functions_name=$hash{"function_name_$i"};
@parameters=();
#push (@parameters,$functions_name);
if(!@{$temp_params[0]}) #check input if not reference return fail
{
printer::Logger_screen_message("Error","Please send parameters array as referens",1,$logger);
return %hash;
}
#parse hash table and run the function
for( my $index=0 ; $index<@{$temp_params[0]} ; $index++)
{
if($temp_params[0][$index])
{
$tmp=$temp_params[0][$index];
push(@parameters,$tmp);
}
else
{
push(@parameters,"");
}
}
#Start new thread
my $t = threads->create (sub{
$SIG{ALRM} = sub { die("Timeout\n"); };
\&$functions_name(@parameters);
});
push(@threads,$t);
}
my $index=0;
foreach my $thread (@threads) #wait for all threads untill the end and insert results to hash
{
$hash_results{$index}=$thread->join;
$index++;
}
return %hash_results;
}