[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/cron/ -> cron_exec.php (source)

   1  <?php
   2  /**

   3   * Execute cron jobs.

   4   *

   5   * Example to use CLI:

   6   * >c:\php4\php cron_exec.php

   7   * >c:\php4\php-cli cron_exec.php

   8   *

   9   * @version $Id: cron_exec.php,v 1.14 2007/06/25 10:58:48 fplanque Exp $

  10   */
  11  
  12  
  13  /**

  14   * Include config

  15   */
  16  require_once dirname(__FILE__).'/../conf/_config.php';
  17  
  18  /**

  19   * @todo fp> This MIGHT be overkill. check...

  20   */
  21  require_once $inc_path .'_main.inc.php';
  22  
  23  /**

  24   * Cron support functions

  25   */
  26  load_funcs( '/cron/_cron.funcs.php' );
  27  
  28  $quiet = 0;
  29  if( $is_cli )
  30  { // called through Command Line Interface, handle args:
  31  
  32      if( isset( $_SERVER['argc'], $_SERVER['argv'] ) )
  33      {
  34          $argc = $_SERVER['argc'];
  35          $argv = $_SERVER['argv'];
  36      }
  37  
  38      if( isset($argv) )
  39      { // may not be set for CGI
  40          foreach( array_slice($argv, 1) as $v )
  41          {
  42              switch( $v )
  43              {
  44                  case '-h':
  45                  case '--help':
  46                      // display help:

  47                      echo $argv[0]." - Execute cron jobs for b2evolution\n";
  48                      echo "\n";
  49                      echo "Options:\n";
  50                      echo " -q --quiet: Be quiet (do not output a message, if there are no jobs).\n";
  51                      echo "             This is especially useful, when running as a cron job.\n";
  52                      exit(0);
  53                      break;
  54  
  55                  case '-q':
  56                  case '--quiet':
  57                      // increase quietness:

  58                      $quiet++;
  59                      break;
  60  
  61                  default:
  62                      echo 'Invalid option "'.$v.'". Use "-h" or "--help" for a list of options.'."\n";
  63                      exit(1);
  64              }
  65          }
  66      }
  67  }
  68  else
  69  { // This is a web request:
  70      ?>
  71      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  72      <html>
  73      <head>
  74          <title>Cron exec</title>
  75          <link rel="stylesheet" type="text/css" href="<?php echo $rsc_url ?>css/basic.css">
  76      </head>
  77      <body>
  78          <h1>Cron exec</h1>
  79          <p>This script will execute the next task in the cron queue.
  80          You should normally call it with the CLI (command line interface) version of PHP
  81          and automate that call through a cron.</p>
  82      <?php
  83  }
  84  
  85  /*

  86   * The following will feel a little bloated...

  87   * BUT it is actually a pretty nifty design to prevent double execution of tasks without any transaction!

  88   * The trick is to rely on the primary key of the cron__log table.

  89   */
  90  
  91  // Get next task to run in queue which has not started execution yet:

  92  $sql = 'SELECT *
  93                      FROM T_cron__task LEFT JOIN T_cron__log ON ctsk_ID = clog_ctsk_ID
  94                   WHERE clog_ctsk_ID IS NULL
  95                       AND ctsk_start_datetime <= '.$DB->quote( date2mysql($localtimenow) ).'
  96                   ORDER BY ctsk_start_datetime ASC, ctsk_ID ASC
  97                   LIMIT 1';
  98  $task = $DB->get_row( $sql, OBJECT, 0, 'Get next task to run in queue which has not started execution yet' );
  99  
 100  if( empty( $task ) )
 101  {
 102      if( $quiet < 1 )
 103      {
 104          cron_log( 'There is no task to execute yet.' );
 105      }
 106  }
 107  else
 108  {
 109      $ctsk_ID = $task->ctsk_ID;
 110      $ctsk_name = $task->ctsk_name;
 111  
 112      cron_log( 'Requesting lock on task #'.$ctsk_ID.' ['.$ctsk_name.']' );
 113  
 114      $DB->halt_on_error = false;
 115      $DB->show_errors = false;
 116      $sql = 'INSERT INTO T_cron__log( clog_ctsk_ID, clog_realstart_datetime, clog_status)
 117                      VALUES( '.$ctsk_ID.', '.$DB->quote( date2mysql($localtimenow) ).', "started" )';
 118      // Duplicate query for tests!

 119      // $DB->query( $sql, 'Request lock' );

 120      if( $DB->query( $sql, 'Request lock' ) != 1 )
 121      { // This has no affected exactly ONE row: error! (probably locked -- duplicate key -- by a concurrent process)
 122          $DB->show_errors = true;
 123          $DB->halt_on_error = true;
 124          cron_log( 'Could not lock. Task is probably handled by another process.' );
 125      }
 126      else
 127      {
 128          if( !empty( $task->ctsk_repeat_after ) )
 129          {    // This task wants to be repeated:
 130              // 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...

 131              if( $task->ctsk_controller == 'cron/_antispam_poll.job.php' )
 132              {    // THIS IS A HACK. Guess why we need that!? :P  Please do not override or you'll kill our server :(
 133                  $new_start_datetime = $localtimenow + rand( 43200, 86400 ); // 12 to 24 hours

 134              }
 135              else
 136              {    // Normal
 137                  $new_start_datetime = $localtimenow + $task->ctsk_repeat_after;
 138              }
 139              $sql = 'INSERT INTO T_cron__task( ctsk_start_datetime, ctsk_repeat_after, ctsk_name, ctsk_controller, ctsk_params )
 140                              VALUES( '.$DB->quote(date2mysql($new_start_datetime)).', '.$DB->quote($task->ctsk_repeat_after).', '
 141                                                  .$DB->quote($ctsk_name).', '.$DB->quote($task->ctsk_controller).', '.$DB->quote($task->ctsk_params).' )';
 142              $DB->query( $sql, 'Schedule repeated task.' );
 143          }
 144  
 145          $DB->show_errors = true;
 146          $DB->halt_on_error = true;
 147          cron_log( 'Starting task #'.$ctsk_ID.' ['.$ctsk_name.'] at '.date( 'H:i:s', $localtimenow ).'.' );
 148  
 149          if( empty($task->ctsk_params) )
 150          {
 151              $cron_params = array();
 152          }
 153          else
 154          {
 155              $cron_params = unserialize( $task->ctsk_params );
 156          }
 157  
 158          // The job may need to know its ID (to set logical locks for example):

 159          $cron_params['ctsk_ID'] = $ctsk_ID;
 160  
 161          // EXECUTE

 162          call_job( $task->ctsk_controller, $cron_params );
 163  
 164          // Record task as finished:

 165          if( empty($timestop) )
 166          {
 167              $timestop = time() + $time_difference;
 168          }
 169          $sql = ' UPDATE T_cron__log
 170                                  SET clog_status = '.$DB->quote($result_status).',
 171                                          clog_realstop_datetime = '.$DB->quote( date2mysql($timestop) ).',
 172                                          clog_messages = '.$DB->quote($result_message) /* May be NULL */.'
 173                              WHERE clog_ctsk_ID = '.$ctsk_ID;
 174          $DB->query( $sql, 'Record task as finished.' );
 175      }
 176  }
 177  
 178  
 179  //echo 'detecting timeouts...';

 180  // Detect timed out tasks:

 181  $sql = ' UPDATE T_cron__log
 182                          SET clog_status = "timeout"
 183                      WHERE clog_status = "started"
 184                                  AND clog_realstart_datetime < '.$DB->quote( date2mysql( time() + $time_difference - $cron_timeout_delay ) );
 185  $DB->query( $sql, 'Detect cron timeouts.' );
 186  
 187  
 188  
 189  if( ! $is_cli )
 190  { // This is a web request:
 191      echo '<p><a href="cron_exec.php">Refresh Now!</a></p>';
 192      echo '<p>This page should refresh automatically in 15 seconds...</p>';
 193      echo '<!-- This is invalid HTML but it is SOOOOOO helpful! (Delay will be triggered when we reach that point -->';
 194      echo '<meta http-equiv="Refresh" content="15" />';
 195  
 196      debug_info();
 197      ?>
 198      </body>
 199      </html>
 200      <?php
 201  }
 202  
 203  /*

 204   * $Log: cron_exec.php,v $

 205   * Revision 1.14  2007/06/25 10:58:48  fplanque

 206   * MODULES (refactored MVC)

 207   *

 208   */
 209  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics