Login   Register  
PHP Classes
elePHPant
Icontem

How to use it?

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      PHP Cron  >  All threads  >  How to use it?  >  (Un) Subscribe thread alerts  
Subject:How to use it?
Summary:Hmm... one of most nonunderstable class.
Messages:2
Author:Kaurov Eugene
Date:2005-07-05 20:56:17
Update:2005-07-07 07:37:58
 

  1. How to use it?   Reply   Report abuse  
Picture of Kaurov Eugene
Kaurov Eugene
2005-07-05 20:56:17
Hallo. Tell me, how to use it? Have you an example?

And one: May I use your class for sheduler planing?

  2. Re: How to use it?   Reply   Report abuse  
Picture of Mick Sear
Mick Sear
2005-07-07 07:37:58 - In reply to message 1 from Kaurov Eugene
OK, so perhaps I should have added an example before I posted the class... :)

I wrote this to solve this problem: How to log cron events and then check if they ran when they should. So, one script runs on server A, and logs the fact in a DB. Server B's script knows what the schedule is (something like 0 5 * * *) and needs to know how to determine if the job on server A ran when it was supposed to. So, the script has something like this which logs and checks:

$project = 1;
@include "slave.php";
@db_log($project, "CRON", "$_ENV[HOSTNAME] starting test cron job");

Slave.php, apart from some includes, has the following code:

if (isset($project)){
if (didCronRun($project, "A") && $_ENV[HOSTNAME] == "B"){
$debug_msg .= "$project cron ran on $_ENV[HOSTNAME] !!!\n";
exit;
} else {
$debug_msg .= "Hostname is set to ".$_ENV[HOSTNAME];
}
}

and the hostname is set an an environment variable in the crontab.

Finally, actually using the class: the function 'didCronRun()' is defined as follows:

/**
* Utility function to determine if the cron job (id) ran normally for a
* given host.
*/
function didCronRun($id, $host){
//Get the cron schedule for this project.
global $debug_msg;
$db = new mcs_db();
$q = "select cron_schedule from projects where id='".$id."'";
$cron_schedule = mysql_result(mysql_query($q, $db->conn),0,0);
$debug_msg .= "Cron schedule: ".$cron_schedule."\n";

//Start new cron parser instance

$cron = new CronParser($cron_schedule);
$lastRan = $cron->getLastRan();

$nice_time = "Last ran: ".$lastRan[5]."/".$lastRan[3]."/".$lastRan[2]." ".$lastRan[1].":".$lastRan[0];
$debug_msg .= "Last ran: ".$nice_time."\n";

//Convert to Unix timestamp
$cron_ran = mktime ( $lastRan[1] ,$lastRan[0],0 , $lastRan[3] ,$lastRan[2], $lastRan[5]);
//$window = mktime ($lastRan[1] ,$lastRan[0],0 , $lastRan[3] ,$lastRan[2], $lastRan[4]);
$debug_msg .= "Cron ran ".date("l dS of F Y h:i:s A", $cron_ran)."\n";

$debug_msg .= "Window ".date("l dS of F Y h:i:s A", $cron_ran-45)."\n";

//Now, we look for a log entry for this app's cron job running OK
//We're looking for a time within 45 secs or so of the $cron_ran time...
$q = "SELECT * FROM `monitor` WHERE event_date > from_unixtime(".($cron_ran-45).") " .
" and event_type = 'CRON' " .
" and project_id='".$id."' " .
" order by event_date desc ";
$debug_msg .= $q."\n";

$r = @mysql_query($q, $db->conn);
$row = @mysql_fetch_array($r);

//Return either true or false depending on whether the job has run OK since given time.
if (isset($row['hostname']) && $row['hostname'] == $host){
return true ;
} else {
return false;
}

}

I'll add some proper documentation and an example application at some stage soon. Hopefully this helps you for now.

Cheers,
Mick