Prevent script from executing if it is currently running.

This commit is contained in:
Andrew Tomaka 2011-10-15 07:16:54 -04:00
parent 590a77cd45
commit a4cbd44400

View file

@ -4,14 +4,22 @@
// * * * * * php cronjobs/index_data.php >/dev/null 2>&1 // * * * * * php cronjobs/index_data.php >/dev/null 2>&1
// Collect data from various services and store locally for later display. // Collect data from various services and store locally for later display.
//***************************************************************************// //***************************************************************************//
$startTime = time();
$interruptedExecution = false;
register_shutdown_function('shutdown');
clearstatcache();
if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) { if(in_array($_SERVER['SERVER_NAME'],array('localhost','a.io'))) {
$CACHE_FILE = '../data/cache.txt'; $CACHE_FILE = '../data/cache.txt';
$DATA_FILE = '../data/index.txt'; $DATA_FILE = '../data/index.txt';
$LOCK_FILE = getcwd() . '/../data/whoisandrew.lock';
} else { } else {
chdir('/home/atomaka/data'); chdir('/home/atomaka/data');
$CACHE_FILE = 'cache.txt'; $CACHE_FILE = 'cache.txt';
$DATA_FILE = 'index.txt'; $DATA_FILE = 'index.txt';
// path changes in the shutdown() function so we need the full path
$LOCK_FILE = '/home/atomaka/data/whoisandrew.lock';
} }
$dataSources = array( $dataSources = array(
@ -25,6 +33,15 @@ $dataSources = array(
'wow' => 43200, 'wow' => 43200,
); );
if(!file_exists($LOCK_FILE)) {
touch($LOCK_FILE);
} else {
$interruptedExecution = true;
exit();
}
while(true) { }
if(file_exists($CACHE_FILE)) { if(file_exists($CACHE_FILE)) {
$cacheData = json_decode(file_get_contents($CACHE_FILE),true); $cacheData = json_decode(file_get_contents($CACHE_FILE),true);
} else { } else {
@ -304,4 +321,13 @@ function progression_sort($a, $b) {
return $a['progression'] < $b['progression']; return $a['progression'] < $b['progression'];
} }
function shutdown() {
global $interruptedExecution, $LOCK_FILE, $startTime;
if(!$interruptedExecution) {
unlink($LOCK_FILE);
}
$completionTime = time() - $startTime;
}
?> ?>