elysia_cron.drush.inc 6.4 KB

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