ultimate_cron.drush.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. if (!empty($messages['status'])) {
  209. foreach ($messages['status'] as $message) {
  210. drush_print(strip_tags($message));
  211. }
  212. }
  213. return;
  214. }
  215. }
  216. $hooks = ultimate_cron_get_hooks();
  217. if (!isset($hooks[$function])) {
  218. return drush_set_error(dt('[!function]: not found', array('!function' => $function)));
  219. }
  220. $hook = &$hooks[$function];
  221. // When run manually don't double check the rules
  222. if (drush_get_option('check-rule')) {
  223. $hook['log'] = ultimate_cron_get_log($function);
  224. if (!ultimate_cron_hook_should_run($hook)) {
  225. drush_print(dt("[!function]: not sceduled to run at this time", array('!function' => $function)));
  226. return;
  227. }
  228. }
  229. else {
  230. $hook['skip_catch_up'] = TRUE;
  231. }
  232. if (!empty($options['simulate'])) {
  233. // Dry-run ...
  234. drush_print("[$function]: Simulated run");
  235. return;
  236. }
  237. if (!empty($options['simulate'])) {
  238. // Dry-run ...
  239. drush_print("[$function]: Simulated run");
  240. return;
  241. }
  242. if ($cli) {
  243. $start = microtime(TRUE);
  244. $result = ultimate_cron_run_hook_cli($function, $hook);
  245. }
  246. else {
  247. $result = ultimate_cron_run_hook($function, $hook);
  248. }
  249. if ($result === FALSE) {
  250. return drush_set_error(dt('[!function]: could not start (already running?)', array('!function' => $function)));
  251. }
  252. if ($cli) {
  253. $log = ultimate_cron_get_log($function);
  254. if ($log['start'] >= $start && !empty($log['msg'])) {
  255. drush_print("[$function]: " . $log['msg']);
  256. }
  257. return $result ? NULL : drush_set_error(dt('[!function]: could not start (service unavailable)', array('!function' => $function)));
  258. }
  259. if ($result === NULL) {
  260. return drush_set_error(dt('[!function]: could not start (service unavailable)', array('!function' => $function)));
  261. }
  262. else {
  263. drush_print(dt('!function started', array('!function' => $function)));
  264. }
  265. }
  266. /**
  267. * Enable a cron job
  268. */
  269. function drush_ultimate_cron_cron_enable($function) {
  270. $hooks = ultimate_cron_get_hooks();
  271. if (!isset($hooks[$function])) {
  272. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  273. }
  274. $conf = ultimate_cron_get_settings($function);
  275. $conf['enabled'] = TRUE;
  276. ultimate_cron_set_settings($function, $conf);
  277. drush_print(dt('!function enabled', array('!function' => $function)));
  278. }
  279. /**
  280. * Disable a cron job
  281. */
  282. function drush_ultimate_cron_cron_disable($function) {
  283. $hooks = ultimate_cron_get_hooks();
  284. if (!isset($hooks[$function])) {
  285. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  286. }
  287. $conf = ultimate_cron_get_settings($function);
  288. $conf['enabled'] = FALSE;
  289. ultimate_cron_set_settings($function, $conf);
  290. drush_print(dt('!function disabled', array('!function' => $function)));
  291. }
  292. /**
  293. * Unlock a cron job
  294. */
  295. function drush_ultimate_cron_cron_unlock($function) {
  296. $hooks = ultimate_cron_get_hooks();
  297. if (!isset($hooks[$function])) {
  298. return drush_set_error(dt('"!function" not found', array('!function' => $function)));
  299. }
  300. $handle_prefix = variable_get('ultimate_cron_handle_prefix', ULTIMATE_CRON_HANDLE_PREFIX);
  301. $handle = $handle_prefix . $function;
  302. if ($process = background_process_get_process($handle)) {
  303. // Unlock the process
  304. if (background_process_remove_process($process->handle, $process->start)) {
  305. drush_print(dt('Process for !function unlocked (process handle: !handle)', array('!handle' => $handle, '!function' => $function)));
  306. module_invoke_all('background_process_shutdown', $process, FALSE, t('Manually unlocked'));
  307. }
  308. }
  309. else {
  310. drush_set_error(dt('Process for !function not found (process handle: !handle)', array('!handle' => $handle, '!function' => $function)));
  311. }
  312. }