use Win32::Process;
use Win32;
####
sub start_app {
my ($self, $app) = @_;
my $ProcessObj;
if(!Win32::Process::Create($ProcessObj,
$app->{app},
$app->{app}." ".$app->{cmdlineopts},
0,
NORMAL_PRIORITY_CLASS,
$self->{basePath})) {
print "Error starting app " . $app->{description} . ": " .
Win32::FormatMessage( Win32::GetLastError() ) .
"\n";
$app->{handle} = undef;
return 0;
} else {
$app->{handle} = $ProcessObj;
my $app_pid = $app->{handle}->GetProcessID();
print "Started app " . $app->{description} . " with PID " . $app_pid . "\n";
return 1;
}
}
####
sub check_app {
my ($self, $app) = @_;
# Ignore if this application was never started
# or was intentionally stopped
if(!defined($app->{handle})) {
return $self->start_app($app);
}
# First, check if the process exited
if($app->{handle}->Wait(1)) {
# Process exited, so, restart
print "Process exit detected: " . $app->{description} . "!n";
return $self->start_app($app);
}
# All ok, just return true
return 1;
}
####
sub stop_app {
my ($self, $app) = @_;
if(defined($app->{handle}) && $app->{handle}) {
print "Killing app " . $app->{description} . "...\n";
my $app_pid = $app->{handle}->GetProcessID();
$app->{handle}->Kill(0);
$app->{handle}->Wait(2000);
$app->{handle} = undef;
print "...killed.\n";
} else {
print "App " . $app->{description} . " already killed\n";
}
}