You are here

protected function BaseUpdateRunner::runUpdate in Scheduled Updates 8

Run an individual update from the queue.

The update may involve multiple entities.

Parameters

\Drupal\scheduled_updates\ScheduledUpdateInterface $update:

array $entity_ids: Ids of entities that should be updated

$queue_item:

Return value

bool

1 call to BaseUpdateRunner::runUpdate()
BaseUpdateRunner::runUpdatesInQueue in src/Plugin/BaseUpdateRunner.php
Run all updates that are in the queue.

File

src/Plugin/BaseUpdateRunner.php, line 229
Contains \Drupal\scheduled_updates\Plugin\BaseUpdateRunner.

Class

BaseUpdateRunner

Namespace

Drupal\scheduled_updates\Plugin

Code

protected function runUpdate(ScheduledUpdateInterface $update, $entity_ids, $queue_item) {

  /** @var ContentEntityInterface[] $entities_to_update */
  $entities_to_update = $this
    ->loadEntitiesToUpdate($entity_ids);
  $invalid_entity_ids = [];
  foreach ($entities_to_update as $entity_id => $entity_to_update) {
    $this
      ->prepareEntityForUpdate($update, $queue_item, $entity_to_update);
    $this
      ->switchUser($update, $entity_to_update);
    $violations = $entity_to_update
      ->validate();
    if ($violations
      ->getEntityViolations()
      ->count() > 0) {
      $invalid_entity_ids[] = $entity_id;
      \Drupal::logger('scheduled_update')
        ->error("Entity update violation(s) for entity " . $entity_id);
      $violations
        ->filterByFieldAccess();

      // Flag entity level violations.
      foreach ($violations
        ->getEntityViolations() as $violation) {
        \Drupal::logger('scheduled_update')
          ->error((string) $violation);
      }
    }
    else {

      // Validation was successful.
      $entity_to_update
        ->save();
    }
    $this
      ->switchUserBack();
  }

  // @todo Should an update only be considered successful if all entities were updated correctly.
  // @todo Should all entities should be rolled back if 1 can't be updated? Add an option?
  $successful = empty($invalid_entity_ids);
  if (!$successful) {

    // At least 1 entity could not be updated.
    if ($this
      ->getInvalidUpdateBehavior() == UpdateRunnerInterface::INVALID_REQUEUE) {

      // We can't release the item now or it will be claimed again.
      $update->status = ScheduledUpdateInterface::STATUS_REQUEUED;
      if ($this->scheduled_update_type
        ->isEmbeddedType()) {
        $update
          ->setUpdateEntityIds($invalid_entity_ids);
      }
      $update
        ->save();
      $this
        ->addItemToRelease($queue_item);
    }
    elseif ($this
      ->getInvalidUpdateBehavior() == UpdateRunnerInterface::INVALID_ARCHIVE) {

      // @todo Should the successful entities be removed
      $update->status = ScheduledUpdateInterface::STATUS_UNSUCESSFUL;
      $update
        ->save();
      $this
        ->getQueue()
        ->deleteItem($queue_item);
    }
    else {
      $update
        ->delete();
      $this
        ->getQueue()
        ->deleteItem($queue_item);
    }
  }
  else {

    // There were no invalid entity updates
    if ($this
      ->getAfterRun() == UpdateRunnerInterface::AFTER_DELETE) {
      $update
        ->delete();
    }
    else {
      $update->status = ScheduledUpdateInterface::STATUS_SUCCESSFUL;
      $update
        ->save();
    }
    $this
      ->getQueue()
      ->deleteItem($queue_item);
  }
  return $successful;
}