You are here

class ScheduledTransitionsJobs in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x src/ScheduledTransitionsJobs.php \Drupal\scheduled_transitions\ScheduledTransitionsJobs

Job runner for Scheduled Transitions.

Hierarchy

Expanded class hierarchy of ScheduledTransitionsJobs

1 string reference to 'ScheduledTransitionsJobs'
scheduled_transitions.services.yml in ./scheduled_transitions.services.yml
scheduled_transitions.services.yml
1 service uses ScheduledTransitionsJobs
scheduled_transitions.jobs in ./scheduled_transitions.services.yml
Drupal\scheduled_transitions\ScheduledTransitionsJobs

File

src/ScheduledTransitionsJobs.php, line 16

Namespace

Drupal\scheduled_transitions
View source
class ScheduledTransitionsJobs implements ScheduledTransitionsJobsInterface {

  /**
   * Duration a scheduled transition should be locked from adding to queue.
   */
  protected const LOCK_DURATION = 1800;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * System time.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * The scheduled transition job queue.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a new ScheduledTransitionsRunner.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   System time.
   * @param \Drupal\Core\Queue\QueueFactory $queueFactory
   *   The queue factory.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, TimeInterface $time, QueueFactory $queueFactory, LoggerInterface $logger) {
    $this->entityTypeManager = $entityTypeManager;
    $this->time = $time;
    $this->queue = $queueFactory
      ->get('scheduled_transition_job');
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  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(),
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ScheduledTransitionsJobs::$entityTypeManager protected property The entity type manager.
ScheduledTransitionsJobs::$logger protected property A logger instance.
ScheduledTransitionsJobs::$queue protected property The scheduled transition job queue.
ScheduledTransitionsJobs::$time protected property System time.
ScheduledTransitionsJobs::jobCreator public function Checks for transitions needing to be run, and adds them to a queue. Overrides ScheduledTransitionsJobsInterface::jobCreator
ScheduledTransitionsJobs::LOCK_DURATION protected constant Duration a scheduled transition should be locked from adding to queue.
ScheduledTransitionsJobs::__construct public function Constructs a new ScheduledTransitionsRunner.