|
Posted by Ciaran on 01/30/06 11:53
Rob, you could try something like the below.
Basically it checks the logfile's modified date, which should be
cheaper than opening it. Also you can set a probability that a page
will trigger a DB check, to avoid multiple pages trying to do the check
at the same time.
It includes a mechanism for locking, so that hopefully if a number of
scripts are all checking the log at the same time, the later ones will
fail.
Note: this is just to give you an idea, I haven't tested this:
<?php
$logfile = 'test.txt'; // the file to write to
$timeout = 60*60*24; // set timeout to an hour
$prob = 1000; // the probability a script will bother checking
// only do the check one time in $prob times!
if(rand(1, $prob) == 1){
// only do this if the file isn't there or its modified time
// is longer ago than the timeout
if(!file_exists($logfile) || (time() - filemtime($logfile)) >
$timeout){
// open the file for appending
$fp = fopen($logfile, "a");
// ask for an exclusive lock
if (flock($fp, LOCK_EX)) {
// do whatever checks you want, here!
// write to the log
fwrite($fp, "Completed DB Check at ".date('Y-m-d H:i:s')."\n");
// release the lock
flock($fp, LOCK_UN);
} else {
// if you couldn't lock it, error!
echo "<!-- DB check failed -->";
}
// close the file pointer
fclose($fp);
}
}
?>
-Ciaran
-Ciaran
Navigation:
[Reply to this message]
|