
| Current Path : /var/www/web-klick.de/dsh/10_customer2017/1204__intel/ |
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/10_customer2017/1204__intel/Job.pm |
package Job;
use File::Temp qw();
BEGIN {
if($^O =~ /win/i) {
require IPC::Run;
IPC::Run->import('run');
}
}
my %JOBS_BY_PID;
sub new
{
my ($class, %args) = @_;
return bless(\%args, $class);
}
sub start
{
my $this = shift;
# create temporary files
my %opt = (SUFFIX => '.log');
if($this->{tmpdir}) {
%opt = (DIR => $this->{tmpdir});
}
$this->{_tmp_out} = File::Temp->new(%opt, TEMPLATE => 'Job_out_XXXXXXXX');
$this->{_tmp_out_name} = $this->{_tmp_out}->filename;
unless($this->{combine_err}) {
$this->{_tmp_err} = File::Temp->new(%opt, TEMPLATE => 'Job_err_XXXXXXXX');
$this->{_tmp_err_name} = $this->{_tmp_err}->filename;
}
#if($^O =~ /win/i) {
# $this->{_tmp_out}->close();
# $this->{_tmp_err}->close() unless($this->{combine_err});
#}
my $pid = fork();
unless(defined $pid) {
die "ERROR: Cannot fork: $!\n";
}
elsif($pid) {
# parent
$this->{_pid} = $pid;
$JOBS_BY_PID{$pid} = $this;
close($this->{_tmp_out});
unless($this->{combine_err}) {
close($this->{_tmp_err});
}
return 1;
}
else {
# child
# redirect STDOUT and STDERR
if($^O =~ /win/i) {
my $cmdref;
# start the command
my $cmd = $this->{command};
my $type = ref $cmd;
if($type && $type eq 'CODE') {
# sub reference
exit &$cmd; # TODO we do not capture any output here!!!
}
elsif($type && $type eq 'ARRAY') {
# array
$cmdref = $cmd;
goto ProcessOut;
}
else {
# we assume it is a string
$cmdref = [ 'cmd', '/c', $cmd ];
ProcessOut:
my @cmdopt = $this->{combine_err} ? ('>&', $this->{_tmp_out_name}) :
('>', $this->{_tmp_out_name}, '2>', $this->{_tmp_err_name});
run($cmdref, @cmdopt);
my $exit_code = $?;
exit($exit_code>>8);
}
} else { # UNIX
open(STDOUT,">&", $this->{_tmp_out}) || die "Can't redirect STDOUT: $!";
if($this->{combine_err}) {
open(STDERR,">&", \*STDOUT) || die "Can't redirect STDERR: $!";
} else {
open(STDERR,">&", $this->{_tmp_err}) || die "Can't redirect STDERR: $!";
}
# start the command
my $cmd = $this->{command};
my $type = ref $cmd;
if($type && $type eq 'CODE') {
# sub reference
exit &$cmd;
}
elsif($type && $type eq 'ARRAY') {
# array
system @$cmd;
exit($?>>8);
}
else {
# we assume it is a string
exec $cmd;
}
}
### FULL STOP
} # end fork
1;
}
sub getCompletedJob
{
return unless keys %JOBS_BY_PID;
my $pid = wait();
my $exit = $?;
if($pid == -1) {
# no child processes
%JOBS_BY_PID = ();
return;
}
my $job = delete $JOBS_BY_PID{$pid};
unless(defined $job) {
warn "ERROR: Could not find job for PID $pid\n";
return;
}
$job->{status} = $exit;
return $job;
}
sub getOutput
{
my $this = shift;
unless(open(IN, "<", $this->{_tmp_out_name})) {
warn "ERROR: Cannot get output of job from file ".$this->{_tmp_out_name}.": $!\n";
return;
}
local($/) = undef;
my $content = <IN>;
close(IN);
return $content;
}
sub DESTROY
{
my $this = $_[0];
$this->{_tmp_out} = $this->{_tmp_err} = undef;
}
1;