• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/handlers/cronhandler.php

00001 <?php
00007 namespace Habari;
00008 
00013 class CronHandler extends ActionHandler
00014 {
00020   static function run_cron( $async = false )
00021   {
00022     // check if it's time to run crons, and if crons are already running.
00023     $next_cron = DateTime::create( Options::get( 'next_cron' ) );
00024     $time = DateTime::create();
00025     if ( ( $next_cron->int > $time->int )
00026       || ( Options::get( 'cron_running' ) && Options::get( 'cron_running' ) > microtime( true ) )
00027     ) {
00028       return;
00029     }
00030 
00031     // cron_running will timeout in 10 minutes
00032     // round cron_running to 4 decimals
00033     $run_time = microtime( true ) + 600;
00034     $run_time = sprintf( "%.4f", $run_time );
00035     Options::set( 'cron_running', $run_time );
00036 
00037     if ( $async ) {
00038       // Timeout is really low so that it doesn't wait for the request to finish
00039       $cronurl = URL::get( 'cron',
00040         array(
00041           'time' => $run_time,
00042           'asyncronous' => Utils::crypt( Options::get( 'public-GUID' ) )
00043         )
00044       );
00045       $request = new RemoteRequest( $cronurl, 'GET', 1 );
00046 
00047       try {
00048         $request->execute();
00049       }
00050       catch ( RemoteRequest_Timeout $e ) {
00051         // the request timed out - we knew that would happen
00052       }
00053       catch ( \Exception $e ) {
00054         // some other error occurred. we still don't care
00055       }
00056     }
00057     else {
00058       // @todo why do we usleep() and why don't we just call act_poll_cron()?
00059       usleep( 5000 );
00060       if ( Options::get( 'cron_running' ) != $run_time ) {
00061         return;
00062       }
00063 
00064       $time = DateTime::create();
00065       $crons = DB::get_results(
00066         'SELECT * FROM {crontab} WHERE start_time <= ? AND next_run <= ? AND active != ?',
00067         array( $time->sql, $time->sql, 0 ),
00068         'CronJob'
00069       );
00070       if ( $crons ) {
00071         foreach ( $crons as $cron ) {
00072           $cron->execute();
00073         }
00074       }
00075 
00076       EventLog::log( _t( 'CronTab run completed.' ), 'debug', 'crontab', 'habari', $crons );
00077 
00078       // set the next run time to the lowest next_run OR a max of one day.
00079       $next_cron = DB::get_value( 'SELECT next_run FROM {crontab} WHERE active != ? ORDER BY next_run ASC LIMIT 1', array( 0 ) );
00080       Options::set( 'next_cron', min( intval( $next_cron ), $time->modify( '+1 day' )->int ) );
00081       Options::set( 'cron_running', false );
00082     }
00083   }
00084 
00091   function act_poll_cron()
00092   {
00093     Utils::check_request_method( array( 'GET', 'HEAD', 'POST' ) );
00094 
00095     $time = doubleval( $this->handler_vars['time'] );
00096     if ( $time != Options::get( 'cron_running' ) ) {
00097       return;
00098     }
00099 
00100     // allow script to run for 10 minutes. This only works on host with safe mode DISABLED
00101     if ( !ini_get( 'safe_mode' ) ) {
00102       set_time_limit( 600 );
00103     }
00104     $time = DateTime::create();
00105     $crons = DB::get_results(
00106       'SELECT * FROM {crontab} WHERE start_time <= ? AND next_run <= ? AND active != ?',
00107       array( $time->sql, $time->sql, 0 ),
00108       'CronJob'
00109     );
00110 
00111     if ( $crons ) {
00112       foreach ( $crons as $cron ) {
00113         $cron->execute();
00114       }
00115     }
00116 
00117     // set the next run time to the lowest next_run OR a max of one day.
00118     $next_cron = DB::get_value( 'SELECT next_run FROM {crontab} WHERE active != ? ORDER BY next_run ASC LIMIT 1', array( 0 ) );
00119     Options::set( 'next_cron', min( intval( $next_cron ), $time->modify( '+1 day' )->int ) );
00120     Options::set( 'cron_running', false );
00121   }
00122 
00123 }
00124 
00125 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1