You are here

public function Crontab::isBehind in Ultimate Cron 8.2

Check if job is behind schedule.

Parameters

\Drupal\ultimate_cron\Entity\CronJob $job: The job to check schedule for.

Return value

bool|int FALSE if job is behind its schedule or number of seconds behind.

Overrides SchedulerBase::isBehind

File

src/Plugin/ultimate_cron/Scheduler/Crontab.php, line 157

Class

Crontab
Crontab scheduler.

Namespace

Drupal\ultimate_cron\Plugin\ultimate_cron\Scheduler

Code

public function isBehind(CronJob $job) {

  // Disabled jobs are not behind!
  if (!$job
    ->status()) {
    return FALSE;
  }
  $log_entry = isset($job->log_entry) ? $job->log_entry : $job
    ->loadLatestLogEntry();

  // If job hasn't run yet, then who are we to say it's behind its schedule?
  // Check the registered time, and use that if it's available.
  $job_last_ran = $log_entry->start_time;
  if (!$job_last_ran) {
    $registered = \Drupal::config('ultimate_cron')
      ->get('ultimate_cron_hooks_registered');
    if (empty($registered[$job
      ->id()])) {
      return FALSE;
    }
    $job_last_ran = $registered[$job
      ->id()];
  }
  $skew = $this
    ->getSkew($job);
  $next_schedule = NULL;
  foreach ($this->configuration['rules'] as $rule) {
    $cron = CronRule::factory($rule, $job_last_ran, $skew);
    $time = $cron
      ->getNextSchedule();
    $next_schedule = is_null($next_schedule) || $time < $next_schedule ? $time : $next_schedule;
  }
  $behind = REQUEST_TIME - $next_schedule;
  return $behind > $this->configuration['catch_up'] ? $behind : FALSE;
}