queue_ui.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file queue_ui.module
  4. */
  5. /**
  6. * Implements hook_permission().
  7. */
  8. function queue_ui_permission() {
  9. return array(
  10. 'admin queue_ui' => array(
  11. 'title' => t('Administer queues'),
  12. 'description' => t('View, run, and delete queues'),
  13. ),
  14. );
  15. }
  16. /**
  17. * Implements hook_menu().
  18. */
  19. function queue_ui_menu() {
  20. $items['admin/config/system/queue-ui'] = array(
  21. 'title' => 'Queue manager',
  22. 'description' => 'View and manage queues',
  23. 'page callback' => 'drupal_get_form',
  24. 'page arguments' => array('queue_ui_page'),
  25. 'access arguments' => array('admin queue_ui'),
  26. 'file' => 'queue_ui.pages.inc',
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Menu loader for queue_ui_queue.
  32. */
  33. function queue_ui_queue_load($name) {
  34. return DrupalQueue::get($name);
  35. }
  36. // @todo remove before prod
  37. function queue_ui_test() {
  38. $queue = DrupalQueue::get('queue ui test_me');
  39. $queue->createQueue();
  40. $num = mt_rand(0,99);
  41. for ($i = 0; $i < $num; $i++) {
  42. $queue->createItem(time());
  43. }
  44. }
  45. /**
  46. * Get the names of queues.
  47. *
  48. * @param Array of queue names suitable for DrupalQueue::get();
  49. */
  50. function queue_ui_queue_names() {
  51. $result = db_query("SELECT DISTINCT name FROM {queue}");
  52. return $result->fetchAll();
  53. }
  54. /**
  55. * Get queues.
  56. *
  57. * @return Array of queues indexed by name and containing queue object and number
  58. * of items.
  59. */
  60. function queue_ui_queues() {
  61. $queues = array();
  62. $queue_names = queue_ui_queue_names();
  63. if (!empty($queue_names)) {
  64. // Build array of queues indexed by name with number of items.
  65. foreach ($queue_names as $name) {
  66. $queue = DrupalQueue::get($name->name);
  67. $queues[$name->name] = array(
  68. 'queue' => $queue,
  69. 'items' => $queue->numberOfItems(),
  70. );
  71. }
  72. }
  73. return $queues;
  74. }
  75. /**
  76. * Get queues defined with hook_queue_info().
  77. *
  78. * @return Array of queues indexed by name and containing
  79. */
  80. function queue_ui_defined_queues() {
  81. $queues = &drupal_static(__FUNCTION__);
  82. if (!isset($queues)) {
  83. // Invoke hook_queue_info().
  84. $queues = module_invoke_all('queue_info');
  85. }
  86. return $queues;
  87. }
  88. /**
  89. * Implements hook_cron().
  90. */
  91. function queue_ui_cron() {
  92. // Retrieve queues set for cron processing.
  93. $defs = queue_ui_defined_queues();
  94. if (!empty($defs)) {
  95. foreach ($defs as $name => $definition) {
  96. $queue = DrupalQueue::get($name);
  97. // A cron callback must be defined and there must be items in the queue.
  98. if (isset($definition['cron']) && is_object($queue) && $queue->numberOfItems()) {
  99. $active = variable_get('queue_ui_cron_' . $name, FALSE);
  100. if ($active) {
  101. // Pass $queue to cron callback for processing.
  102. $function = $definition['cron']['callback'];
  103. // Definitions can define arguments.
  104. $args = isset($definition['cron']['callback']) ? $definition['cron']['callback'] : NULL;
  105. $function($queue, $args);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. // @todo remove before prod
  112. function queue_ui_queue_info() {
  113. return array(
  114. 'queue ui test_me' => array(
  115. 'title' => t('Test queue'),
  116. 'batch' => array(
  117. 'operations' => array(array('queue_ui_batch_test', array())),
  118. 'finished' => 'queue_ui_batch_finished',
  119. 'title' => t('Processing test queue'),
  120. ),
  121. 'cron' => array(
  122. 'callback' =>'queue_ui_test_process',
  123. ),
  124. ),
  125. );
  126. }
  127. function queue_ui_test_process($queue) {
  128. $count = $queue->numberOfItems();
  129. for ($i = 0; $i < 20 && $count > 0; $i++) {
  130. $item = $queue->claimItem(20); // Lease time.
  131. if ($item) {
  132. // We would do some processing, if this were REAL.
  133. $queue->deleteItem($item);
  134. $count--;
  135. }
  136. }
  137. }
  138. function queue_ui_batch_test($queue, &$context) {
  139. if (empty($context['sandbox'])) {
  140. $context['sandbox']['progress'] = 0;
  141. $context['sandbox']['current'] = 0;
  142. $context['sandbox']['max'] = $queue->numberOfItems();
  143. }
  144. for ($i = 0; $i < 20 && $context['sandbox']['current'] < $context['sandbox']['max']; $i++) {
  145. $item = $queue->claimItem(20); // Lease time.
  146. if ($item) {
  147. // We would do some processing, if this were REAL.
  148. $queue->deleteItem($item);
  149. }
  150. $context['sandbox']['progress']++;
  151. $context['sandbox']['current']++;
  152. }
  153. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  154. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  155. }
  156. }
  157. function queue_ui_batch_finished($success, $results, $operations) {
  158. if ($success) {
  159. $message = 'success';
  160. }
  161. else {
  162. $message = t('An error occured @todo.');
  163. }
  164. drupal_set_message($message);
  165. }