function Cron::processQueue

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Cron.php \Drupal\Core\Cron::processQueue()

Processes a cron queue.

Parameters

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

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

Throws

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

1 call to Cron::processQueue()
Cron::processQueues in core/lib/Drupal/Core/Cron.php
Processes cron queues.

File

core/lib/Drupal/Core/Cron.php, line 265

Class

Cron
The Drupal core Cron service.

Namespace

Drupal\Core

Code

protected function processQueue(QueueInterface $queue, QueueWorkerInterface $worker) {
  $lease_time = $worker->getPluginDefinition()['cron']['time'];
  $end = $this->time
    ->getCurrentTime() + $lease_time;
  while ($this->time
    ->getCurrentTime() < $end && ($item = $queue->claimItem($lease_time))) {
    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 (\Exception $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);
    }
  }
}

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