elysia_cron.drush.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 'force' => 'run channels/tasks even if it disabled/not ready for execution/already running',
  60. 'ignore-disable' => 'run channels/tasks even if disabled',
  61. 'ignore-maintenance' => 'run channels/tasks even if maintenance mode is enabled',
  62. 'ignore-time' => 'run channels/tasks even if not ready for execution',
  63. 'ignore-running' => 'run channels/tasks even if already running',
  64. ),
  65. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  66. );
  67. return $items;
  68. }
  69. /**
  70. * Custom callback for 'elysia-cron' drush command.
  71. */
  72. function drush_elysia_cron_run_wrapper($op = FALSE, $target = FALSE) {
  73. global $_elysia_cron_drush;
  74. $force = drush_get_option('force', FALSE);
  75. if (variable_get('maintenance_mode', FALSE)) {
  76. $ignore_maintenance = $force || drush_get_option('ignore-maintenance', FALSE);
  77. if (!$ignore_maintenance && !variable_get('elysia_cron_run_maintenance', FALSE)) {
  78. drush_set_error('Cron run is not allowed in maintenance mode');
  79. return;
  80. }
  81. }
  82. $quiet = drush_get_option('quiet', FALSE);
  83. $verbose = drush_get_option('verbose', FALSE);
  84. if (!$verbose) {
  85. $verbose = drush_get_option('elysia-cron-verbose', FALSE);
  86. }
  87. if ($quiet) {
  88. $_elysia_cron_drush = 1;
  89. }
  90. elseif ($verbose) {
  91. $_elysia_cron_drush = 3;
  92. }
  93. else {
  94. $_elysia_cron_drush = 2;
  95. }
  96. switch ($op) {
  97. case 'list':
  98. global $_elysia_cron_settings_by_channel;
  99. elysia_cron_initialize();
  100. foreach ($_elysia_cron_settings_by_channel as $channel => $jobs) {
  101. $lines = array();
  102. if (!$verbose) {
  103. $line = array('@' . $channel);
  104. }
  105. else {
  106. $line = array('Channel: @' . $channel);
  107. if ($running = elysia_cron_is_channel_running($channel)) {
  108. $line[] = dt('Running, since !time', array('!time' => elysia_cron_date($running)));
  109. }
  110. if (!empty($jobs['#data']['disabled'])) {
  111. $line[] = dt('Disabled');
  112. }
  113. if (!$running) {
  114. $line[] = dt('Last run: !time', array('!time' => elysia_cron_date(_ec_variable_get('elysia_cron_last_run', 0))));
  115. }
  116. }
  117. drush_print(implode($line, ', '));
  118. foreach ($jobs as $job => $conf) {
  119. $line = array();
  120. if (strpos($job, '#') !== 0) {
  121. if (!$verbose) {
  122. drush_print($job);
  123. }
  124. else {
  125. $line['name'] = $job;
  126. $line['status'] = empty($conf['disabled']) ? dt('Enabled') : dt('Disabled');
  127. $line['run_status'] = empty($conf['running']) && elysia_cron_should_run($conf) ? dt('Ready to run') : dt('Waiting');
  128. $line['run_status'] = !empty($conf['running']) ? dt('Running, since !time', array('!time' => elysia_cron_date($conf['running']))) : $line['run_status'];
  129. $line['last_run'] = !empty($conf['last_run']) ? dt('Last run: !time', array('!time' => elysia_cron_date($conf['last_run']))) : '';
  130. $lines[] = $line;
  131. }
  132. }
  133. }
  134. if ($lines) {
  135. drush_print_table($lines);
  136. }
  137. }
  138. break;
  139. case 'run':
  140. // Collect options.
  141. $ignore_disable = $force || drush_get_option('ignore-disable', FALSE);
  142. $ignore_time = $force || drush_get_option('ignore-time', FALSE);
  143. $ignore_running = $force || drush_get_option('ignore-running', FALSE);
  144. if (empty($target)) {
  145. elysia_cron_run(FALSE, $ignore_disable, $ignore_time, $ignore_running);
  146. }
  147. elseif (strpos($target, '@') === 0) {
  148. elysia_cron_initialize();
  149. if (elysia_cron_channel_exists(substr($target, 1))) {
  150. elysia_cron_run_channel(substr($target, 1), $ignore_disable, $ignore_time, $ignore_running);
  151. }
  152. else {
  153. drush_set_error('Channel ' . substr($target, 1) . ' does not exists');
  154. }
  155. }
  156. else {
  157. elysia_cron_initialize();
  158. if (elysia_cron_job_exists($target)) {
  159. elysia_cron_run_job($target, $ignore_disable, $ignore_time, $ignore_running);
  160. }
  161. else {
  162. drush_set_error('Job ' . $target . ' does not exists');
  163. }
  164. }
  165. break;
  166. case 'disable':
  167. case 'enable':
  168. if (!empty($target)) {
  169. if (strpos($target, '@') === 0) {
  170. elysia_cron_set_channel_disabled(substr($target, 1), $op == 'disable');
  171. }
  172. else {
  173. elysia_cron_set_job_disabled($target, $op == 'disable');
  174. }
  175. drush_log('Done', 'ok');
  176. }
  177. else {
  178. drush_set_error('Target not specified');
  179. }
  180. break;
  181. default:
  182. drush_print_help(drush_get_command());
  183. break;
  184. }
  185. }