Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a CGI script that inserts data into an oracle database. It takes quite some time (up to an hour depending on how much data I insert). When the script finishes running it outputs some quality control data.

I want to get some periodic updates (every 30 seconds or so) on the progress of the inserts, while the CGI script is running. The only way I can think to do this is to use a link that opens a new window that refreshes a cgi script that queries the db to determine the progress. I would rather use a counter in the script itself and display that information in the original window. Ideally when the inserts were done, the QC data would then write over this information. Anyone know of a good way to do this??

Thanks.

-jim

Replies are listed 'Best First'.
Re: Keep track of running CGI script
by merlyn (Sage) on Oct 18, 2001 at 19:41 UTC
Re: Keep track of running CGI script
by cLive ;-) (Prior) on Oct 18, 2001 at 22:07 UTC
    I would:
    • fork database update
    • return page to browser with JS that calls a monitor script - use setTimeout()
    When database is updating, it sets a percentage mark - eg, 20,000 records, % mark is 200 ($percent_mark = int($total/100);

    create a temp file. Every $percent_mark records, append a character to the file.

    Return a marker for the temp file in the cgi response, so the JavaScript call points to correct file:

    <SCRIPT LANGUAGE="JavaScript> setTimeout(30000,"top.display_frame.location.href= 'counter.cgi?tmpfil +e=temp_file_name';"); </SCRIPT>
    Then, when you call the counter.cgi file, percentage complete is:
    my $percent_complete = stat(temp_file_name)[7];
    Return this in your display frame along with JS above.

    Amend to suit - only my .02

    cLive ;-)

    PS - if it really takes an hour, you might want to set a cookie and tell them to check back later - and use the cookie to identify them in the script.

Re: Keep track of running CGI script
by tune (Curate) on Oct 18, 2001 at 21:08 UTC
    I did not read The Great Merlyn's article, but I have a simple solution for you.

    The script that is inserting records into the database should append lines to a HTML-formatted logfile, which should be given out by another script using CGI.
    The script should use the META REFRESH tag until it finds a special EOF (or whatever) sign in the logfile. It is possibly not a solution however, if you want to see a lot of information of each insert.

    --
    tune

Re: Keep track of running CGI script
by Anonymous Monk on Oct 18, 2001 at 21:02 UTC
    Wouldn't it be better for a lightweight cgi script that forks another script that actually does the work? That script can then email you with the report. That's my prefered method of getting stuff like that done. --Gavin
Re: Keep track of running CGI script
by Anonymous Monk on Oct 18, 2001 at 22:13 UTC
    Hi. Me again. I am currently forking a process like you suggested, but what I want to do is determine the progress of the script by tracking the number of records it has inserted. So I want the "Please be patient" file to display:


    45896 of 296390 records inserted


    I do not know of a simple way to do this when I fork a child process to do the work. The only thing I thought of was to have the child write the current record to a temporary file at some interval and have the "Please be patient" file read the latest record and display it. I was hoping there was an easier way to do it.

    Thanks for the responses,

    -jim
      I like the suggestion by clive, but I'd modify it slightly. You've got a database so why not get the result from that. Have a cgi script with 2 params, total and begin, total has the number of records we are going to put in the table and begin is how many there was before we started.

      /status.cgi?total=296390&begin=245

      select count(*) from table

      printf "%d of %d records inserted", $count - param('begin'), param('total');

      -- Gavin