This is my implementation of the scheduler, I only need to run it if it is inactive and add all the tasks to the mysql task table (I already have one for my main script), this script will run all the tasks ready for execution (the sql table has a datetime field )
What I call "Mutex" is a class that reports that one or more copies of the script are working, and can even send commands to the running script through the pipe (you just need to create a new mutex with the same name for all scripts), so you can even stop the script from starting from another script.
<?php //---logging--- $logfile = dirname(dirname(__FILE__)).'/scheduler.log'; $ob_file = fopen($logfile,'a'); function ob_file_callback($buffer) { global $ob_file; fwrite($ob_file,$buffer); } //--includes--- $inc=dirname(dirname(__FILE__)).'/.include/'; require_once($inc.'Mutex.php'); require_once($inc.'jobdb.php'); //--mutex--- //i call it mutex but it nothing like POSIX mutex,it a way to synchronyze scripts $m=new Mutex('jscheduler'); if(!$m->lock())//if this script is already running exit();//only one scheduler at time //---check loop--- set_time_limit(-1);//remove script time limit for(;;){ ob_start('ob_file_callback');//logging $j=jobdb_get_ready_jobs(true);//gets all ready jobs,works with mysql if($j!=null)//found some jobs foreach($j as $val){//run all jobs $ex='*SCRIPT NAME AND PARAMETERS HERE*'; if(!run_script($ex)) echo "UNABLE TO LAUNCH THE JOB!\n"; } $n=($j!=null)?count($j).'JOBS LAUNCHED':'NO JOBS'; sleep(60); if($m->has_to_stop())//yeah,i can stop this script from other scripts,it works with a file pipeline exit("# STOPPING SCHEDULER\n"); ob_end_flush();//LOGGING } ?>
My run_script function works the same as the Sciberras run function.
To activate the scheduler, you just have to use this command
run_script('scheduler.php');
to check if it is active
$m=new Mutex('jscheduler'); if(!$m->test_lock()) echo 'SCHEDULER IS ACTIVE'; else echo 'SCHEDULER IS INACTIVE';
and stop the scheduler
$m=new Mutex('jscheduler'); $m->ask_to_stop();
Perhaps I have moved too far in its implementation, one way or another, there may be a problem with a βdelayβ, for example, if the scheduler starts at 0: 00.00, and I have two scripts at 0: 01.01 and 0: 01.59, both are running in 0: 02.0. To correct this βlagβ, I can return all tasks scheduled in the next minute and schedule them, as in Sciberras code, using time_sleep_until. This will not create too many running threads (I might want to check if there is a limit or performance drop when starting the number of HUDGE threads, but I'm sure there will be some problems) and provides an ideal time, requiring only checking, the scheduler is active.