ultimate_cron.module 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * @todo Add filter on overview page.
  6. * @todo Add log view (with graph).
  7. * @todo Make proper markup for overview page.
  8. * @todo Refactor drush stuff, too many intimate relations with Background Process
  9. * @todo Refactor Cron % offset stuff. Too mixed up and ungrokable code-wise and 'delta' is not consistent.
  10. *
  11. * hook_cron_alter(&$hooks)
  12. * hook_cron_schedule_alter(&$hooks)
  13. * hook_cron_pre_execute($name, $hook)
  14. * hook_cron_pre_execute_FUNCTION($hook)
  15. * hook_cron_post_execute($name, $hook)
  16. * hook_cron_post_execute_FUNCTION($hook)
  17. */
  18. /**
  19. * Maximum number of simultaneous connections.
  20. */
  21. define('ULTIMATE_CRON_SIMULTANEOUS_CONNECTIONS', 40);
  22. /**
  23. * Default rule.
  24. */
  25. define('ULTIMATE_CRON_RULE', '*/10+@ * * * *');
  26. /**
  27. * Default rule for easy hook "hourly".
  28. */
  29. define('ULTIMATE_CRON_HOURLY_RULE', '0 * * * *');
  30. /**
  31. * Default rule for easy hook "daily".
  32. */
  33. define('ULTIMATE_CRON_DAILY_RULE', '0 0 * * *');
  34. /**
  35. * Default rule for easy hook "weekly".
  36. */
  37. define('ULTIMATE_CRON_WEEKLY_RULE', '0 0 * * 1');
  38. /**
  39. * Default rule for easy hook "monthly".
  40. */
  41. define('ULTIMATE_CRON_MONTHLY_RULE', '0 0 1 * *');
  42. /**
  43. * Default rule for easy hook "yearly".
  44. */
  45. define('ULTIMATE_CRON_YEARLY_RULE', '0 0 1 1 *');
  46. /**
  47. * Default max execution time for Ultimate Cron.
  48. */
  49. define('ULTIMATE_CRON_MAX_EXECUTION_TIME', 86400);
  50. /**
  51. * Default catch up time for Ultimate Cron.
  52. */
  53. define('ULTIMATE_CRON_CATCH_UP', 300);
  54. /**
  55. * Default lease time for Ultimate Cron queues.
  56. */
  57. define('ULTIMATE_CRON_QUEUE_LEASE_TIME', 30);
  58. /**
  59. * Default clean up time for log entries (30 days).
  60. */
  61. define('ULTIMATE_CRON_CLEANUP_LOG', 86400 * 30);
  62. /**
  63. * Default setting for poorman.
  64. */
  65. define('ULTIMATE_CRON_POORMAN', TRUE);
  66. /**
  67. * Default queue polling latency.
  68. */
  69. define('ULTIMATE_CRON_QUEUE_POLLING_LATENCY', '');
  70. /**
  71. * Time in seconds to spend on launcing cron jobs.
  72. */
  73. define('ULTIMATE_CRON_LAUNCH_WINDOW', 55);
  74. /**
  75. * Time in seconds to spend on launcing cron jobs.
  76. */
  77. define('ULTIMATE_CRON_SERVICE_GROUP', 'default');
  78. // ---------- HOOKS ----------
  79. module_load_include('nagios.inc', 'ultimate_cron');
  80. /**
  81. * Implements hook_help().
  82. */
  83. function ultimate_cron_help($path, $arg) {
  84. switch ($path) {
  85. case 'admin/help#ultimate_cron':
  86. // Return a line-break version of the module README
  87. return '<pre>' . file_get_contents(dirname(__FILE__) . '/README.txt') . '</pre>';
  88. case 'admin/build/cron':
  89. return '<p>' . t('Here you can see the crontab settings for each job available') . '</p>';
  90. case 'admin/build/cron/settings':
  91. return '<p>' . t('Here you can change the crontab settings for each job available') . '</p>';
  92. }
  93. }
  94. /**
  95. * Implements hook_menu().
  96. */
  97. function ultimate_cron_menu() {
  98. $items = array();
  99. $items['admin/config/system/cron/settings'] = array(
  100. 'title' => 'Settings',
  101. 'description' => 'Cron settings',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('ultimate_cron_settings_form'),
  104. 'access arguments' => array('administer ultimate cron'),
  105. 'type' => MENU_LOCAL_TASK,
  106. 'file' => 'ultimate_cron.admin.inc',
  107. );
  108. $items['admin/config/system/cron/settings/%'] = array(
  109. 'title' => 'Settings',
  110. 'page callback' => 'drupal_get_form',
  111. 'page arguments' => array('ultimate_cron_function_settings_form', 5),
  112. 'access arguments' => array('administer ultimate cron'),
  113. 'weight' => 0,
  114. 'file' => 'ultimate_cron.admin.inc',
  115. );
  116. $items['admin/reports/cron'] = array(
  117. 'title' => 'Cron logs',
  118. 'description' => 'View logs for all cron jobs.',
  119. 'page callback' => 'ultimate_cron_view_page',
  120. 'access arguments' => array('administer ultimate cron'),
  121. 'file' => 'ultimate_cron.admin.inc',
  122. );
  123. $items['admin/reports/cron/%'] = array(
  124. 'title' => 'Cron log',
  125. 'description' => 'View log for specific function.',
  126. 'page callback' => 'ultimate_cron_function_log_page',
  127. 'page arguments' => array(3),
  128. 'access arguments' => array('administer ultimate cron'),
  129. 'file' => 'ultimate_cron.admin.inc',
  130. );
  131. $items['admin/ultimate-cron/service/start/%'] = array(
  132. 'type' => MENU_CALLBACK,
  133. 'title' => 'Run cron job',
  134. 'description' => 'Run cron job',
  135. 'page callback' => 'ultimate_cron_service_start',
  136. 'page arguments' => array(4),
  137. 'access arguments' => array('administer ultimate cron'),
  138. 'file' => 'ultimate_cron.admin.inc',
  139. );
  140. $items['admin/ultimate-cron/service/enable/%'] = array(
  141. 'type' => MENU_CALLBACK,
  142. 'title' => 'Enable cron job',
  143. 'description' => 'Enable cron job',
  144. 'page callback' => 'ultimate_cron_service_enable',
  145. 'page arguments' => array(4, TRUE),
  146. 'access arguments' => array('administer ultimate cron'),
  147. 'file' => 'ultimate_cron.admin.inc',
  148. );
  149. $items['admin/ultimate-cron/service/disable/%'] = array(
  150. 'type' => MENU_CALLBACK,
  151. 'title' => 'Disable cron job',
  152. 'description' => 'Disable cron job',
  153. 'page callback' => 'ultimate_cron_service_enable',
  154. 'page arguments' => array(4, FALSE),
  155. 'access arguments' => array('administer ultimate cron'),
  156. 'file' => 'ultimate_cron.admin.inc',
  157. );
  158. $items['admin/ultimate-cron/service/process-status'] = array(
  159. 'type' => MENU_CALLBACK,
  160. 'title' => 'Cron job process status',
  161. 'description' => 'Cron job process status',
  162. 'page callback' => 'ultimate_cron_service_process_status',
  163. 'access arguments' => array('administer ultimate cron'),
  164. 'file' => 'ultimate_cron.admin.inc',
  165. );
  166. return $items;
  167. }
  168. /**
  169. * Implements hook_cron_queue_info().
  170. * Used for code injection in order to hijack cron runs.
  171. */
  172. function ultimate_cron_cron_queue_info() {
  173. static $processed = FALSE;
  174. if (!$processed) {
  175. $processed = TRUE;
  176. if (basename($_SERVER['PHP_SELF']) == 'cron.php') {
  177. ultimate_cron_cron_run(FALSE);
  178. exit;
  179. }
  180. }
  181. return array();
  182. }
  183. /**
  184. * Implements hook_permission().
  185. */
  186. function ultimate_cron_permission() {
  187. return array(
  188. 'administer ultimate cron' => array(
  189. 'title' => t('Administer Ultimate Cron'),
  190. 'description' => t('Lets you configure everything in Ultimate Cron')
  191. )
  192. );
  193. }
  194. /**
  195. * The cron handler takes over the normal Drupal cron handler
  196. * and runs the normal hook_cron() plus the hook_cronapi().
  197. *
  198. * @param boolean $return
  199. * return to caller if TRUE, otherwise exit().
  200. */
  201. function ultimate_cron_cron_run($return = FALSE) {
  202. if (variable_get('install_task', FALSE) != 'done') {
  203. return;
  204. }
  205. // Be other cron module friendly
  206. if (_ultimate_cron_incompatible_modules()) {
  207. return;
  208. }
  209. // If run from core cron through CLI then don't do anything (drush core-cron)
  210. if (!$return && drupal_is_cli()) {
  211. return;
  212. }
  213. // Acquire lock
  214. if (!lock_acquire('cron', 240.0)) {
  215. drupal_set_message(t('Ultimate Cron launcher already running'), 'error');
  216. return;
  217. }
  218. $msc = variable_get('ultimate_cron_simultaneous_connections', ULTIMATE_CRON_SIMULTANEOUS_CONNECTIONS);
  219. // Get list of cron hooks.
  220. $hooks = ultimate_cron_get_hooks();
  221. // Get schedule.
  222. $schedule = ultimate_cron_get_schedule($hooks);
  223. drupal_set_message(t('%jobs jobs scheduled for launch', array('%jobs' => count($schedule))));
  224. // Start the jobs. Keep launching jobs until X seconds into the request.
  225. set_time_limit(120);
  226. $time = time();
  227. $expire = $time + variable_get('ultimate_cron_launch_window', ULTIMATE_CRON_LAUNCH_WINDOW);
  228. $running = array();
  229. $launched = 0;
  230. // Try to launch jobs within the given time frame
  231. while (!empty($schedule) && time() < $expire) {
  232. $running_processes = db_select('background_process', 'bp')
  233. ->fields('bp', array('handle'))
  234. ->condition('bp.handle', 'uc:%', 'LIKE')
  235. ->countQuery()
  236. ->execute()
  237. ->fetchField();
  238. // Launch jobs.
  239. reset($schedule);
  240. while ((list($name, $hook) = each($schedule)) && time() < $expire) {
  241. // Congestion protection
  242. if (empty($hook['override_congestion_protection']) && $running_processes >= $msc) {
  243. continue;
  244. }
  245. if (empty($hook['force_run']) && !ultimate_cron_hook_should_run($hook)) {
  246. unset($schedule[$name]);
  247. continue;
  248. }
  249. $result = ultimate_cron_run_hook($name, $hook);
  250. // Handle errors.
  251. if ($result) {
  252. $handle = 'uc:' . $name;
  253. $running[$handle] = $result;
  254. unset($schedule[$name]);
  255. $launched++;
  256. $running_processes++;
  257. }
  258. else {
  259. if ($result === FALSE) {
  260. // Could not get lock, skip job.
  261. unset($schedule[$name]);
  262. }
  263. else {
  264. // Failed to start, retry next time.
  265. watchdog('ultimate_cron', "Error starting $name", array(), WATCHDOG_WARNING);
  266. }
  267. }
  268. }
  269. // Jobs running ... check for start
  270. if ($running) {
  271. $result = db_query("SELECT p.name FROM {progress} p WHERE p.name IN (:running)", array(':running' => array_keys($running)));
  272. while ($handle = $result->fetchObject()) {
  273. fclose($running[$handle->name]);
  274. unset($running[$handle->name]);
  275. }
  276. }
  277. sleep(1);
  278. }
  279. // Close all jobs left
  280. if ($running) {
  281. foreach (array_keys($running) as $handle) {
  282. fclose($running[$handle]);
  283. unset($running[$handle]);
  284. }
  285. }
  286. // Update drupals cron timestamp, but don't clear the cache for all variables!
  287. $name = 'cron_last';
  288. $value = time();
  289. global $conf;
  290. db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
  291. $conf[$name] = $value;
  292. drupal_set_message(t('%jobs jobs launched', array('%jobs' => $launched)));
  293. if (count($schedule)) {
  294. drupal_set_message(t('%jobs jobs failed to launch within %seconds seconds', array(
  295. '%jobs' => count($schedule),
  296. '%seconds' => variable_get('ultimate_cron_launch_window', ULTIMATE_CRON_LAUNCH_WINDOW)
  297. )), empty($schedule) ? 'status' : 'error');
  298. watchdog('ultimate_cron', '%jobs jobs failed to launch within %seconds seconds', array(
  299. '%jobs' => count($schedule),
  300. '%seconds' => variable_get('ultimate_cron_launch_window', ULTIMATE_CRON_LAUNCH_WINDOW)
  301. ), WATCHDOG_WARNING);
  302. }
  303. // Release the lock
  304. lock_release('cron');
  305. // And we're done ...
  306. if ($return) {
  307. return empty($schedule);
  308. }
  309. else {
  310. exit;
  311. }
  312. }
  313. /**
  314. * Implements hook_cronapi().
  315. */
  316. function ultimate_cron_cronapi($op, $job = NULL, $hook = NULL) {
  317. // Grab the defined cron queues.
  318. static $queues = NULL;
  319. if (!isset($queues)) {
  320. $queues = module_invoke_all('cron_queue_info');
  321. drupal_alter('cron_queue_info', $queues);
  322. }
  323. switch ($op) {
  324. case 'list':
  325. $jobs['ultimate_cron_cleanup_log'] = t('Cleanup log entries');
  326. foreach ($queues as $queue_name => $info) {
  327. $jobs['ultimate_cron_queue_' . $queue_name] = t('Queue: %name', array('%name' => $queue_name));
  328. }
  329. return $jobs;
  330. case 'rule':
  331. $queue_name = preg_replace('/^ultimate_cron_queue_/', '', $job);
  332. if ($queue_name === $job) {
  333. return;
  334. }
  335. return '* * * * *';
  336. case 'execute':
  337. $queue_name = preg_replace('/^ultimate_cron_queue_/', '', $job);
  338. if ($queue_name === $job) {
  339. return;
  340. }
  341. $polling_latency = variable_get('ultimate_cron_queue_polling_latency', ULTIMATE_CRON_QUEUE_POLLING_LATENCY);
  342. $info = $queues[$queue_name];
  343. $function = $info['worker callback'];
  344. $end = time() + (isset($info['time']) ? $info['time'] : 15);
  345. $queue = DrupalQueue::get($queue_name);
  346. $items = 0;
  347. while (time() < $end) {
  348. $lease_time = isset($hook['settings']['queue_lease_time']) ? $hook['settings']['queue_lease_time'] : variable_get('ultimate_cron_queue_lease_time', ULTIMATE_CRON_QUEUE_LEASE_TIME);
  349. $item = $queue->claimItem($lease_time);
  350. if (!$item) {
  351. if (is_numeric($polling_latency)) {
  352. usleep($polling_latency * 1000);
  353. continue;
  354. }
  355. else {
  356. break;
  357. }
  358. }
  359. $function($item->data);
  360. $queue->deleteItem($item);
  361. $items++;
  362. }
  363. drupal_set_message(t('Processed @items items', array('@items' => $items)));
  364. if (is_numeric($polling_latency)) {
  365. $settings = ultimate_cron_get_settings($job);
  366. $hook['settings'] = $settings + $hook['settings'];
  367. if (ultimate_cron_hook_should_run($hook)) {
  368. background_process_keepalive();
  369. }
  370. }
  371. return;
  372. }
  373. }
  374. /**
  375. * Implements hook_progress_message_alter().
  376. */
  377. function ultimate_cron_progress_message_alter(&$message) {
  378. $pseudo_process = new stdClass();
  379. $pseudo_process->handle = $message->data->progress->name;
  380. $pseudo_process->start_stamp = $message->data->progress->start_stamp;
  381. $pseudo_process->progress = $message->data->progress->progress;
  382. $pseudo_process->exec_status = 2;
  383. $message->data->background_process = $pseudo_process;
  384. return ultimate_cron_background_process_message_alter($message);
  385. }
  386. /**
  387. * Implements hook_background_process_message_alter().
  388. */
  389. function ultimate_cron_background_process_message_alter(&$message) {
  390. // Only alter Ultimate Cron background process messages
  391. if (substr($message->data->background_process->handle, 0, 3) !== 'uc:') {
  392. return;
  393. }
  394. $message->data->ultimate_cron['start_stamp'] = format_date((int)$message->data->background_process->start_stamp, 'custom', 'Y-m-d H:i:s');
  395. if ($message->data->background_process->progress >= 0) {
  396. $message->data->ultimate_cron['progress'] = gmdate('H:i:s', (int)(microtime(TRUE) - $message->data->background_process->start_stamp)) . sprintf(" (%3d%%)", $message->data->background_process->progress * 100);
  397. }
  398. $message->data->ultimate_cron['unlockURL'] = url('background-process/unlock/' . $message->data->background_process->handle, array('query' => array('destination' => 'admin/config/system/cron')));
  399. }
  400. /**
  401. * Implements hook_theme_registry_alter().
  402. */
  403. function ultimate_cron_theme_registry_alter(&$theme_registry) {
  404. $theme_registry['page']['theme paths'][] = drupal_get_path('module', 'ultimate_cron') . '/templates';
  405. }
  406. /**
  407. * Implements hook_watchdog().
  408. */
  409. function ultimate_cron_watchdog($log = array()) {
  410. $record = &drupal_static('ultimate_cron_record', FALSE);
  411. if ($record && $log['severity'] <= WATCHDOG_WARNING) {
  412. $log['variables'] = is_array($log['variables']) ? $log['variables'] : array();
  413. ultimate_cron_record_log(t($log['message'], $log['variables']));
  414. }
  415. }
  416. /**
  417. * Implements hook_init().
  418. */
  419. function ultimate_cron_init() {
  420. // No need for hocus pocus and poorman until site is installed.
  421. if (variable_get('install_task') != 'done') {
  422. return;
  423. }
  424. $name = 'cron_last';
  425. if ($value = db_query("SELECT value FROM {variable} WHERE name = :name", array(':name' => $name))->fetchField()) {
  426. $value = unserialize($value);
  427. }
  428. global $conf;
  429. $conf['cron_last'] = $value;
  430. ultimate_cron_trigger_poorman();
  431. }
  432. /**
  433. * Launch poorman cron if it's time to do so.
  434. */
  435. function ultimate_cron_trigger_poorman() {
  436. // Launch poormans cron if applicable
  437. if (variable_get('ultimate_cron_poorman', ULTIMATE_CRON_POORMAN) && !_ultimate_cron_incompatible_modules()) {
  438. $last = variable_get('cron_last', 0);
  439. $last = floor($last / 60) * 60;
  440. $time = time();
  441. $time = floor($time / 60) * 60;
  442. // Don't attempt, if already run within last minute
  443. if ($last < $time) {
  444. ultimate_cron_launch_poorman();
  445. }
  446. }
  447. }
  448. /**
  449. * Launch the poormans cron background process.
  450. */
  451. function ultimate_cron_launch_poorman() {
  452. $handle = 'ultimate_cron_poorman';
  453. if ($process = background_process_get_process($handle)) {
  454. if ($process->start + 120 < time()) {
  455. // Must have timed out or something ... remove it!
  456. if (background_process_remove_process($handle, $process->start)) {
  457. $process = NULL;
  458. }
  459. }
  460. }
  461. if (!$process) {
  462. $process = new BackgroundProcess($handle);
  463. // Because anyone can launch poormans cron, run it as anonymous
  464. $process->uid = 0;
  465. $process->service_host = 'ultimate_cron_poorman';
  466. $result = $process->start('_ultimate_cron_poorman');
  467. }
  468. }
  469. /**
  470. * Implements hook_background_process_shutdown().
  471. *
  472. * Shutdown handler for cronjobs.
  473. */
  474. function ultimate_cron_background_process_shutdown($process, $shutdown_msg = NULL) {
  475. $args = func_get_args();
  476. $name = preg_replace('/^uc:/', '', $process->handle);
  477. if (!empty($name) && $name != $process->handle) {
  478. static $has_run = array();
  479. if (!empty($has_run[$name])) {
  480. return;
  481. }
  482. $has_run[$name] = TRUE;
  483. // Record end time
  484. $end = microtime(TRUE);
  485. $fatal_msg = '';
  486. if (count(func_get_args()) == 1) {
  487. $error = error_get_last();
  488. if ($error) {
  489. if ($error['type'] & (E_ERROR | E_CORE_ERROR | E_RECOVERABLE_ERROR)) {
  490. $fatal_msg = $error['message'] . ' (line ' . $error['line'] . ' of ' . $error['file'] . ').' . "\n";
  491. }
  492. else {
  493. $fatal_msg .= t('Died unexpectedly');
  494. }
  495. }
  496. }
  497. if (is_null($shutdown_msg)) {
  498. $messages = drupal_get_messages();
  499. }
  500. $msg = ultimate_cron_record_log(NULL);
  501. if ($shutdown_msg) {
  502. $msg .= $msg ? "\n$shutdown_msg" : $shutdown_msg;
  503. }
  504. if (!empty($messages['error'])) {
  505. $msg .= join("\n", $messages['error']) . "\n";
  506. }
  507. $msg .= $fatal_msg;
  508. // If success, use status messages if any
  509. $result = empty($msg);
  510. if ($result && !empty($messages['status'])) {
  511. $msg = join("\n", $messages['status']);
  512. }
  513. // log results here ...
  514. $object = (object)array(
  515. 'name' => $name,
  516. 'start_stamp' => $process->start,
  517. 'end_stamp' => $end,
  518. 'service_host' => $process->service_host,
  519. 'exec_status' => $result,
  520. 'msg' => trim($msg),
  521. );
  522. $old_db = db_set_active('background_process');
  523. drupal_write_record('ultimate_cron_log', $object);
  524. db_set_active($old_db);
  525. $object->formatted['start_stamp'] = format_date((int)$object->start_stamp, 'custom', 'Y-m-d H:i:s');
  526. $object->formatted['end_stamp'] = format_date((int)$object->end_stamp, 'custom', 'Y-m-d H:i:s');
  527. $object->formatted['duration'] = gmdate('H:i:s', (int)($object->end_stamp - $object->start_stamp));
  528. $object->formatted['severity'] = $object->exec_status ? 'noerror' : 'error';
  529. $object->formatted['executeURL'] = url('admin/ultimate-cron/service/start/' . $object->name, array('query' => array('destination' => 'admin/config/system/cron')));
  530. $object->formatted['msg'] = strip_tags(html_entity_decode($object->msg, ENT_QUOTES));
  531. if (module_exists('nodejs')) {
  532. $message = (object) array(
  533. 'channel' => 'ultimate_cron',
  534. 'data' => (object) array(
  535. 'action' => 'log',
  536. 'log' => $object,
  537. ),
  538. 'callback' => 'nodejsUltimateCron',
  539. );
  540. nodejs_send_content_channel_message($message);
  541. }
  542. }
  543. }
  544. // ---------- FIXUPS FOR CORE ----------
  545. /**
  546. * Implements hook_menu_alter().
  547. *
  548. * Steal the run-cron, so when you "run cron manually" from the status-reports
  549. * page the ultimate_cron cron handler is run.
  550. */
  551. function ultimate_cron_menu_alter(&$items) {
  552. if (_ultimate_cron_incompatible_modules()) {
  553. return;
  554. }
  555. $items['admin/config/system/cron'] = array(
  556. 'title' => 'Cron',
  557. 'description' => 'View and manage cron table',
  558. 'page callback' => 'ultimate_cron_view_page',
  559. 'access arguments' => array('administer ultimate cron'),
  560. 'module' => 'ultimate_cron',
  561. 'file' => 'ultimate_cron.admin.inc',
  562. );
  563. $items['admin/config/system/cron/overview'] = array(
  564. 'title' => 'List',
  565. 'type' => MENU_DEFAULT_LOCAL_TASK,
  566. 'weight' => -10,
  567. );
  568. $steal = &$items['admin/reports/status/run-cron'];
  569. $steal['page callback'] = 'ultimate_cron_run_cron';
  570. $steal['page arguments'] = array();
  571. $steal['module'] = 'ultimate_cron';
  572. $steal['file'] = 'ultimate_cron.admin.inc';
  573. }
  574. /**
  575. * Implements hook_cron_alter().
  576. * Add better description to core modules.
  577. */
  578. function ultimate_cron_cron_alter(&$hooks) {
  579. $update['dblog_cron']['description'] = t('Remove expired log messages and flood control events');
  580. $update['field_cron']['description'] = t('Purges deleted Field API data');
  581. $update['filter_cron']['description'] = t('Expire outdated filter cache entries');
  582. $update['node_cron']['description'] = t('Mark old nodes as read');
  583. $update['search_cron']['description'] = t('Update indexes');
  584. $update['system_cron']['description'] = t('Cleanup (batch, flood, temp-files, etc.)');
  585. $update['aggregator_cron']['description'] = t('Refresh feeds');
  586. $update['openid_cron']['description'] = t('Remove expired nonces from the database');
  587. $update['ping_cron']['description'] = t('Notify remote sites');
  588. $update['poll_cron']['description'] = t('Close expired polls');
  589. $update['statistics_cron']['description'] = t('Reset counts and clean up');
  590. $update['trigger_cron']['description'] = t('Run actions for cron triggers');
  591. $update['tracker_cron']['description'] = t('Update tracker index');
  592. $update['update_cron']['description'] = t('Check system for updates');
  593. $update['ultimate_cron_cleanup_log']['configure'] = 'admin/config/system/cron/settings';
  594. $update['dblog_cron']['configure'] = 'admin/config/development/logging';
  595. foreach ($update as $name => $data) {
  596. if (isset($hooks[$name])) {
  597. foreach ($data as $key => $value) {
  598. $hooks[$name][$key] = $value;
  599. }
  600. }
  601. }
  602. }
  603. // ---------- HELPER FUNCTIONS ----------
  604. /**
  605. * Set current hook being processed
  606. *
  607. * @param $hook
  608. * @return
  609. * Current hook
  610. */
  611. function ultimate_cron_set_current_hook($hook = NULL) {
  612. static $current_hook;
  613. if ($hook) {
  614. $current_hook = $hook;
  615. }
  616. return $current_hook;
  617. }
  618. /**
  619. * Get current hook being processed
  620. *
  621. * @return
  622. * Current hook
  623. */
  624. function ultimate_cron_get_current_hook() {
  625. return ultimate_cron_set_current_hook();
  626. }
  627. /**
  628. * The actual poorman function
  629. * @return type
  630. */
  631. function _ultimate_cron_poorman() {
  632. if (!variable_get('ultimate_cron_poorman', ULTIMATE_CRON_POORMAN) || _ultimate_cron_incompatible_modules()) {
  633. return;
  634. }
  635. // Restart when done
  636. background_process_keepalive();
  637. // Derive current minute
  638. $time = time();
  639. $time = floor($time / 60) * 60;
  640. // Run the cron
  641. ultimate_cron_cron_run(TRUE);
  642. // Wait until end of "current" minute
  643. $wait = $time + 60 - time();
  644. if ($wait > 0 && $wait <= 60) {
  645. sleep($wait);
  646. }
  647. }
  648. /**
  649. * Clean up log entries.
  650. */
  651. function ultimate_cron_cleanup_log() {
  652. do {
  653. $result = db_query_range("SELECT lid FROM {ultimate_cron_log} WHERE start_stamp < :start", 0, 1000, array(':start' => time() - variable_get('ultimate_cron_cleanup_log', ULTIMATE_CRON_CLEANUP_LOG)));
  654. $lids = array();
  655. while ($row = $result->fetchObject()) {
  656. $lids[] = $row->lid;
  657. }
  658. if (!empty($lids)) {
  659. db_query("DELETE FROM {ultimate_cron_log} WHERE lid IN (:lids)", array(':lids' => $lids));
  660. }
  661. } while (!empty($lids));
  662. }
  663. /**
  664. * Run a cron hook.
  665. * Launches the cron job in a background process
  666. *
  667. * @param $name
  668. * @param $hook
  669. * @return mixed
  670. * Connections file handle on success.
  671. */
  672. function ultimate_cron_run_hook($name, $hook) {
  673. // Run the job in background
  674. $result = NULL;
  675. $handle = "uc:" . $name;
  676. $process = new BackgroundProcess($handle);
  677. // Always run cron job as anonymous user
  678. $process->uid = 0;
  679. // Determine service group
  680. $process->service_group = empty($hook['settings']['service_group']) ? variable_get('ultimate_cron_service_group', ULTIMATE_CRON_SERVICE_GROUP) : $hook['settings']['service_group'];
  681. $hook['timestamp'] = time();
  682. unset($hook['log']['msg']);
  683. $result = $process->start('_ultimate_cron_run_hook', array($name, $hook));
  684. return $result ? $process->connection : $result;
  685. }
  686. function ultimate_cron_run_hook_cli($name, $hook) {
  687. // Run the job in background
  688. $result = NULL;
  689. $handle = "uc:" . $name;
  690. $process = new BackgroundProcess($handle);
  691. // Always run cron job as anonymous user
  692. $process->uid = 0;
  693. $hook['timestamp'] = time();
  694. if ($process->lock()) {
  695. try {
  696. $process_obj = background_process_get_process($handle);
  697. $process->start = $process_obj->start;
  698. if (function_exists('background_process_update_status')) {
  699. background_process_update_status($handle, BACKGROUND_PROCESS_STATUS_RUNNING);
  700. }
  701. else {
  702. db_update('background_process')
  703. ->fields(array(
  704. 'exec_status' => BACKGROUND_PROCESS_STATUS_RUNNING,
  705. ))
  706. ->condition('handle', $handle)
  707. ->execute();
  708. }
  709. $old_handle = background_process_current_handle();
  710. background_process_current_handle($handle);
  711. _ultimate_cron_run_hook($name, $hook);
  712. module_invoke_all('background_process_shutdown', $process);
  713. background_process_current_handle($old_handle);
  714. background_process_remove_process($handle, $process->start);
  715. return TRUE;
  716. }
  717. catch (Exception $e) {
  718. module_invoke_all('background_process_shutdown', $process, (string)$e);
  719. return NULL;
  720. }
  721. }
  722. return FALSE;
  723. }
  724. /**
  725. * This is the function that is launched into a background process.
  726. * It runs the cron job and does housekeeping, pre/post execute hooks, etc.
  727. *
  728. * @param $module
  729. * Module containing function.
  730. * @param $name
  731. * Function to call.
  732. * @return boolean
  733. * TRUE on success, FALSE on failure.
  734. */
  735. function _ultimate_cron_run_hook($name, $hook) {
  736. set_time_limit(variable_get('ultimate_cron_max_execution_time', ULTIMATE_CRON_MAX_EXECUTION_TIME));
  737. drupal_save_session(FALSE);
  738. // Load current process
  739. $process = background_process_get_process(background_process_current_handle());
  740. $record = &drupal_static('ultimate_cron_record', FALSE);
  741. $record = TRUE;
  742. ultimate_cron_record_log(NULL, TRUE);
  743. // Load log if not present
  744. if (!isset($hook['log'])) {
  745. $hook['log'] = ultimate_cron_get_log($name);
  746. }
  747. $time = time();
  748. if (empty($hook['skip_catch_up']) && !ultimate_cron_hook_should_run($hook)) {
  749. // Hook started too late!
  750. watchdog('ultimate_cron', '%function skipped. Invoked at %invoke, but did not start until %start', array(
  751. '%function' => $name,
  752. '%invoke' => format_date($hook['timestamp'], 'custom', 'Y-m-d H:i:s'),
  753. '%start' => format_date($time, 'custom', 'Y-m-d H:i:s'),
  754. ), WATCHDOG_ERROR);
  755. ultimate_cron_background_process_shutdown($process, NULL);
  756. return FALSE;
  757. }
  758. // Let other modules do stuff before execution, if they need to.
  759. module_invoke_all('cron_pre_execute', $name, $hook);
  760. module_invoke_all('cron_pre_execute_' . $name, $hook);
  761. if (!empty($hook['file'])) {
  762. include_once $hook['file'];
  763. }
  764. $callback = $hook['callback'];
  765. ultimate_cron_set_current_hook($hook);
  766. if (is_callable($callback)) {
  767. call_user_func($callback);
  768. }
  769. else {
  770. module_invoke($hook['module'], 'cronapi', 'execute', $name, $hook);
  771. }
  772. ultimate_cron_background_process_shutdown($process, NULL);
  773. // Let other modules do stuff before execution, if they need to.
  774. module_invoke_all('cron_post_execute', $name, $hook);
  775. module_invoke_all('cron_post_execute_' . $name, $hook);
  776. return TRUE;
  777. }
  778. /**
  779. * Get a list of functions that should be run now.
  780. *
  781. * @param $hooks
  782. * Array of cron hooks to check.
  783. * @return array
  784. * Functions to run now.
  785. */
  786. function ultimate_cron_get_schedule($hooks) {
  787. // Create list of scheduled functions
  788. $schedule = array();
  789. foreach ($hooks as $name => &$hook) {
  790. ultimate_cron_load_hook_data($hook);
  791. // Store last run in hook for sorting purposes
  792. $last_run = isset($hook['log']['start']) ? $hook['log']['start'] : 0;
  793. $hook['last_run'] = $last_run;
  794. if (ultimate_cron_hook_should_run($hook)) {
  795. $schedule[$name] = $hook;
  796. }
  797. }
  798. // Sort by last run time
  799. uasort($schedule, '_ultimate_cron_sort_schedule');
  800. // Allow other to manipulate the schedule
  801. drupal_alter('cron_schedule', $schedule);
  802. return $schedule;
  803. }
  804. /**
  805. * Populate hook array with settings and log data
  806. *
  807. * @param type $hook
  808. */
  809. function ultimate_cron_load_hook_data(&$hook) {
  810. // Get settings
  811. $hook['settings'] = ultimate_cron_get_settings($hook['function']) + $hook['settings'];
  812. // Get log, used for checking last start time
  813. $hook['log'] = ultimate_cron_get_log($hook['function']);
  814. }
  815. /**
  816. * Check if a hook should be run now.
  817. *
  818. * @param array $hook
  819. * @return boolean
  820. */
  821. function ultimate_cron_hook_should_run($hook) {
  822. // Is it enabled?
  823. if (empty($hook['settings']['enabled'])) {
  824. return FALSE;
  825. }
  826. $last_run = isset($hook['log']['start']) ? $hook['log']['start'] : 0;
  827. return ultimate_cron_should_run($hook['settings']['rules'], $last_run, time(), $hook['settings']['catch_up'], $hook['delta']);
  828. }
  829. /**
  830. * Sort callback for ordering schedule.
  831. *
  832. * @param type $a
  833. * @param type $b
  834. * @return type
  835. */
  836. function _ultimate_cron_sort_schedule($a, $b) {
  837. return $a['last_run'] == $b['last_run'] ? 0 : ($a['last_run'] < $b['last_run'] ? -1 : 1);
  838. }
  839. /**
  840. * Get cron hooks available.
  841. *
  842. * @return array
  843. * List of modules.
  844. */
  845. function ultimate_cron_get_hooks() {
  846. static $hooks = NULL;
  847. if (isset($hooks)) {
  848. return $hooks;
  849. }
  850. $hooks = array();
  851. $delta = 0;
  852. // Generate list of hooks
  853. $modules = module_list();
  854. foreach ($modules as $module) {
  855. $file = drupal_get_path('module', $module) . '/' . $module . '.info';
  856. $info = drupal_parse_info_file($file);
  857. foreach (ultimate_cron_easy_hooks() as $hook => $description) {
  858. if (module_hook($module, $hook)) {
  859. $name = $module . '_' . $hook;
  860. $hooks[$name]['description'] = $description;
  861. $hooks[$name]['module'] = $module;
  862. $hooks[$name]['configure'] = isset($info['configure']) ? $info['configure'] : '';
  863. $hooks[$name]['settings'] = ultimate_cron_get_default_settings($module, $name, ultimate_cron_easy_hooks_rule($hook));
  864. }
  865. }
  866. if ($cronapi = module_invoke($module, 'cronapi', 'list')) {
  867. foreach ($cronapi as $name => $description) {
  868. $hooks[$name]['description'] = $description;
  869. $hooks[$name]['module'] = $module;
  870. $hooks[$name]['settings'] = ultimate_cron_get_default_settings($module, $name, ultimate_cron_easy_hooks_rule('cron'));
  871. $hooks[$name]['configure'] = module_invoke($module, 'cronapi', 'configure', $name);
  872. }
  873. }
  874. }
  875. foreach ($hooks as $name => &$hook) {
  876. $hook['callback'] = $hook['function'] = $name;
  877. $hook['background_process'] = array();
  878. $hook['delta'] = $delta++;
  879. }
  880. // Remove ourselves from the list
  881. unset($hooks['ultimate_cron_cron']);
  882. // Allow other to manipulate the hook list
  883. drupal_alter('cron', $hooks);
  884. return $hooks;
  885. }
  886. /**
  887. * Get default settings for job.
  888. *
  889. * @param type $module
  890. * @param type $name
  891. * @return array
  892. */
  893. function ultimate_cron_get_default_settings($module, $name, $default_rule) {
  894. $conf = module_invoke($module, 'cronapi', 'settings', $name);
  895. if (!is_array($conf)) {
  896. $conf = array();
  897. }
  898. $rule = module_invoke($module, 'cronapi', 'rule', $name);
  899. if (empty($conf['rules']) && !empty($rule)) {
  900. $conf['rules'] = is_array($rule) ? $rule : array($rule);
  901. }
  902. $conf += _ultimate_cron_default_settings($default_rule);
  903. return $conf;
  904. }
  905. /**
  906. * Get settings for a function.
  907. *
  908. * @param $name
  909. * @return array
  910. * Settings for function
  911. */
  912. function ultimate_cron_get_settings($name) {
  913. $settings = array();
  914. if (module_exists('ctools')) {
  915. ctools_include('export');
  916. $function = ctools_export_crud_load('ultimate_cron', $name);
  917. if ($function) {
  918. $settings = (array) $function->settings;
  919. }
  920. }
  921. else {
  922. $function = db_query("SELECT settings FROM {ultimate_cron} WHERE name = :name", array(':name' => $name))->fetchObject();
  923. if (!empty($function) && !empty($function->settings)) {
  924. $settings = (array) unserialize($function->settings);
  925. }
  926. }
  927. if (empty($settings['catch_up'])) {
  928. unset($settings['catch_up']);
  929. }
  930. return $settings;
  931. }
  932. /**
  933. * CRUD save. Also used for ctools integration.
  934. * @param object $object
  935. * object to be saved ->name containing unique machine name.
  936. * @return boolean
  937. * result of query.
  938. */
  939. function ultimate_cron_crud_save($object) {
  940. return db_merge('ultimate_cron')
  941. ->key(array('name' => $object->name))
  942. ->fields(array(
  943. 'settings' => serialize($object->settings),
  944. ))
  945. ->execute();
  946. }
  947. /**
  948. * Set settings for a function.
  949. *
  950. * @param $name
  951. * Function to set settings for.
  952. * @param $settings
  953. * Settings data
  954. * @return boolean
  955. * TRUE on success, FALSE on failure.
  956. */
  957. function ultimate_cron_set_settings($name, $settings) {
  958. if (module_exists('ctools')) {
  959. ctools_include('export');
  960. $function = ctools_export_crud_new('ultimate_cron');
  961. $function->name = $name;
  962. $function->settings = $settings;
  963. return ctools_export_crud_save('ultimate_cron', $function);
  964. }
  965. else {
  966. $function = new stdClass();
  967. $function->name = $name;
  968. $function->settings = $settings;
  969. return ultimate_cron_crud_save($function);
  970. }
  971. }
  972. /**
  973. * Get latest log line for a function.
  974. *
  975. * @param $name
  976. * Function to get latest log line for,
  977. * @return object
  978. * Log line.
  979. */
  980. function ultimate_cron_get_log($name) {
  981. $log = db_query_range("
  982. SELECT l.* FROM {ultimate_cron_log} l
  983. WHERE l.name = :name
  984. ORDER BY l.start_stamp DESC",
  985. 0, 1, array(':name' => $name))
  986. ->fetchAssoc();
  987. if ($log) {
  988. $log['start'] = $log['start_stamp'];
  989. $log['status'] = $log['exec_status'];
  990. $log['end'] = $log['end_stamp'];
  991. }
  992. return $log;
  993. }
  994. /**
  995. * Store watchdog error messages for later use.
  996. *
  997. * @staticvar string $log
  998. * @param $msg
  999. * Message to record.
  1000. * @param $reset
  1001. * Reset recorded message.
  1002. * @return string
  1003. * Message recorded.
  1004. */
  1005. function ultimate_cron_record_log($msg = NULL, $reset = FALSE) {
  1006. static $log = '';
  1007. if ($reset) {
  1008. $log = '';
  1009. }
  1010. if ($msg) {
  1011. $log .= "$msg\n";
  1012. }
  1013. return $log;
  1014. }
  1015. /**
  1016. * Check if rule is valid.
  1017. *
  1018. * @param $rule
  1019. * rule to validate.
  1020. * @return
  1021. * TRUE if valid, FALSE if not.
  1022. */
  1023. function ultimate_cron_validate_rule($rule) {
  1024. require_once 'CronRule.class.php';
  1025. $cron = new CronRule($rule);
  1026. if (!$cron->isValid()) {
  1027. return FALSE;
  1028. }
  1029. else {
  1030. return TRUE;
  1031. }
  1032. }
  1033. /**
  1034. * Check if rule is scheduled to run at a given time.
  1035. *
  1036. * @param $rules
  1037. * rules to validate.
  1038. * @param $last_run
  1039. * last time the rule was run.
  1040. * @param $now
  1041. * time of validation, set to NULL for now.
  1042. * @param $catch_up
  1043. * run if we missed our time window?
  1044. * @return boolean
  1045. * TRUE if rule is scheduled to run, FALSE if not.
  1046. */
  1047. function ultimate_cron_should_run($rules, $last_run, $now = NULL, $catch_up = 0, $offset = 0) {
  1048. $now = is_null($now) ? time() : $now;
  1049. require_once 'CronRule.class.php';
  1050. $cron = new CronRule();
  1051. foreach ($rules as $rule) {
  1052. $cron->rule = $rule;
  1053. $cron->offset = $offset;
  1054. $last_ran = $cron->getLastRan($now);
  1055. if ($last_ran > $last_run && $last_ran >= $now - $catch_up) {
  1056. return TRUE;
  1057. }
  1058. }
  1059. return FALSE;
  1060. }
  1061. /**
  1062. * Get a list of the "easy-hooks".
  1063. *
  1064. * @return array
  1065. * hooks (hook_name => hook_description).
  1066. */
  1067. function ultimate_cron_easy_hooks() {
  1068. return array(
  1069. 'cron' => 'Default cron handler',
  1070. 'hourly' => 'Hourly',
  1071. 'daily' => 'Daily',
  1072. 'weekly' => 'Weekly',
  1073. 'monthly' => 'Monthly',
  1074. 'yearly' => 'Yearly'
  1075. );
  1076. }
  1077. /**
  1078. * Get rule(s) for easy hook(s)
  1079. *
  1080. * @param $hook
  1081. * Hook to get rule for (optional).
  1082. * @return mixed
  1083. * Rule for $hook if specified, otherwise all rules for all easy hooks.
  1084. */
  1085. function ultimate_cron_easy_hooks_rule($hook = NULL) {
  1086. $rules = array(
  1087. 'cron' => variable_get('ultimate_cron_rule', ULTIMATE_CRON_RULE),
  1088. 'hourly' => ULTIMATE_CRON_HOURLY_RULE,
  1089. 'daily' => ULTIMATE_CRON_DAILY_RULE,
  1090. 'weekly' => ULTIMATE_CRON_WEEKLY_RULE,
  1091. 'monthly' => ULTIMATE_CRON_MONTHLY_RULE,
  1092. 'yearly' => ULTIMATE_CRON_YEARLY_RULE,
  1093. );
  1094. return isset($rules[$hook]) ? $rules[$hook] : variable_get('ultimate_cron_rule', ULTIMATE_CRON_RULE);
  1095. }
  1096. /**
  1097. * Get module name
  1098. * @param $module
  1099. * @return string
  1100. * Name of module
  1101. */
  1102. function ultimate_cron_module_name($module) {
  1103. $file = drupal_get_path('module', $module) . '/' . $module . '.info';
  1104. $info = drupal_parse_info_file($file);
  1105. return $info['name'] ? $info['name'] : $module;
  1106. }
  1107. /**
  1108. * Load all cronjob settings and processes.
  1109. *
  1110. * @return array
  1111. * Array of cronjobs and their data.
  1112. */
  1113. function _ultimate_cron_preload_cron_data() {
  1114. if (module_exists('ctools')) {
  1115. ctools_include('export');
  1116. $functions = ctools_export_crud_load_all('ultimate_cron');
  1117. }
  1118. else {
  1119. $functions = db_select('ultimate_cron', 'u')
  1120. ->fields('u', array('name', 'settings'))
  1121. ->execute()
  1122. ->fetchAllAssoc('name', PDO::FETCH_ASSOC);
  1123. foreach ($functions as &$function) {
  1124. $function = (object)$function;
  1125. if (!empty($function->settings)) {
  1126. $function->settings = unserialize($function->settings);
  1127. }
  1128. }
  1129. }
  1130. $hooks = ultimate_cron_get_hooks();
  1131. if (function_exists('background_process_update_status')) {
  1132. $query = db_select('background_process', 'b')
  1133. ->fields('b', array('handle', 'service_host'))
  1134. ->condition('handle', 'uc:%', 'LIKE');
  1135. $query->addField('b', 'start_stamp', 'start');
  1136. $query->addField('b', 'exec_status', 'status');
  1137. $processes = $query->execute()->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
  1138. }
  1139. else {
  1140. $processes = db_select('background_process', 'b')
  1141. ->fields('b', array('handle', 'service_host', 'start', 'status'))
  1142. ->condition('handle', 'uc:%', 'LIKE')
  1143. ->execute()
  1144. ->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
  1145. }
  1146. $data = array();
  1147. foreach ($hooks as $name => $hook) {
  1148. $settings = empty($functions[$name]->settings) ? array() : $functions[$name]->settings;
  1149. $settings += $hook['settings'];
  1150. $handle = 'uc:' . $name;
  1151. $data[$name] = array(
  1152. 'settings' => $settings,
  1153. 'background_process' => empty($processes[$handle]) ? NULL : (object)$processes[$handle],
  1154. );
  1155. }
  1156. return $data;
  1157. }
  1158. /**
  1159. * Return a list of modules that are incompatible with Ultimate Cron
  1160. */
  1161. function _ultimate_cron_incompatible_modules() {
  1162. static $modules = NULL;
  1163. if (isset($modules)) {
  1164. return $modules;
  1165. }
  1166. $modules = array();
  1167. $candidates = array('parallel_cron');
  1168. foreach ($candidates as $module) {
  1169. if (module_exists($module)) {
  1170. $modules[$module] = ultimate_cron_module_name($module);
  1171. }
  1172. }
  1173. return $modules;
  1174. }
  1175. /**
  1176. * Get service hosts defined in the system.
  1177. */
  1178. function ultimate_cron_get_service_groups() {
  1179. if (function_exists('background_process_get_service_groups')) {
  1180. return background_process_get_service_groups();
  1181. }
  1182. // Fallback for setups that havent upgraded Background Process.
  1183. // We have this to avoid upgrade dependencies or majer version bump.
  1184. $service_groups = variable_get('background_process_service_groups', array());
  1185. $service_groups += array(
  1186. 'default' => array(
  1187. 'hosts' => array(variable_get('background_process_default_service_host', 'default')),
  1188. ),
  1189. );
  1190. foreach ($service_groups as &$service_group) {
  1191. $service_group += array(
  1192. 'method' => 'background_process_service_group_random'
  1193. );
  1194. }
  1195. return $service_groups;
  1196. }
  1197. /**
  1198. * Smelly code; $default_rule determines if we should populate ...
  1199. */
  1200. function _ultimate_cron_default_settings($default_rule = NULL) {
  1201. return array(
  1202. 'enabled' => TRUE,
  1203. 'rules' => $default_rule ? array($default_rule) : array(),
  1204. 'catch_up' => $default_rule ? variable_get('ultimate_cron_catch_up', ULTIMATE_CRON_CATCH_UP) : '',
  1205. 'queue_lease_time' => '',
  1206. );
  1207. }