You are here

public function CronJob::run in Ultimate Cron 8.2

Run job.

Parameters

string $init_message: (optional) The launch message. If left NULL, a default message will be displayed.

Return value

bool TRUE if the job is ran, FALSE otherwise.

Overrides CronJobInterface::run

File

src/Entity/CronJob.php, line 448

Class

CronJob
Class for handling cron jobs.

Namespace

Drupal\ultimate_cron\Entity

Code

public function run($init_message = NULL) {
  if (!$init_message) {
    $init_message = t('Launched manually');
  }
  $lock_id = $this
    ->lock();
  if (!$lock_id) {
    return FALSE;
  }
  $log_entry = $this
    ->startLog($lock_id, $init_message);
  $accountSwitcher = \Drupal::service('account_switcher');
  try {
    $this
      ->clearSignals();
    $this
      ->initializeProgress();
    \Drupal::moduleHandler()
      ->invokeAll('cron_pre_run', array(
      $this,
    ));

    // Force the current user to anonymous to ensure consistent permissions
    // on cron runs.
    $accountSwitcher
      ->switchTo(new AnonymousUserSession());
    self::$currentJob = $this;
    $this
      ->invokeCallback();
  } catch (\Error $e) {

    // PHP 7 throws Error objects in case of a fatal error. It will also call
    // the finally block below and close the log entry. Because of that,
    // the global fatal error catching will not work and we have to log it
    // explicitly here instead. The advantage is that this will not
    // interrupt the process.
    $variables = Error::decodeException($e);
    unset($variables['backtrace']);
    $log_entry
      ->log('%type: @message in %function (line %line of %file).', $variables, RfcLogLevel::ERROR);
    return FALSE;
  } catch (\Exception $e) {
    $variables = Error::decodeException($e);
    unset($variables['backtrace']);
    $log_entry
      ->log('%type: @message in %function (line %line of %file).', $variables, RfcLogLevel::ERROR);
    return FALSE;
  } finally {
    self::$currentJob = NULL;
    \Drupal::moduleHandler()
      ->invokeAll('cron_post_run', array(
      $this,
    ));
    $this
      ->finishProgress();

    // Restore original user account.
    $accountSwitcher
      ->switchBack();
    $log_entry
      ->finish();
    $this
      ->unlock($lock_id);
  }
  return TRUE;
}