Batch.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Drupal\Core\Queue;
  3. /**
  4. * Defines a batch queue handler used by the Batch API.
  5. *
  6. * This implementation:
  7. * - Ensures FIFO ordering.
  8. * - Allows an item to be repeatedly claimed until it is actually deleted (no
  9. * notion of lease time or 'expire' date), to allow multipass operations.
  10. *
  11. * Stale items from failed batches are cleaned from the {queue} table on cron
  12. * using the 'created' date.
  13. *
  14. * @ingroup queue
  15. */
  16. class Batch extends DatabaseQueue {
  17. /**
  18. * Overrides \Drupal\Core\Queue\DatabaseQueue::claimItem().
  19. *
  20. * Unlike \Drupal\Core\Queue\DatabaseQueue::claimItem(), this method provides
  21. * a default lease time of 0 (no expiration) instead of 30. This allows the
  22. * item to be claimed repeatedly until it is deleted.
  23. */
  24. public function claimItem($lease_time = 0) {
  25. try {
  26. $item = $this->connection->queryRange('SELECT data, item_id FROM {queue} q WHERE name = :name ORDER BY item_id ASC', 0, 1, [':name' => $this->name])->fetchObject();
  27. if ($item) {
  28. $item->data = unserialize($item->data);
  29. return $item;
  30. }
  31. }
  32. catch (\Exception $e) {
  33. $this->catchException($e);
  34. }
  35. return FALSE;
  36. }
  37. /**
  38. * Retrieves all remaining items in the queue.
  39. *
  40. * This is specific to Batch API and is not part of the
  41. * \Drupal\Core\Queue\QueueInterface.
  42. *
  43. * @return array
  44. * An array of queue items.
  45. */
  46. public function getAllItems() {
  47. $result = [];
  48. try {
  49. $items = $this->connection->query('SELECT data FROM {queue} q WHERE name = :name ORDER BY item_id ASC', [':name' => $this->name])->fetchAll();
  50. foreach ($items as $item) {
  51. $result[] = unserialize($item->data);
  52. }
  53. }
  54. catch (\Exception $e) {
  55. $this->catchException($e);
  56. }
  57. return $result;
  58. }
  59. }