function drupal_cron_run

Executes a cron run when called.

Do not call this function from a test. Use $this->cronRun() instead.

Return value

TRUE if cron ran successfully.

8 calls to drupal_cron_run()
CronController::run in drupal/core/modules/system/lib/Drupal/system/CronController.php
Run Cron once.
CronController::runManually in drupal/core/modules/system/lib/Drupal/system/CronController.php
Run cron manually.
CronForm::submitCron in drupal/core/modules/system/lib/Drupal/system/Form/CronForm.php
Runs cron and reloads the page.
DeleteTest::testInUse in drupal/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php
Tries deleting a file that is in use.
FileFieldRevisionTest::testRevisions in drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php
Tests creating multiple revisions of a node and managing attached files.

... See full list

File

drupal/core/includes/common.inc, line 4197
Common functions that many Drupal modules will need to reference.

Code

function drupal_cron_run() {

  // Allow execution to continue even if the request gets canceled.
  @ignore_user_abort(TRUE);

  // Prevent session information from being saved while cron is running.
  $original_session_saving = drupal_save_session();
  drupal_save_session(FALSE);

  // Force the current user to anonymous to ensure consistent permissions on
  // cron runs.
  $original_user = $GLOBALS['user'];
  $GLOBALS['user'] = drupal_anonymous_user();

  // Try to allocate enough time to run all the hook_cron implementations.
  drupal_set_time_limit(240);
  $return = FALSE;

  // Grab the defined cron queues.
  $queues = module_invoke_all('queue_info');
  drupal_alter('queue_info', $queues);

  // Try to acquire cron lock.
  if (!lock()
    ->acquire('cron', 240.0)) {

    // Cron is still running normally.
    watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
  }
  else {

    // Make sure every queue exists. There is no harm in trying to recreate an
    // existing queue.
    foreach ($queues as $queue_name => $info) {
      if (isset($info['cron'])) {
        Drupal::queue($queue_name)
          ->createQueue();
      }
    }

    // Iterate through the modules calling their cron handlers (if any):
    foreach (module_implements('cron') as $module) {

      // Do not let an exception thrown by one module disturb another.
      try {
        module_invoke($module, 'cron');
      } catch (Exception $e) {
        watchdog_exception('cron', $e);
      }
    }

    // Record cron time.
    Drupal::state()
      ->set('system.cron_last', REQUEST_TIME);
    watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);

    // Release cron lock.
    lock()
      ->release('cron');

    // Return TRUE so other functions can check if it did run successfully
    $return = TRUE;
  }
  foreach ($queues as $queue_name => $info) {
    if (isset($info['cron'])) {
      $callback = $info['worker callback'];
      $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
      $queue = Drupal::queue($queue_name);
      while (time() < $end && ($item = $queue
        ->claimItem())) {
        call_user_func_array($callback, array(
          $item->data,
        ));
        $queue
          ->deleteItem($item);
      }
    }
  }

  // Restore the user.
  $GLOBALS['user'] = $original_user;
  drupal_save_session($original_session_saving);
  return $return;
}