rules_scheduler.drush.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * Rules Scheduler Drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function rules_scheduler_drush_command() {
  10. $items = array();
  11. $items['rules-scheduler-tasks'] = array(
  12. 'description' => 'Check for scheduled tasks to be added to the queue.',
  13. 'options' => array(
  14. 'claim' => 'Optionally claim tasks from the queue to work on. Any value set will override the default time spent on this queue.',
  15. ),
  16. 'drupal dependencies' => array('rules', 'rules_scheduler'),
  17. 'aliases' => array('rusch'),
  18. 'examples' => array(
  19. 'drush rusch' => 'Add scheduled tasks to the queue.',
  20. 'drush rusch --claim' => 'Add scheduled tasks to the queue and claim items for the default amount of time.',
  21. 'drush rusch --claim=30' => 'Add scheduled tasks to the queue and claim items for 30 seconds.',
  22. ),
  23. );
  24. return $items;
  25. }
  26. /**
  27. * Implements hook_drush_help().
  28. */
  29. function rules_scheduler_drush_help($section) {
  30. switch ($section) {
  31. case 'drush:rules-scheduler-tasks':
  32. return dt('Checks for scheduled tasks to be added the queue. Can optionally claim tasks from the queue to work on.');
  33. }
  34. }
  35. /**
  36. * Command callback for processing the rules_scheduler_tasks queue.
  37. *
  38. * @see rules_scheduler_cron_queue_info()
  39. * @see rules_scheduler_cron()
  40. */
  41. function drush_rules_scheduler_tasks() {
  42. if (rules_scheduler_queue_tasks()) {
  43. // hook_exit() is not invoked for drush runs, so register it as shutdown
  44. // callback for logging the rules log to the watchdog.
  45. drupal_register_shutdown_function('rules_exit');
  46. // Clear the log before running tasks via the queue to avoid logging
  47. // unrelated logs from previous operations.
  48. RulesLog::logger()->clear();
  49. drush_log(dt('Added scheduled tasks to the queue.'), 'success');
  50. }
  51. $claim = drush_get_option('claim', FALSE);
  52. if ($claim) {
  53. // Fetch the queue information and let other modules alter it.
  54. $queue_name = 'rules_scheduler_tasks';
  55. $info = module_invoke('rules_scheduler', 'cron_queue_info');
  56. drupal_alter('cron_queue_info', $info);
  57. $function = $info[$queue_name]['worker callback'];
  58. // The drush option can override the default process time.
  59. $time = is_numeric($claim) ? (int) $claim : $info[$queue_name]['time'];
  60. $end = time() + $time;
  61. // Claim items and process the queue.
  62. $queue = DrupalQueue::get($queue_name);
  63. $claimed = 0;
  64. while (time() < $end && ($item = $queue->claimItem())) {
  65. $function($item->data);
  66. $queue->deleteItem($item);
  67. $claimed++;
  68. }
  69. if ($claimed) {
  70. drush_log(dt('Claimed and worked on !claimed scheduled tasks for up to !time seconds.', array('!claimed' => $claimed, '!time' => $time)), 'success');
  71. }
  72. }
  73. }