Location: PHPKode > projects > WEB-PA > spider/spidermanager.php
<?php

$spidermanager_version = '1.0';

require('include/config.inc.php');
require('include/funzioni.php');

	// Config section
	$debugfile = $SPIDER_HOME . '/log/spidermanager.log';
	// End config

	logga("SpiderManager ver. $spidermanager_version - Checking");

	$statustring = getSpiderStatus();
	$x = strpos($statustring,' ');
	$status = substr($statustring,0,$x);
	$rest = substr($statustring,$x+1);
	$command = getSpiderCommand();

	if ($status == 'FIXUP' || $status == 'RUN') {
		if (inprogress('analizza.php' /*,TRUE */)) {
			logga("OK - $status in progress");
			if ($command=='FIXUP' || $command == 'RUN') {
				command('');
				logga("caught $command command - cancelled");
			}
		} else {
			logga("ERROR $status interrupted - Process analizza is NOT active!");
			inprogress('analizza.php',TRUE);
			status("IDLE $status interrupted at " . date('Y-m-d H:i'));
			command($status);
			reschedule('analizza');
			status('WAIT Spider rescheduled (1) at '. date('Y-m-d H:i'));
		}
	} else if ($status == 'WAIT') { // Do nothing!
		logga('status=' . $statustring);
		if ($command != null) {
			if ($command=='STOP') {
				status('IDLE Stopped waiting at ' . date('Y-m-d H:i'));
			}
		}
	} else if ($status == 'IDLE') { // IDLE or other
		logga('status=' . $statustring);
		if ($command != null) {
			logga('command=' . $command);
			if ($command=='FIXUP' || $command == 'RUN') {
				if (!inprogress('anali')) {
					reschedule('analizza');
					status('WAIT Spider scheduled (2) at '. date('Y-m-d H:i'));
				}
			}
		}
	}



function logga($phrase) {
	global $debugfile;

	$now = date('Y-m-d H:i') . " $phrase\n";

	$fh = fopen($debugfile, 'a') or die("can't open file");
	fwrite($fh, $now);
	fclose($fh);
}


function reschedule($script) {
	global $SPIDER_HOME;

		logga("Rescheduling now: $script");
		$cmd="/usr/bin/crontab -l";
		exec($cmd, $result);

	$fh = fopen("$SPIDER_HOME/reschedule.txt", 'w') or die("can't open file");
		foreach ($result as $key => $val) {
			if (!(strpos($val,$script) === false)) {
				$cmd = substr($val,strpos($val,'/'));
				$val = '' . date('i H d m',mktime(date("H"), date("i")+2, 0, date("m"), date("d"),  date("Y"))) . ' * ' . $cmd;
			}
			fwrite($fh, $val . "\n");
		}
	fclose($fh);

	$cmd="/usr/bin/crontab < $SPIDER_HOME/reschedule.txt";
	exec($cmd, $result, $ret);

	if ($ret > 0) {
		logga("ERRORE - rescheduler crontab returned: $ret\n");
	}
	//echo "reschedule result: $ret\n";
	//foreach ($result as $key => $val) {
	//	echo "-$val\n";
	//}
}

function inprogress($command,$verbose = FALSE) {
		$cmd="/bin/ps aux | grep $command";
		exec($cmd, $result, $retval);
		if ($verbose) logga("retval: $retval\nResult----------\n" . implode("\n",$result) . "\n---------------\n");
		$count = 0;
		foreach ($result as $line) {
			if (strpos($line,'grep ') == FALSE) $count++;
		}
		if ($verbose) logga("$count processes matching '$command'\n");
		if ($count > 0) {
			return true;
		} else {
			return false;
		}
}

?>
Return current item: WEB-PA