You are here

private function QueueService::commitAdding in Purge 8.3

Commit all adding invalidations in the buffer to the queue.

2 calls to QueueService::commitAdding()
QueueService::claim in src/Plugin/Purge/Queue/QueueService.php
Claim invalidation objects from the queue.
QueueService::commit in src/Plugin/Purge/Queue/QueueService.php
Commit all actions in the internal buffer to the queue.

File

src/Plugin/Purge/Queue/QueueService.php, line 215

Class

QueueService
Provides the service that lets invalidations interact with a queue backend.

Namespace

Drupal\purge\Plugin\Purge\Queue

Code

private function commitAdding() {
  $items = $this->buffer
    ->getFiltered(TxBuffer::ADDING);
  if (!($items_count = count($items))) {
    return;
  }

  // Since we do have items to add, initialize the queue.
  $this
    ->initializeQueue();

  // Small anonymous function that fetches the 'data' field for createItem()
  // and createItemMultiple() - keeps queue plugins out of Purge specifics.
  $getProxiedData = function ($invalidation) {
    $proxy = new ProxyItem($invalidation, $this->buffer);
    return $proxy->data;
  };

  // Add just one item to the queue using createItem() on the queue.
  if ($items_count === 1) {
    $invalidation = current($items);
    if (!($id = $this->queue
      ->createItem($getProxiedData($invalidation)))) {
      throw new UnexpectedServiceConditionException("The queue returned FALSE on createItem().");
    }
    else {
      $this->buffer
        ->set($invalidation, TxBuffer::RELEASED);
      $this->buffer
        ->setProperty($invalidation, 'item_id', $id);
      $this->buffer
        ->setProperty($invalidation, 'created', time());
    }
  }
  else {
    $item_chunks = array_chunk($items, 1000);
    if ($item_chunks) {
      foreach ($item_chunks as $chunk) {
        $data_items = [];
        foreach ($chunk as $invalidation) {
          $data_items[] = $getProxiedData($invalidation);
        }
        if (!($ids = $this->queue
          ->createItemMultiple($data_items))) {
          throw new UnexpectedServiceConditionException("The queue returned FALSE on createItemMultiple().");
        }
        foreach ($chunk as $key => $invalidation) {
          $this->buffer
            ->set($invalidation, TxBuffer::ADDED);
          $this->buffer
            ->setProperty($invalidation, 'item_id', $ids[$key]);
          $this->buffer
            ->setProperty($invalidation, 'created', time());
        }
      }
    }
  }
  $this->purgeQueueStats
    ->updateTotals($items);
}