You are here

public function QueueService::claim in Purge 8.3

Claim invalidation objects from the queue.

Parameters

int $claims: Determines how many claims should be taken from the queue. When the queue has less items available, less will be returned. When this parameter is left as NULL, CapacityTrackerInterface::getRemainingInvalidationsLimit() will be used as input.

int $lease_time: The expected (maximum) time needed per claim, which will get multiplied for you by the number of claims you request. When this is left NULL, this value comes from CapacityTrackerInterface::getTimeHint().

After the lease_time expires, another running request or CLI process can also claim the items and process them, therefore too short lease times are dangerous as it could lead to double processing.

Return value

\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface[]|array Returned will be a non-associative array with the given amount of invalidation objects as claimed. Be aware that it can be expected that the claimed invalidations will need to be processed by the purger within the given $lease_time, else they will become available again. The returned array is empty when the queue is.

Overrides QueueServiceInterface::claim

File

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

Class

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

Namespace

Drupal\purge\Plugin\Purge\Queue

Code

public function claim($claims = NULL, $lease_time = NULL) {
  $this
    ->commitAdding();
  $this
    ->commitReleasing();
  $this
    ->commitDeleting();

  // When the claim number or lease_time isn't passed, the capacity tracker
  // will kindly give it to us. Then multiply the lease time with the claims.
  $tracker = $this->purgePurgers
    ->capacityTracker();
  if (is_null($claims)) {
    if (!($claims = $tracker
      ->getRemainingInvalidationsLimit())) {
      $this->logger
        ->debug("no more items can be claimed within this request.");
      return [];
    }
  }
  if (is_null($lease_time)) {
    $lease_time = $tracker
      ->getLeaseTimeHint($claims);
  }
  else {
    $lease_time = $claims * $lease_time;
  }

  // Claim one or several items out of the queue or finish the call.
  $this
    ->initializeQueue();
  if ($claims === 1) {
    if (!($item = $this->queue
      ->claimItem($lease_time))) {
      $this->logger
        ->debug("attempt to claim 1 item failed.");
      return [];
    }
    $items = [
      $item,
    ];
  }
  elseif (!($items = $this->queue
    ->claimItemMultiple($claims, $lease_time))) {
    $this->logger
      ->debug("attempt to claim @no items failed.", [
      '@no' => $claims,
    ]);
    return [];
  }

  // Iterate the $items array and replace each with full instances.
  foreach ($items as $i => $item) {

    // See if the inv. object is still buffered locally, else instantiate it.
    if (!($inv = $this->buffer
      ->getByProperty('item_id', $item->item_id))) {
      $inv = $this->purgeInvalidationFactory
        ->getFromQueueData($item->data);
    }

    // Ensure it is buffered, has the right state and properties, then add it.
    $this->buffer
      ->set($inv, TxBuffer::CLAIMED);
    $this->buffer
      ->setProperty($inv, 'item_id', $item->item_id);
    $this->buffer
      ->setProperty($inv, 'created', $item->created);
    $items[$i] = $inv;
  }
  $this->logger
    ->debug("claimed @no items.", [
    '@no' => count($items),
  ]);
  return $items;
}