elysia_cron.drush.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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() {
  26. $args = drush_get_arguments();
  27. array_shift($args);
  28. // If drush command has no arguments or the first argument is not in the
  29. // list of allowed operations then we assume the cron execution.
  30. if (empty($args) || !in_array($args[0], array('list', 'run', 'enable', 'disable'))) {
  31. $args = array('run');
  32. }
  33. call_user_func_array('drush_elysia_cron_run_wrapper', $args);
  34. elysia_cron_drush_die();
  35. }
  36. /**
  37. * Implements hook_drush_command().
  38. */
  39. function elysia_cron_drush_command() {
  40. $items['elysia-cron'] = array(
  41. '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.'),
  42. 'callback' => 'drush_elysia_cron_run_wrapper',
  43. 'arguments' => array(
  44. 'op' => 'Operation: list, run, disable, enable',
  45. 'target' => 'Target of operation (optional): usually a task name or a channel name starting with "@"',
  46. ),
  47. 'examples' => array(
  48. 'elysia-cron run' => 'Run all cron tasks in all active modules (as the standard "core-cron")',
  49. 'elysia-cron run --verbose' => 'Run all cron tasks in verbose mode',
  50. 'elysia-cron run @channel' => 'Run all cron tasks in specified channel',
  51. 'elysia-cron run search_cron --ignore-time' => 'Run only search index build task (force execution)',
  52. 'elysia-cron list --elysia-cron-verbose' => 'List all channels/tasks in verbose mode',
  53. 'elysia-cron disable search_cron' => 'Disable search index build task',
  54. ),
  55. 'options' => array(
  56. 'quiet' => 'suppress all output',
  57. 'verbose' => 'enable extended output',
  58. 'elysia-cron-verbose' => 'enable extended output (the same as --verbose, but without enabling drush verbose mode)',
  59. 'ignore-disable' => 'run channels/tasks even if disabled',
  60. 'ignore-time' => 'run channels/tasks even if not ready for execution',
  61. 'ignore-running' => 'run channels/tasks even if already running',
  62. ),
  63. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  64. );
  65. return $items;
  66. }
  67. /**
  68. * Custom callback for 'elysia-cron' drush command.
  69. */
  70. function drush_elysia_cron_run_wrapper($op = FALSE, $target = FALSE) {
  71. global $_elysia_cron_drush;
  72. if (variable_get('maintenance_mode', FALSE)) {
  73. if (!variable_get('elysia_cron_run_maintenance', FALSE)) {
  74. drush_set_error('Cron run is not allowed in maintenance mode');
  75. return;
  76. }
  77. }
  78. $quiet = drush_get_option('quiet', FALSE);
  79. $verbose = drush_get_option('verbose', FALSE);
  80. if (!$verbose) {
  81. $verbose = drush_get_option('elysia-cron-verbose', FALSE);
  82. }
  83. $_elysia_cron_drush = $quiet ? 1 : !$verbose ? 2 : 3;
  84. switch ($op) {
  85. case 'list':
  86. global $_elysia_cron_settings_by_channel;
  87. elysia_cron_initialize();
  88. foreach ($_elysia_cron_settings_by_channel as $channel => $jobs) {
  89. $lines = array();
  90. if (!$verbose) {
  91. $line = array('@' . $channel);
  92. }
  93. else {
  94. $line = array('Channel: @' . $channel);
  95. if ($running = elysia_cron_is_channel_running($channel)) {
  96. $line[] = dt('Running, since !time', array('!time' => elysia_cron_date($running)));
  97. }
  98. if (!empty($jobs['#data']['disabled'])) {
  99. $line[] = dt('Disabled');
  100. }
  101. if (!$running) {
  102. $line[] = dt('Last run: !time', array('!time' => elysia_cron_date(_ec_variable_get('elysia_cron_last_run', 0))));
  103. }
  104. }
  105. drush_print(implode($line, ', '));
  106. foreach ($jobs as $job => $conf) {
  107. $line = array();
  108. if (strpos($job, '#') !== 0) {
  109. if (!$verbose) {
  110. drush_print($job);
  111. }
  112. else {
  113. $line['name'] = $job;
  114. $line['status'] = empty($conf['disabled']) ? dt('Enabled') : dt('Disabled');
  115. $line['run_status'] = empty($conf['running']) && elysia_cron_should_run($conf) ? dt('Ready to run') : dt('Waiting');
  116. $line['run_status'] = !empty($conf['running']) ? dt('Running, since !time', array('!time' => elysia_cron_date($conf['running']))) : $line['run_status'];
  117. $line['last_run'] = !empty($conf['last_run']) ? dt('Last run: !time', array('!time' => elysia_cron_date($conf['last_run']))) : '';
  118. $lines[] = $line;
  119. }
  120. }
  121. }
  122. if ($lines) {
  123. drush_print_table($lines);
  124. }
  125. }
  126. break;
  127. case 'run':
  128. if (empty($target)) {
  129. elysia_cron_run(FALSE, drush_get_option('ignore-disable', FALSE), drush_get_option('ignore-time', FALSE), drush_get_option('ignore-running', FALSE));
  130. }
  131. elseif (strpos($target, '@') === 0) {
  132. elysia_cron_initialize();
  133. if (elysia_cron_channel_exists(substr($target, 1))) {
  134. 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));
  135. }
  136. else {
  137. drush_set_error('Channel ' . substr($target, 1) . ' does not exists');
  138. }
  139. }
  140. else {
  141. elysia_cron_initialize();
  142. if (elysia_cron_job_exists($target)) {
  143. elysia_cron_run_job($target, drush_get_option('ignore-disable', FALSE), drush_get_option('ignore-time', FALSE), drush_get_option('ignore-running', FALSE));
  144. }
  145. else {
  146. drush_set_error('Job ' . $target . ' does not exists');
  147. }
  148. }
  149. break;
  150. case 'disable':
  151. case 'enable':
  152. if (!empty($target)) {
  153. if (strpos($target, '@') === 0) {
  154. elysia_cron_set_channel_disabled(substr($target, 1), $op == 'disable');
  155. }
  156. else {
  157. elysia_cron_set_job_disabled($target, $op == 'disable');
  158. }
  159. drush_log('Done', 'ok');
  160. }
  161. else {
  162. drush_set_error('Target not specified');
  163. }
  164. break;
  165. default:
  166. drush_print_help(drush_get_command());
  167. break;
  168. }
  169. }