sub cge_start { my($arg_ref) = @_; #=+ iterate over provided config and replace defaults while(my($k,$v) = each %$arg_ref) { #=+ Replace each default value if the key exists. Perhaps paranoid, but prevent arbitrary/unexpected settings $arguments{$k} = $v if exists $arguments{$k}; } #=+ Make sure we have a bare minimum of settings return undef if $arguments{dataDir} eq '' || !-d $arguments{dataDir} || $arguments{imagesPerNode} eq '' || $arguments{nodeCount} eq ''; #=+ Create the results directory # make sure that our directory has a trailing slash "/" $arguments{dataDir} .= '/' unless $arguments{dataDir} =~ m/\/$/; mkdir $arguments{dataDir}.'results', 0744 if !-d $arguments{dataDir}.'results'; $arguments{resultDir} = $arguments{dataDir}.'results'; #=+ Create the logfile string (does not need to exist) $arguments{logFile} = $arguments{dataDir}.'cge_logfile.log'; #=+ Make sure we have both the number of instances and number of desired nodes return (undef,undef) unless $arguments{imagesPerNode} > 0 && $arguments{nodeCount} > 0; #=+ Create a cleanup script that we will use to kill "this" server session $arguments{cleanupScript} = $arguments{dataDir}.'cleanup.sh'; #=+ Make sure we have a free TCP port to use my $port = 3750; if(check_port($port)) { $port++; while(check_port($port)) { $port++; } } $arguments{queryPort} = $port; #=+ Concatenate all the non-blank command line arguments my @args; while(my($k,$v) = each %arguments) { push @args, '--'.$k.' '.$v.' ' unless $v eq ''; } my $arg_string = join('',@args); #=+ Run the launcher #exec($cge_launch.' '.$arg_string.'> /dev/null 2>&1 &'); my $ref = run_forked($cge_launch.' '.$arg_string.' &', {stdout_handler => sub { my $line = @_; say 'BUFFER: '.Dumper($line); if($line =~ m/Starting port forwarding/) { open(my $CU,'<',$arguments{cleanupScript}) or croak $!; my $text = <$CU>; close($CU); my $pid; if($text =~ m/scancel (\d+)/m) { $pid = $1; } return ($pid,$port); } }}); #my ($success,$error,$buffer_arrayref,$stdout_arrayref,$stderr_arrayref) = run(command => $cge_launch.' '.$arg_string, verbose => 1); # if($success) { # foreach my $line(@$buffer_arrayref) { # say 'BUFFER: '.$line; # if($line =~ m/Starting port forwarding/) { # open(my $CU,'<',$arguments{cleanupScript}) or croak $!; # my $text = <$CU>; # close($CU); # # my $pid; # if($text =~ m/scancel (\d+)/m) { # $pid = $1; # } # return ($pid,$port); # } # } # } #else { return (undef,undef); #} }