system_run_cron_image

Versions
7
system_run_cron_image()

Menu callback; executes cron via an image callback.

This callback runs cron in a separate HTTP request to prevent "mysterious" slow-downs of regular HTTP requests. It is either invoked via an AJAX request (if the client's browser supports JavaScript) or by an IMG tag directly in the page output (for clients not supporting JavaScript). For the latter case, we need to output a transparent 1x1 image, so the browser does not render the image's alternate text or a 'missing image placeholder'. The AJAX request does not process the returned output.

See also

system_page_alter()

@see theme_system_run_cron_image()

See also

system_run_cron_image_access()

Code

modules/system/system.module, line 3113

<?php
function system_run_cron_image() {
  drupal_page_is_cacheable(FALSE);

  // Output a transparent 1x1 image to the browser; required for clients not
  // supporting JavaScript.
  drupal_add_http_header('Content-Type', 'image/gif');
  echo "\x47\x49\x46\x38\x39\x61\x1\x0\x1\x0\x80\xff\x0\xc0\xc0\xc0\x0\x0\x0\x21\xf9\x4\x1\x0\x0\x0\x0\x2c\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b";

  // Cron threshold semaphore is used to avoid errors every time the image
  // callback is displayed when a previous cron is still running.
  $threshold_semaphore = variable_get('cron_threshold_semaphore', FALSE);
  if ($threshold_semaphore) {
    if (REQUEST_TIME - $threshold_semaphore > 3600) {
      // Either cron has been running for more than an hour or the semaphore
      // was not reset due to a database error.
      watchdog('cron', 'Cron has been running for more than an hour and is most likely stuck.', array(), WATCHDOG_ERROR);

      // Release the cron threshold semaphore.
      variable_del('cron_threshold_semaphore');
    }
  }
  else {
    // Run cron automatically if it has never run or threshold was crossed.
    $cron_last = variable_get('cron_last', NULL);
    $cron_threshold = variable_get('cron_safe_threshold', DRUPAL_CRON_DEFAULT_THRESHOLD);
    if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $cron_threshold)) {
      // Lock cron threshold semaphore.
      variable_set('cron_threshold_semaphore', REQUEST_TIME);
      drupal_cron_run();
      // Release the cron threshold semaphore.
      variable_del('cron_threshold_semaphore');
    }
  }

  drupal_exit();
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.