queue.drush.inc

  1. 8.0.x commands/core/queue.drush.inc
  2. 6.x commands/core/queue.drush.inc
  3. 7.x commands/core/queue.drush.inc
  4. 5.x commands/core/queue.drush.inc
  5. master commands/core/queue.drush.inc

Functions

Namesort descending Description
drush_queue_get Call the correct queue factory method
drush_queue_get_queues Get queues defined with hook_cron_queue_info().
drush_queue_list Command callback for drush queue-list.
drush_queue_list_validate Validation callback for drush queue-list.
drush_queue_run Command callback for drush queue-run.
drush_queue_run_validate Validation callback for drush queue-run.
drush_queue_validate Validation for Drupal 6 to ensure the drupal_queue module is enabled.
queue_drush_command Implements hook_drush_command().
queue_drush_help Implements hook_drush_help().

File

commands/core/queue.drush.inc
View source
  1. <?php
  2. /**
  3. * Implements hook_drush_help().
  4. */
  5. function queue_drush_help($section) {
  6. switch ($section) {
  7. case 'drush:queue-run':
  8. return dt('Run Drupal queue workers. As opposed to "drush cron" that can only be run one at a time on a single site, "drush queue-run" can be invoked as many times as the server load allows.');
  9. }
  10. }
  11. /**
  12. * Implements hook_drush_command().
  13. */
  14. function queue_drush_command() {
  15. $items['queue-run'] = array(
  16. 'description' => 'Run a specific queue by name',
  17. 'arguments' => array(
  18. 'queue_name' => 'The name of the queue to run, as defined in either hook_queue_info or hook_cron_queue_info.',
  19. ),
  20. 'required-arguments' => TRUE,
  21. 'options' => array(
  22. 'time-limit' => 'The maximum number of seconds allowed to run the queue',
  23. ),
  24. );
  25. $items['queue-list'] = array(
  26. 'description' => 'Returns a list of all defined queues',
  27. 'outputformat' => array(
  28. 'default' => 'table',
  29. 'pipe-format' => 'csv',
  30. 'field-labels' => array(
  31. 'queue' => 'Queue',
  32. 'items' => 'Items',
  33. 'class' => 'Class',
  34. ),
  35. 'ini-item' => 'items',
  36. 'table-metadata' => array(
  37. 'key-value-item' => 'items',
  38. ),
  39. 'output-data-type' => 'format-table',
  40. ),
  41. );
  42. return $items;
  43. }
  44. /**
  45. * Call the correct queue factory method
  46. *
  47. * @deprecated
  48. */
  49. function drush_queue_get($name) {
  50. return DrupalQueue::get($name);
  51. }
  52. /**
  53. * Validation for Drupal 6 to ensure the drupal_queue module is enabled.
  54. *
  55. * @param $command
  56. * The command being validated.
  57. */
  58. function drush_queue_validate($command) {
  59. if (drush_drupal_major_version() == 6) {
  60. if (!module_exists('drupal_queue')) {
  61. $args = array('!command' => $command, '!dependencies' => 'drupal_queue');
  62. return drush_set_error('DRUSH_COMMAND_DEPENDENCY_ERROR', dt('Command !command needs the following modules installed/enabled to run: !dependencies.', $args));
  63. }
  64. else {
  65. drupal_queue_include();
  66. }
  67. }
  68. }
  69. /**
  70. * Validation callback for drush queue-run.
  71. */
  72. function drush_queue_run_validate($queue_name) {
  73. drush_queue_validate('queue-run');
  74. // Get all queues.
  75. $queues = drush_queue_get_queues();
  76. if (!isset($queues[$queue_name])) {
  77. return drush_set_error('DRUSH_QUEUE_ERROR', dt('Could not find the !name queue.', array('!name' => $queue_name)));
  78. }
  79. }
  80. /**
  81. * Validation callback for drush queue-list.
  82. */
  83. function drush_queue_list_validate() {
  84. drush_queue_validate('queue-list');
  85. }
  86. /**
  87. * Command callback for drush queue-run.
  88. *
  89. * Queue runner that is compatible with queues declared using both
  90. * hook_queue_info() and hook_cron_queue_info().
  91. *
  92. * @param $queue_name
  93. * Arbitrary string. The name of the queue to work with.
  94. */
  95. function drush_queue_run($queue_name) {
  96. // Get all queues.
  97. $queues = drush_queue_get_queues();
  98. $info = $queues[$queue_name];
  99. $function = $info['worker callback'];
  100. $time_limit = (int) drush_get_option('time-limit');
  101. $end = time() + $time_limit;
  102. /** @var DrupalQueueInterface $queue */
  103. $queue = DrupalQueue::get($queue_name);
  104. $count = 0;
  105. $start = microtime(TRUE);
  106. while ((!$time_limit || time() < $end) && ($item = $queue->claimItem())) {
  107. try {
  108. drush_log(dt('Processing item @id from @name queue.', array('@name' => $queue_name, 'id' => $item->item_id)), 'info');
  109. $function($item->data);
  110. $queue->deleteItem($item);
  111. $count++;
  112. }
  113. catch (Exception $e) {
  114. // In case of exception log it and leave the item in the queue
  115. // to be processed again later.
  116. drush_set_error('DRUSH_QUEUE_EXCEPTION', $e->getMessage());
  117. }
  118. }
  119. $elapsed = microtime(TRUE) - $start;
  120. drush_log(dt('Processed @count items from the @name queue in @elapsed sec.', array('@count' => $count, '@name' => $queue_name, '@elapsed' => round($elapsed, 2))), drush_get_error() ? 'warning' : 'ok');
  121. }
  122. /**
  123. * Command callback for drush queue-list.
  124. */
  125. function drush_queue_list() {
  126. $result = array();
  127. $queues = drush_queue_get_queues();
  128. foreach (array_keys($queues) as $name) {
  129. /** @var DrupalQueueInterface $q */
  130. $q = DrupalQueue::get($name);
  131. $result[$name] = array(
  132. 'queue' => $name,
  133. 'items' => $q->numberOfItems(),
  134. 'class' => get_class($q),
  135. );
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Get queues defined with hook_cron_queue_info().
  141. *
  142. * @return array
  143. * Array of queues indexed by name and containing queue object and number
  144. * of items.
  145. */
  146. function drush_queue_get_queues() {
  147. static $queues;
  148. if (!isset($queues)) {
  149. $queues = module_invoke_all('cron_queue_info');
  150. drupal_alter('cron_queue_info', $queues);
  151. // Merge in queues from modules that implement hook_queue_info.
  152. // Currently only defined by the queue_ui module.
  153. $info_queues = module_invoke_all('queue_info');
  154. foreach ($info_queues as $name => $queue) {
  155. $queues[$name]['worker callback'] = $queue['cron']['callback'];
  156. if (isset($queue['cron']['time'])) {
  157. $queues[$name]['time'] = $queue['cron']['time'];
  158. }
  159. }
  160. }
  161. return $queues;
  162. }