#!Perl -w
use strict;
use CGI::Pretty qw(:standard :cgi-lib);
use CGI::Carp qw(fatalsToBrowser); # Remove for production code
use CGI::Session;
$CGI::DISABLE_UPLOADS = 1; # Disable uploads
$CGI::POST_MAX = 10240; # Maximum number of bytes per post
$| = 1; # Unbuffered output
if (param('Spawn')) {
# setup monitoring page then spawn the monitored process
my $cache = CGI::Session->new ();
my $session = $cache->id();
$cache->param ('status', "wait ..."); # no data yet
Delete_all();
# parent redirects browser to monitor session
param('session', $session);
print redirect (self_url());
close STDOUT;
# Rest of this block alters in *nix context to spawn monitored process
use Win32::Process;
my $job;
Win32::Process::Create (
$job,
'c:/Perl/bin/perl.exe', "perl.exe spawned.pl \"$session\"",
0,
NORMAL_PRIORITY_CLASS | DETACHED_PROCESS,
'.'
);
exit 0; # all done
} elsif (my $session = param('session')) {
# display monitored data
my $cache = CGI::Session->new ($session);
my $data = $cache->param ('status');
if (! $data) { # something is wrong
showError ("Cache data not available");
exit 0;
}
my $headStr = $data eq 'Completed' ? '' : "";
print header();
print start_html (-title => "Spawn Results", -head => [$headStr]);
print h1("Spawn Results");
print pre(escapeHTML($data));
print end_html;
} else {
# display spawn form
print header(), start_html("Spawn"), h1("Spawn");
print start_form();
print submit('Spawn', 'spawn');
my %params = Vars ();
for my $param (keys %params) {
print br ("$param -> $params{$param}");
}
print end_form(), end_html();
}
exit 0;
sub showError {
print header(), start_html("SpawnError"), h1("Spawn Error");
print p (shift);
my %params = Vars ();
for my $param (keys %params) {
print br ("$param -> $params{$param}");
}
print end_html();
}
####
#!Perl -w
use strict;
use CGI::Session;
my $session = shift;
my $cache = CGI::Session->load ($session);
$cache->param('status', "configuring ..."); # no data yet
my $end = time () + 20;
my $count = 0;
while (time () < $end) {
$cache->param ('status', "Count: $count\n");
$cache->flush ();
++$count;
sleep (1);
}
$cache->param ('status', "Completed");
$cache->flush ();
exit 0; # all done