You are here

public function DatabaseQueue::delayItem in Drupal 9

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Queue/DatabaseQueue.php \Drupal\Core\Queue\DatabaseQueue::delayItem()

Delay an item so it runs in the future.

Parameters

object $item: The item returned by \Drupal\Core\Queue\QueueInterface::claimItem().

int $delay: A delay before the item's lock should expire (in seconds). Relative to the current time, not the item's current expiry.

Return value

bool TRUE if the item has been updated, FALSE otherwise.

Throws

\InvalidArgumentException When a negative $delay is provided; $delay must be non-negative.

To immediately release an item without delay.

Overrides DelayableQueueInterface::delayItem

See also

\Drupal\Core\Queue\QueueInterface::releaseItem()

File

core/lib/Drupal/Core/Queue/DatabaseQueue.php, line 177

Class

DatabaseQueue
Default queue implementation.

Namespace

Drupal\Core\Queue

Code

public function delayItem($item, int $delay) {

  // Only allow a positive delay interval.
  if ($delay < 0) {
    throw new \InvalidArgumentException('$delay must be non-negative');
  }
  try {

    // Add the delay relative to the current time.
    $expire = \Drupal::time()
      ->getCurrentTime() + $delay;

    // Update the expiry time of this item.
    $update = $this->connection
      ->update(static::TABLE_NAME)
      ->fields([
      'expire' => $expire,
    ])
      ->condition('item_id', $item->item_id);
    return (bool) $update
      ->execute();
  } catch (\Exception $e) {
    $this
      ->catchException($e);

    // If the table doesn't exist we should consider the item nonexistent.
    return TRUE;
  }
}