| 1 | ############################################################################## | ||
| 2 | # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. # | ||
| 3 | # # | ||
| 4 | # This program is free software; you can redistribute it and/or # | ||
| 5 | # modify it under the terms of the GNU General Public License # | ||
| 6 | # as published by the Free Software Foundation; either version 2 # | ||
| 7 | # of the License, or (at your option) any later version. # | ||
| 8 | # # | ||
| 9 | # This program is distributed in the hope that it will be useful, # | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
| 12 | # GNU General Public License for more details. # | ||
| 13 | # # | ||
| 14 | # You should have received a copy of the GNU General Public License # | ||
| 15 | # along with this program; if not, write to the Free Software # | ||
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.# | ||
| 17 | # # | ||
| 18 | # Jon Howell can be contacted at: # | ||
| 19 | # 6211 Sudikoff Lab, Dartmouth College # | ||
| 20 | # Hanover, NH 03755-3510 # | ||
| 21 | # jonh@cs.dartmouth.edu # | ||
| 22 | # # | ||
| 23 | # An electronic copy of the GPL is available at: # | ||
| 24 | # http://www.gnu.org/copyleft/gpl.html # | ||
| 25 | # # | ||
| 26 | ############################################################################## | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | |||
| 30 | ### | ||
| 31 | ### The Slow module provides a mechanism for a long-running process to | ||
| 32 | ### continue computing while letting the web browser get feedback sooner. | ||
| 33 | |||
| 34 | ### An alternative would be nph-CGI (non-parsed headers, where flushed | ||
| 35 | ### output is sent directly to the browser, rather than pausing in the | ||
| 36 | ### server until it is complete), but | ||
| 37 | ### (a) I don't know if browsers eventually give up on nph- input. (I don't | ||
| 38 | ### think so, but it's an excuse. :v) | ||
| 39 | ### (b) Using nph- would require that admin's web servers were configured | ||
| 40 | ### to use it | ||
| 41 | ### (c) admins would have to rename their CGI stub to nph-fom or somesuch; | ||
| 42 | ### or have two CGI stubs, or something. What a mess. | ||
| 43 | |||
| 44 | package FAQ::OMatic::Slow; | ||
| 45 | |||
| 46 | use FAQ::OMatic; | ||
| 47 | use FAQ::OMatic::I18N; | ||
| 48 | |||
| 49 | my $reloadFrequency = 15; # seconds | ||
| 50 | my $tailSize = 20; # lines | ||
| 51 | |||
| 52 | sub split { | ||
| 53 | my $cgi = FAQ::OMatic::dispatch::cgi(); | ||
| 54 | |||
| 55 | my $slowFile = "slow-output.".FAQ::OMatic::nonce(); | ||
| 56 | my $slowPath = $FAQ::OMatic::Config::metaDir."/".$slowFile; | ||
| 57 | my $url = FAQ::OMatic::makeAref('-command'=>'displaySlow', | ||
| 58 | '-changedParams' => { 'slowFile' => $slowFile }, | ||
| 59 | '-refType'=>'url'); | ||
| 60 | #$url .= "#cursor"; # make browser snap to bottom of page | ||
| 61 | my $pid; | ||
| 62 | |||
| 63 | if (($pid = fork())==0) { | ||
| 64 | # child | ||
| 65 | # close stdio so httpd will not wait for me to get done | ||
| 66 | # and we'll let STDOUT go to the slow-output file, so we | ||
| 67 | # can watch what has been happening | ||
| 68 | open STDIN, "</dev/null"; | ||
| 69 | open SLOWFILE, ">$slowPath"; | ||
| 70 | open STDOUT, ">&SLOWFILE"; # redirect to $slowPath | ||
| 71 | open STDERR, ">&SLOWFILE"; | ||
| 72 | # All "slow-capable" modules are expected to accept the filehandle | ||
| 73 | # we return, and print to that instead. | ||
| 74 | # This is because I thought mod_perl was somehow screwing up | ||
| 75 | # my prints to STDOUT. It turns out I was just never flushing | ||
| 76 | # them, and when the fork()ed child Apache::exit()ed, it went | ||
| 77 | # back to its main loop to handle web requests(!), and the | ||
| 78 | # file never got flushed. But this works pretty well, so we'll | ||
| 79 | # stick with it. | ||
| 80 | |||
| 81 | print SLOWFILE "<p>\n"; | ||
| 82 | |||
| 83 | return *SLOWFILE; | ||
| 84 | } else { | ||
| 85 | # parent | ||
| 86 | # should display results in slowFile | ||
| 87 | FAQ::OMatic::redirect($cgi, $url); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub childDone { | ||
| 92 | close STDOUT; | ||
| 93 | close STDERR; | ||
| 94 | # can't unlink the file, as user will load it one more time? | ||
| 95 | # But then he'll be gone -- what then? How to arrange for the | ||
| 96 | # unlink? | ||
| 97 | } | ||
| 98 | |||
| 99 | sub display { | ||
| 100 | my $params = shift; | ||
| 101 | |||
| 102 | if (not $params->{'slowFile'} =~ m/^(slow-output.[p\d]+)$/) { | ||
| 103 | FAQ::OMatic::gripe('error', | ||
| 104 | "Taint check failed on ".$params->{'slowFile'}); | ||
| 105 | } | ||
| 106 | |||
| 107 | my $slowFile = $1; | ||
| 108 | my $url = FAQ::OMatic::makeAref('-command'=>'displaySlow', | ||
| 109 | '-changedParams' => { 'slowFile' => $slowFile }, | ||
| 110 | '-refType'=>'url'); | ||
| 111 | #$url .= "#cursor"; # make browser snap to bottom of page | ||
| 112 | # unfortunately, what appears to be a bug in Netscape foils | ||
| 113 | # that trick. So I'll only display the bottom n lines | ||
| 114 | # of the file, instead. Harrumph. | ||
| 115 | |||
| 116 | my $slowPath = $FAQ::OMatic::Config::metaDir."/".$slowFile; | ||
| 117 | if (not open SLOW, $slowPath) { | ||
| 118 | FAQ::OMatic::gripe('error', "can't open $slowFile: $!"); | ||
| 119 | } | ||
| 120 | print "Content-type: text/html\n"; | ||
| 121 | print "Refresh: $reloadFrequency; URL=$url\n"; | ||
| 122 | print "\n"; | ||
| 123 | |||
| 124 | my @tail = (); | ||
| 125 | my $cropped = '<pre>'; | ||
| 126 | while (defined($_=<SLOW>)) { | ||
| 127 | if ((not $params->{'wholeFile'}) | ||
| 128 | and (scalar(@tail) >= $tailSize)) { | ||
| 129 | shift(@tail); | ||
| 130 | $cropped = "<pre>...(cropped)...<br>\n"; | ||
| 131 | } | ||
| 132 | push @tail, $_; | ||
| 133 | } | ||
| 134 | close(SLOW); | ||
| 135 | print "<title>Slow page</title>\n"; | ||
| 136 | |||
| 137 | my $msg; | ||
| 138 | print gettexta("This page will reload every %0 seconds,", $reloadFrequency); | ||
| 139 | print " "; | ||
| 140 | if ($params->{'wholeFile'}) { | ||
| 141 | print gettexta("showing the entire process output."); | ||
| 142 | $msg = gettexta("Show the tail of the process log"); | ||
| 143 | } else { | ||
| 144 | print gettexta("showing the last %0 lines of the process output.", $tailSize); | ||
| 145 | $msg = gettexta("Show the entire process log") | ||
| 146 | } | ||
| 147 | print "\n"; | ||
| 148 | my $url2 = FAQ::OMatic::makeAref('-command'=>'displaySlow', | ||
| 149 | '-changedParams' => {'wholeFile' => !$params->{'wholeFile'}}); | ||
| 150 | print "[".$url2.$msg."</a>]\n"; | ||
| 151 | print "<hr>\n"; | ||
| 152 | |||
| 153 | print $cropped; | ||
| 154 | print join('', @tail); | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; |