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_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. );
  22. $items['queue-list'] = array(
  23. 'description' => 'Returns a list of all defined queues',
  24. 'options' => array(
  25. 'pipe' => 'Return a comma delimited list of queues.'
  26. )
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Validation for Drupal 6 to ensure the drupal_queue module is enabled.
  32. *
  33. * @param $command
  34. * The command being validated.
  35. */
  36. function drush_queue_validate($command) {
  37. if (drush_drupal_major_version() == 6) {
  38. if (!module_exists('drupal_queue')) {
  39. $args = array('!command' => $command, '!dependencies' => 'drupal_queue');
  40. return drush_set_error('DRUSH_COMMAND_DEPENDENCY_ERROR', dt('Command !command needs the following modules installed/enabled to run: !dependencies.', $args));
  41. }
  42. else {
  43. drupal_queue_include();
  44. }
  45. }
  46. }
  47. /**
  48. * Validation callback for drush queue-run.
  49. */
  50. function drush_queue_run_validate($queue_name) {
  51. drush_queue_validate('queue-run');
  52. // Get all queues.
  53. $queues = drush_queue_get_queues();
  54. if (!isset($queues[$queue_name])) {
  55. return drush_set_error('DRUSH_QUEUE_ERROR', dt('Could not find the !name queue.', array('!name' => $queue_name)));
  56. }
  57. }
  58. /**
  59. * Validation callback for drush queue-list.
  60. */
  61. function drush_queue_list_validate() {
  62. drush_queue_validate('queue-list');
  63. }
  64. /**
  65. * Command callback for drush queue-run.
  66. *
  67. * Queue runner that is compatible with queues declared using both
  68. * hook_queue_info() and hook_cron_queue_info().
  69. *
  70. * @param $queue_name
  71. * Arbitrary string. The name of the queue to work with.
  72. */
  73. function drush_queue_run($queue_name) {
  74. // Get all queues.
  75. $queues = drush_queue_get_queues();
  76. // Try to increase the maximum execution time if it is too low.
  77. $max_execution_time = ini_get('max_execution_time');
  78. if ($max_execution_time > 0 && $max_execution_time < 240 && !ini_get('safe_mode')) {
  79. set_time_limit(240);
  80. }
  81. $info = $queues[$queue_name];
  82. $function = $info['cron']['callback'];
  83. $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
  84. $queue = DrupalQueue::get($queue_name);
  85. while (time() < $end && ($item = $queue->claimItem())) {
  86. $function($item->data);
  87. $queue->deleteItem($item);
  88. }
  89. }
  90. /**
  91. * Command callback for drush queue-list.
  92. */
  93. function drush_queue_list() {
  94. $queues = drush_queue_get_queues();
  95. $default_class = variable_get('queue_default_class', 'SystemQueue');
  96. $rows = array(array('Queue', 'Items', 'Class'));
  97. foreach (array_keys($queues) as $name) {
  98. $q = DrupalQueue::get($name);
  99. $class = variable_get('queue_class_' . $name, $default_class);
  100. $rows[] = array($name, $q->numberOfItems(), $class);
  101. }
  102. if (drush_get_option('pipe')) {
  103. $pipe = array();
  104. array_shift($rows);
  105. foreach ($rows as $r) {
  106. $pipe[] = implode(",", $r);
  107. }
  108. drush_print_pipe($pipe);
  109. }
  110. else {
  111. drush_print_table($rows, TRUE);
  112. }
  113. // Return the result for backend invoke
  114. return $rows;
  115. }
  116. /**
  117. * Get queues defined with hook_cron_queue_info().
  118. *
  119. * @return
  120. * Array of queues indexed by name and containing queue object and number
  121. * of items.
  122. */
  123. function drush_queue_get_queues() {
  124. static $queues;
  125. if (!isset($queues)) {
  126. // Invoke hook_queue_info(), currently only defined by the queue_ui module.
  127. $queues = module_invoke_all('queue_info');
  128. // Merge in queues from modules that implement hook_cron_queue_info().
  129. $cron_queues = module_invoke_all('cron_queue_info');
  130. drupal_alter('cron_queue_info', $cron_queues);
  131. foreach($cron_queues as $name => $queue) {
  132. $queues[$name] = array(
  133. 'cron' => array(
  134. 'callback' => $queue['worker callback'],
  135. 'time' => $queue['time'],
  136. )
  137. );
  138. }
  139. }
  140. return $queues;
  141. }