QueueInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Drupal\Core\Queue;
  3. /**
  4. * Interface for a queue.
  5. *
  6. * Classes implementing this interface will do a best effort to preserve order
  7. * in messages and to execute them at least once.
  8. *
  9. * @ingroup queue
  10. */
  11. interface QueueInterface {
  12. /**
  13. * Adds a queue item and store it directly to the queue.
  14. *
  15. * @param $data
  16. * Arbitrary data to be associated with the new task in the queue.
  17. *
  18. * @return
  19. * A unique ID if the item was successfully created and was (best effort)
  20. * added to the queue, otherwise FALSE. We don't guarantee the item was
  21. * committed to disk etc, but as far as we know, the item is now in the
  22. * queue.
  23. */
  24. public function createItem($data);
  25. /**
  26. * Retrieves the number of items in the queue.
  27. *
  28. * This is intended to provide a "best guess" count of the number of items in
  29. * the queue. Depending on the implementation and the setup, the accuracy of
  30. * the results of this function may vary.
  31. *
  32. * e.g. On a busy system with a large number of consumers and items, the
  33. * result might only be valid for a fraction of a second and not provide an
  34. * accurate representation.
  35. *
  36. * @return int
  37. * An integer estimate of the number of items in the queue.
  38. */
  39. public function numberOfItems();
  40. /**
  41. * Claims an item in the queue for processing.
  42. *
  43. * @param $lease_time
  44. * How long the processing is expected to take in seconds, defaults to an
  45. * hour. After this lease expires, the item will be reset and another
  46. * consumer can claim the item. For idempotent tasks (which can be run
  47. * multiple times without side effects), shorter lease times would result
  48. * in lower latency in case a consumer fails. For tasks that should not be
  49. * run more than once (non-idempotent), a larger lease time will make it
  50. * more rare for a given task to run multiple times in cases of failure,
  51. * at the cost of higher latency.
  52. *
  53. * @return
  54. * On success we return an item object. If the queue is unable to claim an
  55. * item it returns false. This implies a best effort to retrieve an item
  56. * and either the queue is empty or there is some other non-recoverable
  57. * problem.
  58. *
  59. * If returned, the object will have at least the following properties:
  60. * - data: the same as what what passed into createItem().
  61. * - item_id: the unique ID returned from createItem().
  62. * - created: timestamp when the item was put into the queue.
  63. */
  64. public function claimItem($lease_time = 3600);
  65. /**
  66. * Deletes a finished item from the queue.
  67. *
  68. * @param $item
  69. * The item returned by \Drupal\Core\Queue\QueueInterface::claimItem().
  70. */
  71. public function deleteItem($item);
  72. /**
  73. * Releases an item that the worker could not process.
  74. *
  75. * Another worker can come in and process it before the timeout expires.
  76. *
  77. * @param $item
  78. * The item returned by \Drupal\Core\Queue\QueueInterface::claimItem().
  79. *
  80. * @return bool
  81. * TRUE if the item has been released, FALSE otherwise.
  82. */
  83. public function releaseItem($item);
  84. /**
  85. * Creates a queue.
  86. *
  87. * Called during installation and should be used to perform any necessary
  88. * initialization operations. This should not be confused with the
  89. * constructor for these objects, which is called every time an object is
  90. * instantiated to operate on a queue. This operation is only needed the
  91. * first time a given queue is going to be initialized (for example, to make
  92. * a new database table or directory to hold tasks for the queue -- it
  93. * depends on the queue implementation if this is necessary at all).
  94. */
  95. public function createQueue();
  96. /**
  97. * Deletes a queue and every item in the queue.
  98. */
  99. public function deleteQueue();
  100. }