ultimate_cron.drush.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for Ultimate Cron!
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function ultimate_cron_drush_command() {
  10. $items = array();
  11. $items['cron-list'] = array(
  12. 'description' => "List cron jobs.",
  13. 'arguments' => array(
  14. 'type' => 'The type of jobs to list (all, running, enabled, disabled, unsafe)',
  15. ),
  16. 'options' => array(
  17. 'module' => 'Only show jobs from comma separated list of modules',
  18. ),
  19. 'examples' => array(
  20. 'drush cron-list running',
  21. ),
  22. 'aliases' => array('cl'),
  23. );
  24. $items['cron-run'] = array(
  25. 'description' => "Run cron job.",
  26. 'arguments' => array(
  27. 'function' => 'Function to run',
  28. ),
  29. 'options' => array(
  30. 'cli' => "Don't spawn a background process through http",
  31. 'check-rule' => "Check rule to determine if job should run",
  32. 'logfile' => "File to log to when spawning cli processes"
  33. ),
  34. 'examples' => array(
  35. 'drush cron-run node_cron',
  36. ),
  37. 'aliases' => array('cr'),
  38. );
  39. $items['cron-enable'] = array(
  40. 'description' => "Enable cron job.",
  41. 'arguments' => array(
  42. 'function' => 'Function to enable',
  43. ),
  44. 'examples' => array(
  45. 'drush cron-enable node_cron',
  46. ),
  47. 'aliases' => array('ce'),
  48. );
  49. $items['cron-disable'] = array(
  50. 'description' => "Disable cron job.",
  51. 'arguments' => array(
  52. 'function' => 'Function to disable',
  53. ),
  54. 'examples' => array(
  55. 'drush cron-disable node_cron',
  56. ),
  57. 'aliases' => array('cd'),
  58. );
  59. $items['cron-unlock'] = array(
  60. 'description' => "Unlock cron job.",
  61. 'arguments' => array(
  62. 'function' => 'Function to unlock',
  63. ),
  64. 'examples' => array(
  65. 'drush cron-unlock node_cron',
  66. ),
  67. 'aliases' => array('cu'),
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Implements hook_drush_help().
  73. */
  74. function ultimate_cron_drush_help($section) {
  75. switch ($section) {
  76. case 'drush:cron-list':
  77. return dt("This command will list cron jobs.");
  78. case 'drush:cron-run':
  79. return dt("This command will run a cron job.");
  80. case 'drush:cron-enable':
  81. return dt("This command will enable a cron job.");
  82. case 'drush:cron-disable':
  83. return dt("This command will disable a cron job.");
  84. case 'drush:cron-unlock':
  85. return dt("This command will unlock a cron job.");
  86. }
  87. }
  88. /**
  89. * List cron jobs.
  90. */
  91. function drush_ultimate_cron_cron_list($type = 'all') {
  92. $module = drush_get_option('module');
  93. $module = $module ? explode(",", $module) : array();
  94. // Get hooks and their data
  95. $hooks = ultimate_cron_get_hooks();
  96. $data = _ultimate_cron_preload_cron_data();
  97. $jobs = array();
  98. $modules = array();
  99. foreach ($hooks as $function => $hook) {
  100. if (!$module || $module == $hook['module']) {
  101. $modules[$hook['module']][$function] = $hook;
  102. }
  103. }
  104. foreach ($hooks as $function => &$hook) {
  105. if ($module && !in_array($hook['module'], $module)) {
  106. continue;
  107. }
  108. $hook['settings'] = $data[$function]['settings'] + $hook['settings'];
  109. $hook['background_process'] = $data[$function]['background_process'];
  110. $hook['log'] = ultimate_cron_get_log($function);
  111. switch ($type) {
  112. case 'enabled':
  113. if (!empty($hook['settings']['enabled'])) {
  114. $jobs[] = $hook;
  115. }
  116. break;
  117. case 'disabled':
  118. if (empty($hook['settings']['enabled'])) {
  119. $jobs[] = $hook;
  120. }
  121. break;
  122. case 'running':
  123. if (!empty($data[$hook['function']]['background_process'])) {
  124. $jobs[] = $hook;
  125. }
  126. break;
  127. case 'unsafe':
  128. if (!empty($hook['unsafe'])) {
  129. $jobs[] = $hook;
  130. }
  131. break;
  132. case 'failed':
  133. if (isset($hook['log']['status']) && empty($hook['log']['status'])) {
  134. $jobs[] = $hook;
  135. }
  136. break;
  137. case 'all':
  138. default:
  139. $jobs[] = $hook;
  140. }
  141. }
  142. $table = array();
  143. $table[] = array('', dt('Module'), dt('Function'), dt('Rules'), dt('Start'), dt('Duration'));
  144. foreach ($jobs as $hook) {
  145. $legend = '';
  146. if (!empty($hook['background_process'])) {
  147. $legend .= 'R';
  148. $hook['log']['start'] = $hook['background_process']->start;
  149. $hook['log']['end'] = microtime(TRUE);
  150. }
  151. if (empty($hook['settings']['enabled'])) $legend .= 'D';
  152. $start = isset($hook['log']['start']) ? format_date((int)$hook['log']['start'], 'custom', 'Y-m-d H:i:s') : dt('N/A');
  153. $end = isset($hook['log']['end']) ? gmdate('H:i:s', (int)($hook['log']['end'] - $hook['log']['start'])) : dt('N/A');
  154. $rules = $hook['settings']['rules'];
  155. $table[] = array($legend, $hook['module'], $hook['function'], implode("\n", $rules), $start, $end);
  156. }
  157. drush_print_table($table);
  158. }
  159. /**
  160. * Run cron job(s)
  161. */
  162. function drush_ultimate_cron_cron_run($function = NULL) {
  163. $cli = drush_get_option('cli');
  164. $check_rule = drush_get_option('check-rule');
  165. $logfile = drush_get_option('logfile');
  166. $logfile = is_string($logfile) ? $logfile : '/dev/null';
  167. // Get global options
  168. $options = drush_get_context('cli');
  169. $cmd_options = '';
  170. // Determine new parameter string for sub-requests
  171. $passthru = array('root', 'php', 'uri', 'simulate');
  172. foreach ($options as $key => $option) {
  173. if (in_array($key, $passthru)) {
  174. $cmd_options .= ' --' . $key . '=' . escapeshellarg($option);
  175. }
  176. }
  177. if ($function == 'all') {
  178. if ($cli) {
  179. $hooks = ultimate_cron_get_hooks();
  180. $schedule = ultimate_cron_get_schedule($hooks);
  181. foreach ($schedule as $function => $hook) {
  182. if (!empty($options['simulate'])) {
  183. // Dry-run ...
  184. drush_print(dt('[!function]: Simulated launch @ !ts', array('!ts' => date('Y-m-d H:i:s'), '!function' => $function)));
  185. continue;
  186. }
  187. drush_print(dt('[!function]: Launching @ !ts', array('!ts' => date('Y-m-d H:i:s'), '!function' => $function)));
  188. // Launch the sub-request
  189. $cmd = $_SERVER['SCRIPT_FILENAME'] . " $cmd_options cron-run $function --cli " . ($check_rule ? '--check-rule' : '');
  190. exec("$cmd >> " . escapeshellarg($logfile) . " 2>&1 &");
  191. }
  192. drush_print(dt('[!ts] Launced !jobs jobs', array('!ts' => date('Y-m-d H:i:s'), '!jobs' => count($schedule))));
  193. // Update drupals cron timestamp, but don't clear the cache for all variables!
  194. if (empty($options['simulate'])) {
  195. $name = 'cron_last';
  196. $value = time();
  197. global $conf;
  198. db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
  199. $conf[$name] = $value;
  200. }
  201. return;
  202. }
  203. else {
  204. if (empty($options['simulate'])) {
  205. ultimate_cron_cron_run(TRUE);
  206. }
  207. $messages = drupal_get_messages();
  208. foreach ($messages['status'] as $message) {
  209. drush_print(strip_tags($message));
  210. }
  211. return;
  212. }
  213. }
  214. $hooks = ultimate_cron_get_hooks();
  215. if (!isset($hooks[$function])) {
  216. return drush_set_error(dt('[!function]: not found', array('!function' => $function)));
  217. }
  218. $hook = &$hooks[$function];
  219. // When run manually don't double check the rules
  220. if (drush_get_option('check-rule')) {
  221. $hook['log'] = ultimate_cron_get_log($function);
  222. if (!ultimate_cron_hook_should_run($hook)) {
  223. drush_print(dt("[!function]: not sceduled to run at this time", array('!function' => $function)));
  224. return;
  225. }
  226. }
  227. else {
  228. $hook['skip_catch_up'] = TRUE;
  229. }
  230. if (!empty($options['simulate'])) {
  231. // Dry-run ...
  232. drush_print("[$function]: Simulated run");
  233. return;
  234. }
  235. if (!empty($options['simulate'])) {
  236. // Dry-run ...
  237. drush_print("[$function]: Simulated run");
  238. return;
  239. }
  240. if ($cli) {
  241. $start = microtime(TRUE);
  242. $result = ultimate_cron_run_hook_cli($function, $hook);
  243. }
  244. else {
  245. $result = ultimate_cron_run_hook($function, $hook);
  246. }
  247. if ($result === FALSE) {
  248. return drush_set_error(dt('[!function]: could not start (already running?)', array('!function' => $function)));
  249. }
  250. if ($cli) {
  251. $log = ultimate_cron_get_log($function);
  252. if ($log['start'] >= $start && !empty($log['msg'])) {
  253. drush_print("[$function]: " . $log['msg']);
  254. }
  255. return $result ? NULL : drush_set_error(dt('[!function]: could not start (service unavailable)', array('!function' => $function)));
  256. }
  257. if ($result === NULL) {
  258. return drush_set_error(dt('[!function]: could not start (service unavailable)', array('!function' => $function)));
  259. }
  260. else {
  261. drush_print(dt('!function started', array('!function' => $function)));
  262. }
  263. }
  264. /**
  265. * Enable a cron job
  266. */
  267. function drush_ultimate_cron_cron_enable($function) {
  268. $hooks = ultimate_cron_get_hooks();
  269. if (!isset($hooks[$function])) {
  270. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  271. }
  272. $conf = ultimate_cron_get_settings($function);
  273. $conf['enabled'] = TRUE;
  274. ultimate_cron_set_settings($function, $conf);
  275. drush_print(dt('!function enabled', array('!function' => $function)));
  276. }
  277. /**
  278. * Disable a cron job
  279. */
  280. function drush_ultimate_cron_cron_disable($function) {
  281. $hooks = ultimate_cron_get_hooks();
  282. if (!isset($hooks[$function])) {
  283. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  284. }
  285. $conf = ultimate_cron_get_settings($function);
  286. $conf['enabled'] = FALSE;
  287. ultimate_cron_set_settings($function, $conf);
  288. drush_print(dt('!function disabled', array('!function' => $function)));
  289. }
  290. /**
  291. * Unlock a cron job
  292. */
  293. function drush_ultimate_cron_cron_unlock($function) {
  294. $hooks = ultimate_cron_get_hooks();
  295. if (!isset($hooks[$function])) {
  296. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  297. }
  298. $handle = 'uc:' . $function;
  299. if ($process = background_process_get_process($handle)) {
  300. // Unlock the process
  301. if (background_process_remove_process($process->handle, $process->start)) {
  302. drush_print(dt('Process for !function unlocked (process handle: !handle)', array('!handle' => $handle, '!function' => $function)));
  303. module_invoke_all('background_process_shutdown', $process, FALSE, t('Manually unlocked'));
  304. }
  305. }
  306. else {
  307. drush_set_error(dt('Process for !function not found (process handle: !handle)', array('!handle' => $handle, '!function' => $function)));
  308. }
  309. }