You are here

public function ScheduledTransitionsJobs::jobCreator in Scheduled Transitions 2.x

Same name and namespace in other branches
  1. 8 src/ScheduledTransitionsJobs.php \Drupal\scheduled_transitions\ScheduledTransitionsJobs::jobCreator()

Checks for transitions needing to be run, and adds them to a queue.

Overrides ScheduledTransitionsJobsInterface::jobCreator

File

src/ScheduledTransitionsJobs.php, line 73

Class

ScheduledTransitionsJobs
Job runner for Scheduled Transitions.

Namespace

Drupal\scheduled_transitions

Code

public function jobCreator() : void {
  $transitionStorage = $this->entityTypeManager
    ->getStorage('scheduled_transition');
  $now = $this->time
    ->getRequestTime();
  $query = $transitionStorage
    ->getQuery();
  $query
    ->condition('transition_on', $now, '<=');
  $or = $query
    ->orConditionGroup()
    ->condition('locked_on', NULL, 'IS NULL')
    ->condition('locked_on', $now - static::LOCK_DURATION, '>=');
  $query
    ->condition($or);
  $ids = $query
    ->execute();

  /** @var \Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface[] $transitions */
  $transitions = $transitionStorage
    ->loadMultiple($ids);
  foreach ($transitions as $transition) {
    $transition
      ->setLockedOn($now)
      ->save();
    $queueItem = [];
    $queueItem[ScheduledTransitionJob::SCHEDULED_TRANSITION_ID] = $transition
      ->id();
    $this->queue
      ->createItem($queueItem);
    $this->logger
      ->info('Created scheduled transition job for #@id', [
      '@id' => $transition
        ->id(),
    ]);
  }
}