Hi Enlighted Ones,
After some time away, I beyond you to expose you some weird beavour I can't explain, and hope some of you can.
I'm developing a site with HTML::Mason, and needed to import records to the database from an .xls file. As the file can be large and there are some verifications for each record before it is imported, I thought that a fork would do the work, and it did, and with some caching I got a progress report working as expected.
Well, at least until the import finished.
My code would autorefresh every few second and report the progress, and the last report would just report the import as completed, without the refresh. This is what I expected.
But what I got was the template, without any reporting.
Around and around I've got, print debug for both STDERR and the component output, until some crazy ideia bitten me... what if this last output I'm getting is from the forked component?
Tried to send some output there, and voilá... it was. Made some testes, and yes, it happens. And it isn't browser dependent (or at least works the same with IE, Galeon and Firefox). It don't happen all the time, but happens, aparently, everytime the last refresh is requested after the forked component is completed and returned.
My question is: Anyone would expect the forked component result to be shown as the final report?
I'm using $HTML::Mason::Version=1.28; with apache 1.3.33, and this is the code I used to verify the beavour I detected, and that is running at
http://lab.camelot.co.pt/long/.
<%perl>
use Digest::MD5 qw(md5_hex);
my $url=$r->uri();
my $sid=$ARGS{sid};
print STDERR "Lab: Got $url ($sid)\n";
my $res="";
my $ended=0;
if ($sid) {
my $rs=$m->cache->get($sid);
if ($rs) {
($ended,$res)=@{$rs};
unless ($ended) {
$res.=<<EOF;
<form name='refreshimport' method='post' action='$url'>
<input type=hidden name=sid value='$sid'>
</form>
<script>
setTimeout('document.refreshimport.submit();',1500);
</script>
EOF
}
} else {
$res="INVALID SESSION";
}
} else {
$sid=md5_hex(time().{}.rand().$$);
$m->cache->set($sid,[(0,"")],'5 min');
if (my $pid=fork()) {
$url=$r->uri();
$url.="?sid=$sid";
print STDERR "Going to $url\n";
$r->header_out('location',$url);
$r->status(302);
return;
} elsif(defined($pid)) {
close STDOUT;
close STDIN;
my $rnd;
for my $i (1..25) {
$rnd.=md5_hex(rand());
$rnd.="<br>";
$m->cache->set($sid,[(0,"$i: $rnd")],'5 min');
sleep 1;
}
$m->cache->set($sid,[(1,"done: $rnd")],'5 min');
$res="I was expecting this to go anywhere!!!<br>".$rnd;
} else {
}
}
</%perl>
<%$res%>
Thank you,