You are here

class BatchQueueMongodb in MongoDB 8

MongoDB batch queue implementation.

Hierarchy

Expanded class hierarchy of BatchQueueMongodb

File

src/BatchQueueMongodb.php, line 15
QueueMongodb functionality.

Namespace

Drupal\mongodb
View source
class BatchQueueMongodb extends QueueMongodb {

  /**
   * The object wrapping the MongoDB database object.
   *
   * @var MongoCollectionFactory
   */
  protected $mongo;

  /**
   * MongoDB collection name.
   *
   * @var string
   */
  protected $collection;

  /**
   * Construct this object.
   *
   * @param string $name
   *   Name of the queue.
   */
  public function __construct($name) {
    $this->collection = 'queue.' . $name;
  }

  /**
   * Claim an item in the queue for processing.
   *
   * @param string $lease_time
   *   How long the processing is expected to take in seconds,
   *
   * @return object/boolean
   *   On success we return an item object. If the queue is unable to claim
   *   an item it returns false.
   */
  public function claimItem($lease_time = 30) {
    $this
      ->garbageCollection();
    $result = $this
      ->mongoCollection()
      ->find([
      'expire' => 0,
    ])
      ->limit(1)
      ->sort([
      'created' => 1,
    ]);
    if ($result
      ->hasNext()) {
      $return = (object) $result
        ->getNext();
      $return->data = unserialize($return->data);
      return $return;
    }
    return FALSE;
  }

  /**
   * Retrieves all remaining items in the queue.
   *
   * This is specific to Batch API and is not part of the
   * \Drupal\Core\Queue\QueueInterface
   *
   * @return array
   *   An array of queue items.
   */
  public function getAllItems() {
    $result = [];
    $items = $this
      ->mongoCollection()
      ->find()
      ->sort([
      '_id' => 1,
    ]);
    foreach ($items as $item) {
      $result[] = unserialize($item['data']);
    }
    return $result;
  }

  /**
  @return \MongoCollection
  */
  protected function mongoCollection() {
    if (!isset($this->mongo)) {
      $this->mongo = \Drupal::service('mongo');
    }
    return $this->mongo
      ->get($this->collection);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchQueueMongodb::$collection protected property MongoDB collection name. Overrides QueueMongodb::$collection
BatchQueueMongodb::$mongo protected property The object wrapping the MongoDB database object. Overrides QueueMongodb::$mongo
BatchQueueMongodb::claimItem public function Claim an item in the queue for processing. Overrides QueueMongodb::claimItem
BatchQueueMongodb::getAllItems public function Retrieves all remaining items in the queue.
BatchQueueMongodb::mongoCollection protected function Overrides QueueMongodb::mongoCollection
BatchQueueMongodb::__construct public function Construct this object. Overrides QueueMongodb::__construct
QueueMongodb::createItem public function Add a queue item and store it directly to the queue. Overrides QueueInterface::createItem
QueueMongodb::createQueue public function Create a queue. Overrides QueueInterface::createQueue
QueueMongodb::deleteItem public function Delete a finished item from the queue. Overrides QueueInterface::deleteItem
QueueMongodb::deleteQueue public function Delete a queue and every item in the queue. Overrides QueueInterface::deleteQueue
QueueMongodb::garbageCollection protected function Automatically release items, that have been claimed and exceeded lease time.
QueueMongodb::numberOfItems public function Retrieve the number of items in the queue. Overrides QueueInterface::numberOfItems
QueueMongodb::releaseItem public function Release an item that the worker could not process Overrides QueueInterface::releaseItem