QueueInterface.php 3.71 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Drupal\Core\Queue;

/**
 * Interface for a queue.
 *
 * Classes implementing this interface will do a best effort to preserve order
 * in messages and to execute them at least once.
10 11
 *
 * @ingroup queue
12 13 14 15 16 17 18 19 20 21
 */
interface QueueInterface {

  /**
   * Adds a queue item and store it directly to the queue.
   *
   * @param $data
   *   Arbitrary data to be associated with the new task in the queue.
   *
   * @return
22 23
   *   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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   *   committed to disk etc, but as far as we know, the item is now in the
   *   queue.
   */
  public function createItem($data);

  /**
   * 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.
   *
40
   * @return int
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   *   An integer estimate of the number of items in the queue.
   */
  public function numberOfItems();

  /**
   * Claims an item in the queue for processing.
   *
   * @param $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
   *   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.
63 64 65 66 67
   *
   *   If returned, the object will have at least the following properties:
   *   - data: the same as what what passed into createItem().
   *   - item_id: the unique ID returned from createItem().
   *   - created: timestamp when the item was put into the queue.
68 69 70 71 72 73 74
   */
  public function claimItem($lease_time = 3600);

  /**
   * Deletes a finished item from the queue.
   *
   * @param $item
75
   *   The item returned by \Drupal\Core\Queue\QueueInterface::claimItem().
76 77 78 79 80 81 82 83 84
   */
  public function deleteItem($item);

  /**
   * Releases an item that the worker could not process.
   *
   * Another worker can come in and process it before the timeout expires.
   *
   * @param $item
85
   *   The item returned by \Drupal\Core\Queue\QueueInterface::claimItem().
86
   *
87
   * @return bool
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
   *   TRUE if the item has been released, FALSE otherwise.
   */
  public function releaseItem($item);

  /**
   * 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).
   */
  public function createQueue();

  /**
   * Deletes a queue and every item in the queue.
   */
  public function deleteQueue();
109

110
}