|
Often when executing a batch process to convert and/or move records of data your script
execution is subject to a timeout specified in a configuration parameter on the various servers
in any of the Webserver, dynamic server-side scripting or database server. There are several options.
The method described below is the most efficient method. A less superior option is to specify a meta
refresh timer or javascript timer routine for the page onload body event, which would cause major
delays if a page process completed before the timer refreshed the page. Placing the javascript
within the body onload event would require an inline html output using the javascript DOM functions
in order to display a detailed report of the progress for each page and output of the closing HTML/BODY tags.
The method implemented below is the cleaner and simpler approach. Most of the structure of the code is
generally the same for all applications exhibiting the batch procedure.
* Remarks: The PHP script is interpreted and executed by the server run-time library, and the batch process
script function is executed. Once the batch process is complete and the function returns with new parameters
for propagation, then the HTML/output is buffered and sent to the
client's Web browser as HTML. The HTML page is parsed by the client Web browser and because
it contains JavaScript, it is interpreted and the same page is loaded with updated parameters identifying
the continuation of the batch process from where it left off on the previous page. The batch process function
acquires pass-by-reference parameters for the reflection of their updated values.
|
<?
// contains all complex functions pertaining to a specific conversion and data transfer to the database server (your batch process)
include_once ("batch_conversion.php");
// defines the maximum number of records (block) to process per page load where this constant should be low enough to never meet the lowest server timeout in seconds (dependent on the time consumption of the batch process and server timeout)
define ('MAX_QUEUE', 5);
// obtain crucial URL parameter(s)
$prev_ttl = $ttl = (isset ($_GET['total']) ? (int) $_GET['total'] : (int) 0);
if ($ttl <= 0) // clear out all existing records (specific to this conversion)
{
// create a new object and remove all table records
$new_structure = new project_dbops ();
$new_structure->clear_tables ();
}
// 'end' will receive a flag indicating end of records, marks process completion
// 'ttl' will be increased by the number of records written on each page load
// 'MAX_QUEUE' is obvious
// no output should be contained in the conversion or data transfer process,
// it should be placed in the pass-by-reference parameter string
// so that it is output after the HTML header and body
$sOutput = "";
$end = launch_conversion_range (&$sOutput, &$ttl, MAX_QUEUE);
// output the HTML header
echo "
<html>
<head>
<title>BATCH DATABASE PROCESS</title>
<meta http-equiv='content-type' content='text/html; charset=iso-8859-1' />
</head>
<body>
";
// output a progress report for the current page -- if a batch process error occurs then the last record ID will indicate the problematic record
echo "<p align = 'center' style = 'font-size: 18pt'>BATCH DATABASE PROCESS</p>\n";
echo "<p>$sOutput</p>\n"; // output details acquired from conversion function
echo "<p><b>Completed records: ".($prev_ttl + 1)." - $ttl</b></p>\n";
if ($end) // batch process complete
echo "<p><b>END OF CONVERSION PROCESS</b></p>\n";
else if (!$end) // batch process has not been completed in its entirety yet
echo "<script language = 'JavaScript'
type = 'text/javascript'>window.location.href =
\"http://domain-name.com/rel_path/this_page.php?total=$ttl\";</script>\n";
/* JavaScript will load the page with parameters after the PHP has processed the
main conversion function and buffered and output the HTML herein. If completed,
the javascript output will terminate */
?>
Code View - dynamicreport.com
|
Your PHP conversion function may look something like this (in batch_conversion.php):
* Remarks: This function is specific to a particular conversion application, but may
look structurally similar to any specific application at the higher level.
|
<?
function launch_conversion_range ($sOutput, $ttl, $max)
{
$db_c = new DB_C ();
$db_c->connect ();
// convert range of data identified uniquely by rid (query to obtain ID information)
$db_c->query ("SELECT DISTINCT rid FROM tbl1");
// check if beyond last record
if ($ttl >= $db_c->num_rows ())
$end = true;
else
{
// move to the current starting record of the block of records to be processed
$db_c->seek ($ttl);
$end = false;
}
for ($i = 0; $i < $max; $i++)
{
if ($end || !$db_c->next_record ())
{
// end of record conversion reached (no records remaining)
$end = true;
$i++;
break;
}
$rid = $db_c->f (0);
// aggregate some useful details for output
$sOutput .= "record: ".($ttl + $i)."ID: $rid<br>\n";
// single record conversion
$arr = get_convert_data ($rid);
xfer_data_db ($arr, $rid);
}
$ttl += $i;
return $end;
}
?>
Code View - dynamicreport.com
|
|