function QueueProcessTrait::processItem

Process a queue item.

Parameters

\Drupal\Core\Queue\QueueInterface $queue: The queue.

\Drupal\Core\Queue\QueueWorkerInterface $worker: The queue worker.

Return value

bool TRUE if an item was claimed, FALSE if not.

Throws

\Drupal\Core\Queue\SuspendQueueException If the queue was suspended.

2 calls to QueueProcessTrait::processItem()
Cron::processQueue in core/lib/Drupal/Core/Cron.php
Processes a cron queue.
InstantQueueRunner::destruct in core/lib/Drupal/Core/Queue/InstantQueueRunner.php
Performs destruct operations.

File

core/lib/Drupal/Core/Queue/QueueProcessTrait.php, line 28

Class

QueueProcessTrait
Provides a trait for processing queue items.

Namespace

Drupal\Core\Queue

Code

protected function processItem(QueueInterface $queue, QueueWorkerInterface $worker) : bool {
  $claimed = FALSE;
  $lease_time = $worker->getPluginDefinition()['cron']['time'] ?? NULL;
  if ($item = $lease_time === NULL ? $queue->claimItem() : $queue->claimItem($lease_time)) {
    $claimed = TRUE;
    try {
      $worker->processItem($item->data);
      $queue->deleteItem($item);
    } catch (DelayedRequeueException $e) {
      // The worker requested the task not be immediately re-queued.
      // - If the queue doesn't support ::delayItem(), we should leave the
      // item's current expiry time alone.
      // - If the queue does support ::delayItem(), we should allow the
      // queue to update the item's expiry using the requested delay.
      if ($queue instanceof DelayableQueueInterface) {
        // This queue can handle a custom delay; use the duration provided
        // by the exception.
        $queue->delayItem($item, $e->getDelay());
      }
    } catch (RequeueException) {
      // The worker requested the task be immediately requeued.
      $queue->releaseItem($item);
    } catch (SuspendQueueException $e) {
      // If the worker indicates the whole queue should be skipped, release
      // the item and go to the next queue.
      $queue->releaseItem($item);
      $this->logger
        ->debug('A worker for @queue queue suspended further processing of the queue.', [
        '@queue' => $worker->getPluginId(),
      ]);
      // Skip to the next queue.
      throw $e;
    } catch (\Throwable $e) {
      // In case of any other kind of exception, log it and leave the item
      // in the queue to be processed again later.
      Error::logException($this->logger, $e);
    }
  }
  return $claimed;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.