batch.queue.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Queue handlers used by the Batch API.
  5. *
  6. * These implementations:
  7. * - Ensure FIFO ordering.
  8. * - Allow 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. /**
  12. * Defines a batch queue.
  13. *
  14. * Stale items from failed batches are cleaned from the {queue} table on cron
  15. * using the 'created' date.
  16. */
  17. class BatchQueue extends SystemQueue {
  18. /**
  19. * Overrides SystemQueue::claimItem().
  20. *
  21. * Unlike SystemQueue::claimItem(), this method provides a default lease
  22. * time of 0 (no expiration) instead of 30. This allows the item to be
  23. * claimed repeatedly until it is deleted.
  24. */
  25. public function claimItem($lease_time = 0) {
  26. $item = db_query_range('SELECT data, item_id FROM {queue} q WHERE name = :name ORDER BY item_id ASC', 0, 1, array(':name' => $this->name))->fetchObject();
  27. if ($item) {
  28. $item->data = unserialize($item->data);
  29. return $item;
  30. }
  31. return FALSE;
  32. }
  33. /**
  34. * Retrieves all remaining items in the queue.
  35. *
  36. * This is specific to Batch API and is not part of the DrupalQueueInterface.
  37. */
  38. public function getAllItems() {
  39. $result = array();
  40. $items = db_query('SELECT data FROM {queue} q WHERE name = :name ORDER BY item_id ASC', array(':name' => $this->name))->fetchAll();
  41. foreach ($items as $item) {
  42. $result[] = unserialize($item->data);
  43. }
  44. return $result;
  45. }
  46. }
  47. /**
  48. * Defines a batch queue for non-progressive batches.
  49. */
  50. class BatchMemoryQueue extends MemoryQueue {
  51. /**
  52. * Overrides MemoryQueue::claimItem().
  53. *
  54. * Unlike MemoryQueue::claimItem(), this method provides a default lease
  55. * time of 0 (no expiration) instead of 30. This allows the item to be
  56. * claimed repeatedly until it is deleted.
  57. */
  58. public function claimItem($lease_time = 0) {
  59. if (!empty($this->queue)) {
  60. reset($this->queue);
  61. return current($this->queue);
  62. }
  63. return FALSE;
  64. }
  65. /**
  66. * Retrieves all remaining items in the queue.
  67. *
  68. * This is specific to Batch API and is not part of the DrupalQueueInterface.
  69. */
  70. public function getAllItems() {
  71. $result = array();
  72. foreach ($this->queue as $item) {
  73. $result[] = $item->data;
  74. }
  75. return $result;
  76. }
  77. }