b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.8) [ class tree: main ] [ index: main ] [ all elements ]

Source for file cron_exec.php

Documentation is available at cron_exec.php

  1. <?php
  2. /**
  3.  * Example to use CLI:
  4.  * >c:\php4\php cron_exec.php
  5.  * >c:\php4\php-cli cron_exec.php
  6.  */
  7.  
  8. require_once dirname(__FILE__).'/../conf/_config.php';
  9.  
  10. // This MIGHT be overkill. TODO: check...
  11. require_once $inc_path .'_main.inc.php';
  12.  
  13. /**
  14.  * Cron support functions
  15.  */
  16. require_once $model_path.'cron/_cron.funcs.php';
  17.  
  18. if$is_cli )
  19. // This is a web request:
  20.     ?>
  21.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  22.     <html>
  23.     <head>
  24.         <title>Cron exec</title>
  25.         <link rel="stylesheet" type="text/css" href="<?php echo $rsc_url ?>css/basic.css">
  26.     </head>
  27.     <body>
  28.         <h1>Cron exec</h1>
  29.         <p>This script will execute the next task in the cron queue.
  30.         You should normally call it with the CLI (command line interface) version of PHP
  31.         and automate that call through a cron.</p>
  32.     <?php
  33. }
  34.  
  35. /*
  36.  * The following will feel a little bloated...
  37.  * BUT it is actually a pretty nifty design to prevent double execution of tasks without any transaction!
  38.  * The trick is to rely on the primary key of the cron__log table.
  39.  */
  40.  
  41. // Get next task to run in queue which has not started execution yet:
  42. $sql 'SELECT *
  43.                     FROM T_cron__task LEFT JOIN T_cron__log ON ctsk_ID = clog_ctsk_ID
  44.                  WHERE clog_ctsk_ID IS NULL
  45.                      AND ctsk_start_datetime <= '.$DB->quotedate2mysql($localtimenow) ).'
  46.                  ORDER BY ctsk_start_datetime ASC, ctsk_ID ASC
  47.                  LIMIT 1';
  48. $task $DB->get_row$sqlOBJECT0'Get next task to run in queue which has not started execution yet' );
  49.  
  50. ifempty$task ) )
  51. {
  52.     cron_log'There is no task to execute yet.' );
  53. }
  54. else
  55. {
  56.     $ctsk_ID $task->ctsk_ID;
  57.     $ctsk_name $task->ctsk_name;
  58.  
  59.     cron_log'Requesting lock on task #'.$ctsk_ID.' ['.$ctsk_name.']' );
  60.  
  61.     $DB->halt_on_error false;
  62.     $DB->show_errors false;
  63.     $sql 'INSERT INTO T_cron__log( clog_ctsk_ID, clog_realstart_datetime, clog_status)
  64.                     VALUES( '.$ctsk_ID.', '.$DB->quotedate2mysql($localtimenow) ).', "started" )';
  65.     // Duplicate query for tests!
  66.     // $DB->query( $sql, 'Request lock' );
  67.     if$DB->query$sql'Request lock' != )
  68.     // This has no affected exactly ONE row: error! (probably locked -- duplicate key -- by a concurrent process)
  69.         $DB->show_errors true;
  70.         $DB->halt_on_error true;
  71.         cron_log'Could not lock. Task is probably handled by another process.' );
  72.     }
  73.     else
  74.     {
  75.         if!empty$task->ctsk_repeat_after ) )
  76.         {    // This task wants to be repeated:
  77.             // Note: we use the current time for 2 reasons: 1) prevent scheduling something in the past AND 2) introduce variety so that everyone doesn't run his repeated tasks at the same exact time, especially pings, pollings...
  78.             if$task->ctsk_controller == 'cron/_antispam_poll.job.php' )
  79.             {    // THIS IS A HACK. Guess why we need that!? :P  Please do not override or you'll kill our server :(
  80.                 $new_start_datetime $localtimenow rand4320086400 )// 12 to 24 hours
  81.             }
  82.             else
  83.             {    // Normal
  84.                 $new_start_datetime $localtimenow $task->ctsk_repeat_after;
  85.             }
  86.             $sql 'INSERT INTO T_cron__task( ctsk_start_datetime, ctsk_repeat_after, ctsk_name, ctsk_controller, ctsk_params )
  87.                             VALUES( '.$DB->quote(date2mysql($new_start_datetime)).', '.$DB->quote($task->ctsk_repeat_after).', '
  88.                                                 .$DB->quote($ctsk_name).', '.$DB->quote($task->ctsk_controller).', '.$DB->quote($task->ctsk_params).' )';
  89.             $DB->query$sql'Schedule repeated task.' );
  90.         }
  91.  
  92.         $DB->show_errors true;
  93.         $DB->halt_on_error true;
  94.         cron_log'Starting task #'.$ctsk_ID.' ['.$ctsk_name.'] at '.date'H:i:s'$localtimenow ).'.' );
  95.  
  96.         ifempty($task->ctsk_params) )
  97.         {
  98.             $cron_params array();
  99.         }
  100.         else
  101.         {
  102.             $cron_params unserialize$task->ctsk_params );
  103.         }
  104.  
  105.         // EXECUTE
  106.         call_job$task->ctsk_controller$cron_params );
  107.  
  108.         // Record task as finished:
  109.         ifempty($timestop) )
  110.         {
  111.             $timestop time($time_difference;
  112.         }
  113.         $sql ' UPDATE T_cron__log
  114.                                 SET clog_status = '.$DB->quote($result_status).',
  115.                                         clog_realstop_datetime = '.$DB->quotedate2mysql($timestop) ).',
  116.                                         clog_messages = '.$DB->quote($result_message/* May be NULL */.'
  117.                             WHERE clog_ctsk_ID = '.$ctsk_ID;
  118.         $DB->query$sql'Record task as finished.' );
  119.     }
  120. }
  121.  
  122.  
  123. //echo 'detecting timeouts...';
  124. // Detect timed out tasks:
  125. $sql ' UPDATE T_cron__log
  126.                         SET clog_status = "timeout"
  127.                     WHERE clog_status = "started"
  128.                                 AND clog_realstart_datetime < '.$DB->quotedate2mysqltime($time_difference $cron_timeout_delay ) );
  129. $DB->query$sql'Detect cron timeouts.' );
  130.  
  131.  
  132.  
  133. if$is_cli )
  134. // This is a web request:
  135.     echo '<p><a href="cron_exec.php">Refresh Now!</a></p>';
  136.     echo '<p>This page should refresh automatically in 15 seconds...</p>';
  137.     echo '<!-- This is invalid HTML but it is SOOOOOO helpful! (Delay will be triggered when we reach that point -->';
  138.     echo '<meta http-equiv="Refresh" content="15" />';
  139.  
  140.     debug_info();
  141.     ?>
  142.     </body>
  143.     </html>
  144.     <?php
  145. }
  146. ?>

Documentation generated on Tue, 18 Dec 2007 23:04:48 +0100 by phpDocumentor 1.4.0