You are here

public function DatabaseQueue::claimItem in Purge 8.3

@todo \Drupal\Core\Queue\DatabaseQueue::claimItem() doesn't included expired items in its query which means that its essentially broken and makes our tests fail. Therefore we overload the implementation with one that does it accurately. However, this should flow back to core.

Overrides DatabaseQueue::claimItem

File

src/Plugin/Purge/Queue/DatabaseQueue.php, line 122

Class

DatabaseQueue
A QueueInterface compliant database backed queue.

Namespace

Drupal\purge\Plugin\Purge\Queue

Code

public function claimItem($lease_time = 3600) {

  // Claim an item by updating its expire fields. If claim is not successful
  // another thread may have claimed the item in the meantime. Therefore loop
  // until an item is successfully claimed or we are reasonably sure there
  // are no unclaimed items left.
  while (TRUE) {
    $conditions = [
      ':now' => time(),
    ];
    $item = $this->connection
      ->queryRange('SELECT * FROM {' . static::TABLE_NAME . '} q WHERE ((expire = 0) OR (:now > expire)) ORDER BY created, item_id ASC', 0, 1, $conditions)
      ->fetchObject();
    if ($item) {
      $item->item_id = (int) $item->item_id;
      $item->expire = (int) $item->expire;

      // Try to update the item. Only one thread can succeed in UPDATEing the
      // same row. We cannot rely on REQUEST_TIME because items might be
      // claimed by a single consumer which runs longer than 1 second. If we
      // continue to use REQUEST_TIME instead of the current time(), we steal
      // time from the lease, and will tend to reset items before the lease
      // should really expire.
      $update = $this->connection
        ->update(static::TABLE_NAME)
        ->fields([
        'expire' => time() + $lease_time,
      ])
        ->condition('item_id', $item->item_id);

      // If there are affected rows, this update succeeded.
      if ($update
        ->execute()) {
        $item->data = unserialize($item->data);
        return $item;
      }
    }
    else {

      // No items currently available to claim.
      return FALSE;
    }
  }
}