elysia_cron.drush.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for Elysia cron module.
  5. */
  6. /**
  7. * Detect, is it drush or not.
  8. *
  9. * @return bool
  10. * TRUE if code executed inside drush, FALSE otherwise.
  11. */
  12. function elysia_cron_drush_detect() {
  13. return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))) && function_exists('drush_main');
  14. }
  15. /**
  16. * Exit from drush execution.
  17. */
  18. function elysia_cron_drush_die() {
  19. drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
  20. drupal_exit();
  21. }
  22. /**
  23. * Wrapper for drush_invoke().
  24. */
  25. function elysia_cron_drush_invoke($replace_core_cron = FALSE) {
  26. $args = drush_get_arguments();
  27. array_shift($args);
  28. // If invoked like 'core-cron' I do the same as that: execute 'run'.
  29. if ($replace_core_cron && empty($args)) {
  30. $args = array('run');
  31. }
  32. call_user_func_array('drush_elysia_cron_run_wrapper', $args);
  33. elysia_cron_drush_die();
  34. }
  35. /**
  36. * Implements hook_drush_command().
  37. */
  38. function elysia_cron_drush_command() {
  39. $items['elysia-cron'] = array(
  40. 'description' => dt('Run all cron tasks in all active modules for specified site using elysia cron system. This replaces the standard "core-cron" drush handler.'),
  41. 'callback' => 'drush_elysia_cron_run_wrapper',
  42. 'arguments' => array(
  43. 'op' => 'Operation: list, run, disable, enable',
  44. 'target' => 'Target of operation (optional): usually a task name or a channel name starting with "@"',
  45. ),
  46. 'examples' => array(
  47. 'elysia-cron run' => 'Run all cron tasks in all active modules (as the standard "core-cron")',
  48. 'elysia-cron run --verbose' => 'Run all cron tasks in verbose mode',
  49. 'elysia-cron run @channel' => 'Run all cron tasks in specified channel',
  50. 'elysia-cron run search_cron --ignore-time' => 'Run only search index build task (force execution)',
  51. 'elysia-cron list --elysia-cron-verbose' => 'List all channels/tasks in verbose mode',
  52. 'elysia-cron disable search_cron' => 'Disable search index build task',
  53. ),
  54. 'options' => array(
  55. 'quiet' => 'suppress all output',
  56. 'verbose' => 'enable extended output',
  57. 'elysia-cron-verbose' => 'enable extended output (the same as --verbose, but without enabling drush verbose mode)',
  58. 'ignore-disable' => 'run channels/tasks even if disabled',
  59. 'ignore-time' => 'run channels/tasks even if not ready for execution',
  60. 'ignore-running' => 'run channels/tasks even if already running',
  61. ),
  62. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  63. );
  64. return $items;
  65. }
  66. /**
  67. * Custom callback for 'elysia-cron' drush command.
  68. */
  69. function drush_elysia_cron_run_wrapper($op = FALSE, $target = FALSE) {
  70. global $_elysia_cron_drush;
  71. if (variable_get('maintenance_mode', FALSE)) {
  72. if (!variable_get('elysia_cron_run_maintenance', FALSE)) {
  73. drush_set_error('Cron run is not allowed in maintenance mode');
  74. return;
  75. }
  76. }
  77. $quiet = drush_get_option('quiet', FALSE);
  78. $verbose = drush_get_option('verbose', FALSE);
  79. if (!$verbose) {
  80. $verbose = drush_get_option('elysia-cron-verbose', FALSE);
  81. }
  82. $_elysia_cron_drush = $quiet ? 1 : !$verbose ? 2 : 3;
  83. switch ($op) {
  84. case 'list':
  85. global $_elysia_cron_settings_by_channel;
  86. elysia_cron_initialize();
  87. foreach ($_elysia_cron_settings_by_channel as $channel => $jobs) {
  88. $lines = array();
  89. if (!$verbose) {
  90. $line = array('@' . $channel);
  91. }
  92. else {
  93. $line = array('Channel: @' . $channel);
  94. if ($running = elysia_cron_is_channel_running($channel)) {
  95. $line[] = dt('Running, since !time', array('!time' => elysia_cron_date($running)));
  96. }
  97. if (!empty($jobs['#data']['disabled'])) {
  98. $line[] = dt('Disabled');
  99. }
  100. if (!$running) {
  101. $line[] = dt('Last run: !time', array('!time' => elysia_cron_date(_ec_variable_get('elysia_cron_last_run', 0))));
  102. }
  103. }
  104. drush_print(implode($line, ', '));
  105. foreach ($jobs as $job => $conf) {
  106. $line = array();
  107. if (strpos($job, '#') !== 0) {
  108. if (!$verbose) {
  109. drush_print($job);
  110. }
  111. else {
  112. $line['name'] = $job;
  113. $line['status'] = empty($conf['disabled']) ? dt('Enabled') : dt('Disabled');
  114. $line['run_status'] = empty($conf['running']) && elysia_cron_should_run($conf) ? dt('Ready to run') : dt('Waiting');
  115. $line['run_status'] = !empty($conf['running']) ? dt('Running, since !time', array('!time' => elysia_cron_date($conf['running']))) : $line['run_status'];
  116. $line['last_run'] = !empty($conf['last_run']) ? dt('Last run: !time', array('!time' => elysia_cron_date($conf['last_run']))) : '';
  117. $lines[] = $line;
  118. }
  119. }
  120. }
  121. if ($lines) {
  122. drush_print_table($lines);
  123. }
  124. }
  125. break;
  126. case 'run':
  127. if (empty($target)) {
  128. elysia_cron_run(FALSE, drush_get_option('ignore-disable', FALSE), drush_get_option('ignore-time', FALSE), drush_get_option('ignore-running', FALSE));
  129. }
  130. elseif (strpos($target, '@') === 0) {
  131. elysia_cron_initialize();
  132. if (elysia_cron_channel_exists(substr($target, 1))) {
  133. elysia_cron_run_channel(substr($target, 1), drush_get_option('ignore-disable', FALSE), drush_get_option('ignore-time', FALSE), drush_get_option('ignore-running', FALSE));
  134. }
  135. else {
  136. drush_set_error('Channel ' . substr($target, 1) . ' does not exists');
  137. }
  138. }
  139. else {
  140. elysia_cron_initialize();
  141. if (elysia_cron_job_exists($target)) {
  142. elysia_cron_run_job($target, drush_get_option('ignore-disable', FALSE), drush_get_option('ignore-time', FALSE), drush_get_option('ignore-running', FALSE));
  143. }
  144. else {
  145. drush_set_error('Job ' . $target . ' does not exists');
  146. }
  147. }
  148. break;
  149. case 'disable':
  150. case 'enable':
  151. if (!empty($target)) {
  152. if (strpos($target, '@') === 0) {
  153. elysia_cron_set_channel_disabled(substr($target, 1), $op == 'disable');
  154. }
  155. else {
  156. elysia_cron_set_job_disabled($target, $op == 'disable');
  157. }
  158. drush_log('Done', 'ok');
  159. }
  160. else {
  161. drush_set_error('Target not specified');
  162. }
  163. break;
  164. default:
  165. drush_print_help(drush_get_command());
  166. break;
  167. }
  168. }