class BatchMemory extends Memory (View source)

Defines a batch queue handler used by the Batch API for non-progressive batches.

This implementation:

  • Ensures FIFO ordering.
  • Allows an item to be repeatedly claimed until it is actually deleted (no notion of lease time or 'expire' date), to allow multipass operations.

Properties

protected array $queue

The queue data.

from  Memory
protected int $idSequence

Counter for item ids.

from  Memory

Methods

__construct(string $name)

Constructs a Memory object.

from  Memory
A
createItem($data)

Adds a queue item and store it directly to the queue.

from  Memory
int
numberOfItems()

Retrieves the number of items in the queue.

from  Memory
On
claimItem($lease_time = 0)

Overrides \Drupal\Core\Queue\Memory::claimItem().

deleteItem($item)

Deletes a finished item from the queue.

from  Memory
bool
releaseItem($item)

Releases an item that the worker could not process.

from  Memory
createQueue()

Creates a queue.

from  Memory
deleteQueue()

Deletes a queue and every item in the queue.

from  Memory
array
getAllItems()

Retrieves all remaining items in the queue.

Details

__construct(string $name)

Constructs a Memory object.

Parameters

string $name

An arbitrary string. The name of the queue to work with.

A createItem($data)

Adds a queue item and store it directly to the queue.

Parameters

$data

Arbitrary data to be associated with the new task in the queue.

Return Value

A

unique ID if the item was successfully created and was (best effort) added to the queue, otherwise FALSE. We don't guarantee the item was committed to disk etc, but as far as we know, the item is now in the queue.

int numberOfItems()

Retrieves the number of items in the queue.

This is intended to provide a "best guess" count of the number of items in the queue. Depending on the implementation and the setup, the accuracy of the results of this function may vary.

e.g. On a busy system with a large number of consumers and items, the result might only be valid for a fraction of a second and not provide an accurate representation.

Return Value

int

An integer estimate of the number of items in the queue.

On claimItem($lease_time = 0)

Overrides \Drupal\Core\Queue\Memory::claimItem().

Unlike \Drupal\Core\Queue\Memory::claimItem(), this method provides a default lease time of 0 (no expiration) instead of 30. This allows the item to be claimed repeatedly until it is deleted.

Parameters

$lease_time

How long the processing is expected to take in seconds, defaults to an hour. After this lease expires, the item will be reset and another consumer can claim the item. For idempotent tasks (which can be run multiple times without side effects), shorter lease times would result in lower latency in case a consumer fails. For tasks that should not be run more than once (non-idempotent), a larger lease time will make it more rare for a given task to run multiple times in cases of failure, at the cost of higher latency.

Return Value

On

success we return an item object. If the queue is unable to claim an item it returns false. This implies a best effort to retrieve an item and either the queue is empty or there is some other non-recoverable problem.

If returned, the object will have at least the following properties:

  • data: the same as what passed into createItem().
  • item_id: the unique ID returned from createItem().
  • created: timestamp when the item was put into the queue.

deleteItem($item)

Deletes a finished item from the queue.

Parameters

$item

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

bool releaseItem($item)

Releases an item that the worker could not process.

Another worker can come in and process it before the timeout expires.

Parameters

$item

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

Return Value

bool

TRUE if the item has been released, FALSE otherwise.

createQueue()

Creates a queue.

Called during installation and should be used to perform any necessary initialization operations. This should not be confused with the constructor for these objects, which is called every time an object is instantiated to operate on a queue. This operation is only needed the first time a given queue is going to be initialized (for example, to make a new database table or directory to hold tasks for the queue -- it depends on the queue implementation if this is necessary at all).

deleteQueue()

Deletes a queue and every item in the queue.

array getAllItems()

Retrieves all remaining items in the queue.

This is specific to Batch API and is not part of the \Drupal\Core\Queue\QueueInterface.

Return Value

array

An array of queue items.