elysia_cron.module 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. <?php
  2. /**
  3. * @file
  4. * Elysia cron primary module file.
  5. */
  6. require_once 'elysia_cron_scheduler.inc';
  7. require_once 'elysia_cron.admin.inc';
  8. require_once 'elysia_cron.ctools.inc';
  9. /**
  10. * Implements hook_menu().
  11. */
  12. function elysia_cron_menu() {
  13. $items['admin/config/system/cron'] = array(
  14. 'title' => 'Cron Settings',
  15. 'description' => 'View and manage cron table',
  16. 'page callback' => 'elysia_cron_admin_page',
  17. 'access callback' => 'elysia_cron_access',
  18. 'access arguments' => array('view elysia_cron'),
  19. );
  20. $items['admin/config/system/cron/status'] = array(
  21. 'type' => MENU_DEFAULT_LOCAL_TASK,
  22. 'title' => 'Status',
  23. 'weight' => 1,
  24. );
  25. $items['admin/config/system/cron/settings'] = array(
  26. 'type' => MENU_LOCAL_TASK,
  27. 'title' => 'Settings',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('elysia_cron_settings_form'),
  30. 'access callback' => 'elysia_cron_access',
  31. 'access arguments' => array('administer elysia_cron'),
  32. 'weight' => 2,
  33. );
  34. $items['admin/config/system/cron/maintenance'] = array(
  35. 'type' => MENU_LOCAL_TASK,
  36. 'title' => 'Maintenance',
  37. 'page callback' => 'drupal_get_form',
  38. 'page arguments' => array('elysia_cron_reset_statistics_form'),
  39. 'access callback' => 'elysia_cron_access',
  40. 'access arguments' => array('administer elysia_cron'),
  41. 'weight' => 3,
  42. );
  43. $items['admin/config/system/cron/execute/%'] = array(
  44. 'type' => MENU_CALLBACK,
  45. 'page callback' => 'elysia_cron_execute_page',
  46. 'page arguments' => array(5),
  47. 'access callback' => 'elysia_cron_access',
  48. 'access arguments' => array('execute elysia_cron'),
  49. );
  50. $items['admin/build/cron/ping'] = array(
  51. 'type' => MENU_CALLBACK,
  52. 'page callback' => 'elysia_cron_ping_page',
  53. 'access callback' => TRUE,
  54. );
  55. return $items;
  56. }
  57. /**
  58. * Implements hook_menu_alter().
  59. */
  60. function elysia_cron_menu_alter(&$items) {
  61. // Override default cron page.
  62. $items['admin/config/system/cron'] = array(
  63. 'title' => 'Cron Settings',
  64. 'description' => 'View and manage cron table',
  65. 'page callback' => 'elysia_cron_admin_page',
  66. 'access callback' => 'elysia_cron_access',
  67. 'access arguments' => array('view elysia_cron'),
  68. 'file' => NULL,
  69. ) + $items['admin/config/system/cron'];
  70. }
  71. /**
  72. * Check access to operation inside elysia cron module.
  73. *
  74. * Permission have nested structure, for example: "administer elysia_cron" allow
  75. * to do anything: view, execute and configure cron jobs.
  76. *
  77. * @param string $perm
  78. * Permission name.
  79. *
  80. * @return bool
  81. * Access granted or not.
  82. */
  83. function elysia_cron_access($perm) {
  84. $access = FALSE;
  85. switch ($perm) {
  86. case 'administer elysia_cron':
  87. $access = user_access($perm);
  88. break;
  89. case 'execute elysia_cron':
  90. $access = user_access($perm) || user_access('administer elysia_cron');
  91. break;
  92. case 'view elysia_cron';
  93. $access = user_access($perm) || user_access('execute elysia_cron') || user_access('administer elysia_cron');
  94. }
  95. return $access;
  96. }
  97. /**
  98. * Implements hook_permission().
  99. */
  100. function elysia_cron_permission() {
  101. return array(
  102. 'administer elysia_cron' => array(
  103. 'title' => t('Administer elysia cron'),
  104. 'description' => t('Perform changes to cron jobs timings, disable cron or single jobs and access cron execution statistics'),
  105. ),
  106. 'execute elysia_cron' => array(
  107. 'title' => t('Execute elysia cron jobs'),
  108. 'description' => t('Allow users to view statistics, execution status and do manually execute cron jobs'),
  109. ),
  110. 'view elysia_cron' => array(
  111. 'title' => t('View elysia cron stats'),
  112. 'description' => t('Allows users to view statistics and execution status of cron jobs'),
  113. ),
  114. );
  115. }
  116. /**
  117. * Implements hook_exit().
  118. */
  119. function elysia_cron_exit() {
  120. global $_elysia_cron_exit_phase;
  121. $_elysia_cron_exit_phase = TRUE;
  122. }
  123. /**
  124. * Implements hook_cron().
  125. *
  126. * Hook cron is invoked only by standard drupal cron.
  127. * It's used to replace drupal cron.
  128. */
  129. function elysia_cron_cron() {
  130. global $_elysia_cron_exit_phase, $_elysia_cron_drush;
  131. // If invoked "core-cron" via drush i'll redirect to elysia-cron handler.
  132. if (function_exists('elysia_cron_drush_detect') && elysia_cron_drush_detect()) {
  133. elysia_cron_drush_invoke(TRUE);
  134. }
  135. // First cron run is executed in standard drupal way.
  136. // This is to enable the use of install profiles.
  137. if (variable_get('cron_last', 0) <= variable_get('install_time', 0)) {
  138. return;
  139. }
  140. // If the path is 'admin/*', or if the user is not anonymous,
  141. // this is a manual cron run (probably by admin/logs/status),
  142. // but not if we are in the exit phase (= this a "poormanscron" run).
  143. $manual_run = empty($_elysia_cron_exit_phase) && ((arg(0) == 'admin') || !empty($GLOBALS['user']->uid));
  144. $result = elysia_cron_run($manual_run);
  145. if ($manual_run) {
  146. if ($result) {
  147. elysia_cron_message('Cron ran successfully');
  148. }
  149. else {
  150. elysia_cron_message('Cron run failed, disabled or nothing to do');
  151. }
  152. // In manual execution it's better to set cron_last variable in standard way
  153. // (so the user sees the execution time updates).
  154. // This invalidates variable cache, but it's a manual execution,
  155. // it should be not a great performance problem.
  156. if (empty($_elysia_cron_drush)) {
  157. variable_set('cron_last', time());
  158. }
  159. drupal_goto('admin/reports/status');
  160. }
  161. // If we are in "poormanscron" mode it's better to force setting of cron_last.
  162. // This invalidates variable cache, but is needed for right execution check.
  163. // @see system_run_automated_cron() in system.module
  164. if (!empty($_elysia_cron_exit_phase)) {
  165. variable_set('cron_last', time());
  166. }
  167. exit();
  168. }
  169. /**
  170. * Implements hook_cron().
  171. *
  172. * I use help section for admin/build/modules page to check if elysia_cron
  173. * is the module with the smallest weight.
  174. * If it's not i'll set it and print a message.
  175. */
  176. function elysia_cron_help($section, $arg = FALSE) {
  177. if ($section == 'admin/modules') {
  178. $query = db_select('system', 's');
  179. $query->addExpression('MIN(weight)');
  180. $min = $query->condition('s.name', 'elysia_cron', '!=')
  181. ->execute()
  182. ->fetchField();
  183. $weight = db_select('system', 's')
  184. ->fields('s', array('weight'))
  185. ->condition('s.name', 'elysia_cron')
  186. ->execute()
  187. ->fetchField();
  188. if ($min <= $weight) {
  189. elysia_cron_message('Elysia cron module is not the module with the smallest weight (and it must be). Updating weight...');
  190. db_update('system')
  191. ->fields(array('weight' => $min - 1))
  192. ->condition('name', 'elysia_cron')
  193. ->execute();
  194. }
  195. }
  196. }
  197. // Variables managed by _ec_variable method,
  198. // because are setted during cron execution handling
  199. // (with standard variable_set this will invalidate variable
  200. // cache in EVERY cron clock).
  201. $GLOBALS['_ec_variables_allowed'] = array(
  202. 'elysia_cron_semaphore',
  203. 'elysia_cron_last_run',
  204. 'elysia_cron_last_channel',
  205. 'cron_semaphore',
  206. 'cron_last',
  207. );
  208. /**
  209. * Initialise global variables.
  210. */
  211. function _ec_variable_init() {
  212. global $_ec_variables, $_ec_variables_allowed;
  213. $_ec_variables = array();
  214. $_ec_variables = array_map('unserialize', db_query("SELECT name, value FROM {variable} where name like '" . implode("' or name like '", $_ec_variables_allowed) . "'")->fetchAllKeyed());
  215. }
  216. /**
  217. * A substitute for variable_get to avoid cache management.
  218. *
  219. * Use ONLY for variables set during cron execution:
  220. * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
  221. * cron_semaphore, cron_last
  222. * WARN_UPGRADE.
  223. */
  224. function _ec_variable_get($name, $default) {
  225. global $_ec_variables, $_ec_variables_allowed;
  226. if (!in_array($name, $_ec_variables_allowed)) {
  227. elysia_cron_error('Wrong variable passed to _ec_variable_get: !var', array('!var' => $name), TRUE);
  228. return variable_get($name, $default);
  229. }
  230. if (!is_array($_ec_variables)) {
  231. _ec_variable_init();
  232. }
  233. // If there is a $GLOBALS['original_conf'] = $conf; at the end of settings.php
  234. // i consider it.
  235. global $_original_conf;
  236. if (isset($_original_conf[$name])) {
  237. return $_original_conf[$name];
  238. }
  239. if (isset($_ec_variables[$name])) {
  240. return $_ec_variables[$name];
  241. }
  242. return $default;
  243. }
  244. /**
  245. * A substitute for variable_set to avoid cache management.
  246. *
  247. * Use ONLY for variables set during cron execution:
  248. * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
  249. * cron_semaphore, cron_last.
  250. * WARN_UPGRADE.
  251. */
  252. function _ec_variable_set($name, $value) {
  253. global $_ec_variables, $_ec_variables_allowed;
  254. if (!in_array($name, $_ec_variables_allowed)) {
  255. elysia_cron_error('Wrong variable passed to _ec_variable_set: !var', array('!var' => $name), TRUE);
  256. variable_set($name, $value);
  257. }
  258. if (!is_array($_ec_variables)) {
  259. _ec_variable_init();
  260. }
  261. db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
  262. $_ec_variables[$name] = $value;
  263. }
  264. /**
  265. * A substitute for variable_del to avoid cache management.
  266. *
  267. * Use ONLY for variables set during cron execution:
  268. * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
  269. * cron_semaphore, cron_last
  270. * WARN_UPGRADE.
  271. */
  272. function _ec_variable_del($name) {
  273. global $_ec_variables, $_ec_variables_allowed;
  274. if (!in_array($name, $_ec_variables_allowed)) {
  275. elysia_cron_error('Wrong variable passed to _ec_variable_del: !var', array('!var' => $name), TRUE);
  276. variable_del($name);
  277. return;
  278. }
  279. if (!is_array($_ec_variables)) {
  280. _ec_variable_init();
  281. }
  282. db_delete('variable')->condition('name', $name)->execute();
  283. unset($_ec_variables[$name]);
  284. }
  285. function _ec_get_name($name) {
  286. $maxlen = 120;
  287. if (strlen($name) < $maxlen) {
  288. return $name;
  289. }
  290. $border = ($maxlen - 32) / 2;
  291. return substr($name, 0, $border) . md5($name) . substr($name, -$border);
  292. }
  293. function elysia_cron_set($name, $channel = FALSE, $values = array()) {
  294. if ($channel) {
  295. $name = ':' . $name;
  296. }
  297. db_merge('elysia_cron')->key(array('name' => $name))->fields($values)->execute();
  298. global $_elysia_cron_db_cache;
  299. unset($_elysia_cron_db_cache[$name]);
  300. }
  301. function elysia_cron_get($name, $channel = FALSE, $key = FALSE, $default = FALSE, $refresh = FALSE) {
  302. global $_elysia_cron_db_cache;
  303. static $elysia_cron_defaults;
  304. if (!isset($elysia_cron_defaults)) {
  305. $elysia_cron_defaults = function_exists('elysia_cron_get_ctools_defaults') ? elysia_cron_get_ctools_defaults() : array();
  306. }
  307. if ($channel) {
  308. $name = ':' . $name;
  309. }
  310. if ($refresh || !isset($_elysia_cron_db_cache[$name])) {
  311. $_elysia_cron_db_cache[$name] = db_select('elysia_cron', 'ec')
  312. ->fields('ec', _elysia_cron_columns())
  313. ->condition('name', $name)
  314. ->execute()
  315. ->fetchAssoc();
  316. if (!$_elysia_cron_db_cache[$name] && isset($elysia_cron_defaults[$name])) {
  317. $_elysia_cron_db_cache[$name] = (array) $elysia_cron_defaults[$name];
  318. }
  319. elseif ($_elysia_cron_db_cache[$name] && isset($elysia_cron_defaults[$name])) {
  320. foreach ($elysia_cron_defaults[$name] as $k => $v) {
  321. if (!isset($_elysia_cron_db_cache[$name][$k]) || is_null($_elysia_cron_db_cache[$name][$k])) {
  322. $_elysia_cron_db_cache[$name][$k] = $v;
  323. }
  324. }
  325. }
  326. }
  327. return !$_elysia_cron_db_cache[$name] || !isset($_elysia_cron_db_cache[$name][$key]) || is_null($_elysia_cron_db_cache[$name][$key]) ? $default : $_elysia_cron_db_cache[$name][$key];
  328. }
  329. /**
  330. * Get status of specified channel.
  331. *
  332. * @param string $channel
  333. * Channel name.
  334. * @param bool $default
  335. * Default value for channel status.
  336. * @param bool $refresh
  337. * If TRUE - fresh status will be get from database.
  338. *
  339. * @return bool
  340. * Channel status.
  341. */
  342. function elysia_cron_is_channel_disabled($channel, $default = FALSE, $refresh = FALSE) {
  343. // May be overridden by a static conf.
  344. if (isset($GLOBALS['conf'][$n = 'ecc_' . _ec_get_name($channel) . '_d'])) {
  345. return $GLOBALS['conf'][$n];
  346. }
  347. return elysia_cron_get($channel, TRUE, 'disable', $default, $refresh);
  348. }
  349. /**
  350. * Update channel status.
  351. *
  352. * @param string $channel
  353. * Channel name.
  354. * @param bool $value
  355. * New channel status:
  356. * TRUE - enabled
  357. * FALSE - disabled.
  358. */
  359. function elysia_cron_set_channel_disabled($channel, $value) {
  360. elysia_cron_set($channel, TRUE, array('disable' => $value ? 1 : 0));
  361. }
  362. /**
  363. * Reset channel status to default (disabled).
  364. *
  365. * @param string $channel
  366. * Channel name.
  367. */
  368. function elysia_cron_reset_channel_disabled($channel) {
  369. elysia_cron_set($channel, TRUE, array('disable' => 0));
  370. }
  371. /**
  372. * Get channel's rule.
  373. *
  374. * @param string $channel
  375. * Channel name.
  376. * @param string $default
  377. * Default rule.
  378. * @param bool $refresh
  379. * If TRUE - fresh status will be get from database.
  380. *
  381. * @return string
  382. * Formatted rule for specified channel.
  383. */
  384. function elysia_cron_get_channel_rule($channel, $default = '', $refresh = FALSE) {
  385. // May be overridden by a static conf.
  386. if (isset($GLOBALS['conf'][$n = 'ecc_' . _ec_get_name($channel) . '_rul'])) {
  387. return $GLOBALS['conf'][$n];
  388. }
  389. return elysia_cron_get($channel, TRUE, 'rule', $default, $refresh);
  390. }
  391. /**
  392. * Set new rule for channel.
  393. *
  394. * @param string $channel
  395. * Channel name.
  396. * @param string $rule
  397. * Crontab formatted rule.
  398. */
  399. function elysia_cron_set_channel_rule($channel, $rule) {
  400. elysia_cron_set($channel, TRUE, array('rule' => $rule));
  401. }
  402. /**
  403. * Reset channel's rule to default.
  404. *
  405. * @param string $channel
  406. * Channel name.
  407. */
  408. function elysia_cron_reset_channel_rule($channel) {
  409. elysia_cron_set($channel, TRUE, array('rule' => NULL));
  410. }
  411. /**
  412. * Check if channel is running now.
  413. *
  414. * @param string $channel
  415. * Channel name.
  416. * @param int $default
  417. * Default value.
  418. * @param bool $refresh
  419. * If TRUE - fresh status will be get from database.
  420. *
  421. * @return int
  422. * Timestamp from beginning of running or zero.
  423. */
  424. function elysia_cron_is_channel_running($channel, $default = 0, $refresh = FALSE) {
  425. return elysia_cron_get($channel, TRUE, 'running', $default, $refresh);
  426. }
  427. /**
  428. * Updated channel running value.
  429. *
  430. * @param string $channel
  431. * Channel name.
  432. * @param int $value
  433. * New running value.
  434. */
  435. function elysia_cron_set_channel_running($channel, $value) {
  436. elysia_cron_set($channel, TRUE, array('running' => $value));
  437. }
  438. /**
  439. * Get channel's last run value.
  440. *
  441. * @param string $channel
  442. * Channel name.
  443. * @param int $default
  444. * Default value.
  445. * @param bool $refresh
  446. * If TRUE - fresh status will be get from database.
  447. *
  448. * @return int
  449. * Last run timestamp or zero.
  450. *
  451. * @todo Not used, to delete?
  452. */
  453. function elysia_cron_get_channel_last_run($channel, $default = 0, $refresh = FALSE) {
  454. return elysia_cron_get($channel, TRUE, 'last_run', $default, $refresh);
  455. }
  456. /**
  457. * Set channel's last run value.
  458. *
  459. * @param string $channel
  460. * Channel name.
  461. * @param int $value
  462. * New last run timestamp.
  463. *
  464. * @todo Not used, to delete?
  465. */
  466. function elysia_cron_set_channel_last_run($channel, $value) {
  467. elysia_cron_set($channel, TRUE, array('last_run' => $value));
  468. }
  469. /**
  470. * Get channel's last aborted value.
  471. *
  472. * @param string $channel
  473. * Channel name.
  474. * @param int $default
  475. * Default value.
  476. * @param bool $refresh
  477. * If TRUE - fresh status will be get from database.
  478. *
  479. * @return int
  480. * Last aborted timestamp or zero.
  481. *
  482. * @todo Not used, to delete?
  483. */
  484. function elysia_cron_get_channel_last_aborted($channel, $default = 0, $refresh = FALSE) {
  485. return elysia_cron_get($channel, TRUE, 'last_aborted', $default, $refresh);
  486. }
  487. /**
  488. * Set channel's last aborted value.
  489. *
  490. * @param string $channel
  491. * Channel name.
  492. * @param int $value
  493. * New last aborted timestamp.
  494. *
  495. * @todo Not used, to delete?
  496. */
  497. function elysia_cron_set_channel_last_aborted($channel, $value) {
  498. elysia_cron_set($channel, TRUE, array('last_aborted' => $value ? 1 : 0));
  499. }
  500. /**
  501. * Get channel's about count.
  502. *
  503. * @param string $channel
  504. * Channel name.
  505. * @param int $default
  506. * Default value.
  507. * @param bool $refresh
  508. * If TRUE - fresh status will be get from database.
  509. *
  510. * @return int
  511. * Abort count for specified channel.
  512. */
  513. function elysia_cron_get_channel_abort_count($channel, $default = 0, $refresh = FALSE) {
  514. return elysia_cron_get($channel, TRUE, 'abort_count', $default, $refresh);
  515. }
  516. /**
  517. * Set channel's abort count.
  518. *
  519. * @param string $channel
  520. * Channel name.
  521. * @param int $value
  522. * New abort count.
  523. *
  524. * @todo Not used, to delete?
  525. */
  526. function elysia_cron_set_channel_abort_count($channel, $value) {
  527. elysia_cron_set($channel, TRUE, array('abort_count' => $value));
  528. }
  529. /**
  530. * Get channel's last about function.
  531. *
  532. * @param string $channel
  533. * Channel name.
  534. * @param string $default
  535. * Default value.
  536. * @param bool $refresh
  537. * If TRUE - fresh status will be get from database.
  538. *
  539. * @return string
  540. * Last abort function or empty string.
  541. *
  542. * @todo Not used, to delete?
  543. */
  544. function elysia_cron_get_channel_last_abort_function($channel, $default = '', $refresh = FALSE) {
  545. return elysia_cron_get($channel, TRUE, 'last_abort_function', $default, $refresh);
  546. }
  547. /**
  548. * Set channel's last abort function.
  549. *
  550. * @param string $channel
  551. * Channel name.
  552. * @param string $value
  553. * Last abort function name.
  554. *
  555. * @todo Not used, to delete?
  556. */
  557. function elysia_cron_set_channel_last_abort_function($channel, $value) {
  558. elysia_cron_set($channel, TRUE, array('last_abort_function' => $value));
  559. }
  560. /**
  561. * Grab statistics by channel.
  562. *
  563. * @param string $channel
  564. * Channel name.
  565. * @param bool $refresh
  566. * If TRUE - fresh status will be get from database.
  567. *
  568. * @return array
  569. * Statistics for specified channel.
  570. */
  571. function elysia_cron_get_channel_stats($channel, $refresh = FALSE) {
  572. return array(
  573. 'last_run' => elysia_cron_get($channel, TRUE, 'last_run', 0, $refresh),
  574. 'last_execution_time' => elysia_cron_get($channel, TRUE, 'last_execution_time', 0, $refresh),
  575. 'execution_count' => elysia_cron_get($channel, TRUE, 'execution_count', 0, $refresh),
  576. 'avg_execution_time' => elysia_cron_get($channel, TRUE, 'avg_execution_time', 0, $refresh),
  577. 'max_execution_time' => elysia_cron_get($channel, TRUE, 'max_execution_time', 0, $refresh),
  578. 'last_shutdown_time' => elysia_cron_get($channel, TRUE, 'last_shutdown_time', 0, $refresh),
  579. 'last_aborted' => elysia_cron_get($channel, TRUE, 'last_aborted', 0, $refresh),
  580. 'abort_count' => elysia_cron_get($channel, TRUE, 'abort_count', 0, $refresh),
  581. 'last_abort_function' => elysia_cron_get($channel, TRUE, 'last_abort_function', 0, ''),
  582. );
  583. }
  584. /**
  585. * Update channel's stats.
  586. *
  587. * @param string $channel
  588. * Channel name.
  589. * @param array $data
  590. * Associative array of values.
  591. */
  592. function elysia_cron_set_channel_stats($channel, array $data = array()) {
  593. elysia_cron_set($channel, TRUE, $data);
  594. }
  595. /**
  596. * Get formatted crontab rule for specified job.
  597. *
  598. * @param string $job
  599. * Job name.
  600. * @param string $default
  601. * Default rule.
  602. * @param bool $refresh
  603. * If TRUE - fresh status will be get from database.
  604. *
  605. * @return string
  606. * Formatted job rule.
  607. */
  608. function elysia_cron_get_job_rule($job, $default = '', $refresh = FALSE) {
  609. // May be overridden by a static conf.
  610. if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_rul'])) {
  611. return $GLOBALS['conf'][$n];
  612. }
  613. return elysia_cron_get($job, FALSE, 'rule', $default, $refresh);
  614. }
  615. /**
  616. * Set formatted crontab rule for specified job.
  617. *
  618. * @param string $job
  619. * Job name.
  620. * @param string $value
  621. * New crontab rule for job.
  622. */
  623. function elysia_cron_set_job_rule($job, $value) {
  624. elysia_cron_set($job, FALSE, array('rule' => $value));
  625. }
  626. /**
  627. * Reset crontab job rule to default.
  628. *
  629. * @param string $job
  630. * Job name.
  631. */
  632. function elysia_cron_reset_job_rule($job) {
  633. elysia_cron_set($job, FALSE, array('rule' => NULL));
  634. }
  635. /**
  636. * Get weight of specified job.
  637. *
  638. * @param string $job
  639. * Job name.
  640. * @param string $default
  641. * Default weight.
  642. * @param bool $refresh
  643. * If TRUE - fresh status will be get from database.
  644. *
  645. * @return string
  646. * Job weight or empty string.
  647. */
  648. function elysia_cron_get_job_weight($job, $default = '', $refresh = FALSE) {
  649. // May be overridden by a static conf.
  650. if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_w'])) {
  651. return $GLOBALS['conf'][$n];
  652. }
  653. return elysia_cron_get($job, FALSE, 'weight', $default, $refresh);
  654. }
  655. /**
  656. * Set weight specified job.
  657. *
  658. * @param string $job
  659. * Job name.
  660. * @param string $value
  661. * New weight for job.
  662. */
  663. function elysia_cron_set_job_weight($job, $value) {
  664. elysia_cron_set($job, FALSE, array('weight' => $value));
  665. }
  666. /**
  667. * Reset job weight to default.
  668. *
  669. * @param string $job
  670. * Job name.
  671. */
  672. function elysia_cron_reset_job_weight($job) {
  673. elysia_cron_set($job, FALSE, array('weight' => NULL));
  674. }
  675. function elysia_cron_is_job_disabled($job, $default = FALSE, $refresh = FALSE) {
  676. // May be overridden by a static conf.
  677. if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_d'])) {
  678. return $GLOBALS['conf'][$n];
  679. }
  680. return elysia_cron_get($job, FALSE, 'disable', $default, $refresh);
  681. }
  682. function elysia_cron_set_job_disabled($job, $value) {
  683. elysia_cron_set($job, FALSE, array('disable' => $value ? 1 : 0));
  684. }
  685. function elysia_cron_reset_job_disabled($job) {
  686. elysia_cron_set($job, FALSE, array('disable' => NULL));
  687. }
  688. function elysia_cron_get_job_channel($job, $default = '', $refresh = FALSE) {
  689. // May be overridden by a static conf.
  690. if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_c'])) {
  691. $c = $GLOBALS['conf'][$n];
  692. }
  693. else {
  694. // In this place, 'context' means 'channel'.
  695. $c = elysia_cron_get($job, FALSE, 'context', $default, $refresh);
  696. }
  697. return !$c ? $default : $c;
  698. }
  699. function elysia_cron_set_job_channel($job, $value) {
  700. // In this place, 'context' means 'channel'.
  701. elysia_cron_set($job, FALSE, array('context' => $value));
  702. }
  703. function elysia_cron_reset_job_channel($job) {
  704. // In this place, 'context' means 'channel'.
  705. elysia_cron_set($job, FALSE, array('context' => NULL));
  706. }
  707. function elysia_cron_is_job_running($job, $default = 0, $refresh = FALSE) {
  708. return elysia_cron_get($job, FALSE, 'running', $default, $refresh);
  709. }
  710. function elysia_cron_set_job_running($job, $value) {
  711. elysia_cron_set($job, FALSE, array('running' => $value));
  712. }
  713. function elysia_cron_get_job_last_run($job, $default = 0, $refresh = FALSE) {
  714. return elysia_cron_get($job, FALSE, 'last_run', $default, $refresh);
  715. }
  716. function elysia_cron_set_job_last_run($job, $value) {
  717. elysia_cron_set($job, FALSE, array('last_run' => $value));
  718. }
  719. function elysia_cron_get_job_stats($job, $refresh = FALSE) {
  720. return array(
  721. 'last_run' => elysia_cron_get($job, FALSE, 'last_run', 0, $refresh),
  722. 'last_execution_time' => elysia_cron_get($job, FALSE, 'last_execution_time', 0, $refresh),
  723. 'execution_count' => elysia_cron_get($job, FALSE, 'execution_count', 0, $refresh),
  724. 'avg_execution_time' => elysia_cron_get($job, FALSE, 'avg_execution_time', 0, $refresh),
  725. 'max_execution_time' => elysia_cron_get($job, FALSE, 'max_execution_time', 0, $refresh),
  726. );
  727. }
  728. function elysia_cron_set_job_stats(
  729. $job,
  730. $last_run = -1,
  731. $last_execution_time = -1,
  732. $execution_count = -1,
  733. $avg_execution_time = -1,
  734. $max_execution_time = -1,
  735. $data = array()
  736. ) {
  737. if ($last_run != -1) {
  738. $data['last_run'] = $last_run;
  739. }
  740. if ($last_execution_time != -1) {
  741. $data['last_execution_time'] = $last_execution_time;
  742. }
  743. if ($execution_count != -1) {
  744. $data['execution_count'] = $execution_count;
  745. }
  746. if ($avg_execution_time != -1) {
  747. $data['avg_execution_time'] = $avg_execution_time;
  748. }
  749. if ($max_execution_time != -1) {
  750. $data['max_execution_time'] = $max_execution_time;
  751. }
  752. elysia_cron_set($job, FALSE, $data);
  753. }
  754. function elysia_cron_last_channel() {
  755. return _ec_variable_get('elysia_cron_last_channel', '');
  756. }
  757. function elysia_cron_set_last_channel($channel) {
  758. _ec_variable_set('elysia_cron_last_channel', $channel);
  759. }
  760. function elysia_cron_reset_stats() {
  761. global $_elysia_cron_settings, $_elysia_cron_settings_by_channel;
  762. elysia_cron_initialize();
  763. foreach ($_elysia_cron_settings as $job => $conf) {
  764. elysia_cron_set_job_stats($job, 0, 0, 0, 0, 0);
  765. }
  766. $channel_reset_data = array(
  767. 'last_run' => 0,
  768. 'last_execution_time' => 0,
  769. 'execution_count' => 0,
  770. 'avg_execution_time' => 0,
  771. 'max_execution_time' => 0,
  772. 'last_shutdown_time' => 0,
  773. 'last_aborted' => 0,
  774. 'abort_count' => 0,
  775. 'last_abort_function' => 0,
  776. );
  777. foreach ($_elysia_cron_settings_by_channel as $channel => $conf) {
  778. elysia_cron_set_channel_stats($channel, $channel_reset_data);
  779. }
  780. }
  781. /*******************************************************************************
  782. * INTERNAL
  783. *
  784. * WARN: Below this point the word "context" should be avoided (use channel)
  785. * Disabled should always be referenced as "disabled" (in db is "disable" for
  786. * compatibility with Ctools )
  787. *******************************************************************************/
  788. /**
  789. * Sends a standard message to user.
  790. *
  791. * Drush see it.
  792. */
  793. function elysia_cron_message($message, $vars = array()) {
  794. global $_elysia_cron_drush;
  795. if (empty($_elysia_cron_drush)) {
  796. drupal_set_message(t($message, $vars));
  797. }
  798. else {
  799. drush_log(strip_tags(dt($message, $vars)), 'ok');
  800. }
  801. }
  802. /**
  803. * Send an error message to user (and log also an error).
  804. *
  805. * Drush see it (even if $skip_user_message)
  806. */
  807. function elysia_cron_error($message, $vars = array(), $skip_user_message = FALSE) {
  808. global $_elysia_cron_drush;
  809. if (empty($_elysia_cron_drush) && !$skip_user_message) {
  810. drupal_set_message(t($message, $vars), 'error');
  811. }
  812. /* @todo remove following code if not necessary.
  813. if (!empty($_elysia_cron_drush)) {
  814. drush_log(strip_tags(dt($message, $vars)), "error");
  815. }*/
  816. watchdog('cron', $message, $vars, WATCHDOG_ERROR);
  817. }
  818. /**
  819. * Log a debug in watchdog (do not print an user message).
  820. *
  821. * Drush see it in verbose mode.
  822. */
  823. function elysia_cron_debug($message, $vars = array(), $type = WATCHDOG_NOTICE) {
  824. global $_elysia_cron_drush;
  825. if ($type < WATCHDOG_NOTICE || variable_get('elysia_cron_debug_messages', 0)) {
  826. watchdog('cron', $message, $vars, $type);
  827. }
  828. if (!empty($_elysia_cron_drush) && $_elysia_cron_drush >= 2 && ($type >= WATCHDOG_NOTICE || !drush_get_option("verbose", FALSE))) {
  829. if ($type >= WATCHDOG_NOTICE) {
  830. drush_print(strip_tags(dt($message, $vars)));
  831. }
  832. else {
  833. drush_log(strip_tags(dt($message, $vars)), $type == WATCHDOG_ERROR ? "error" : (($type == WATCHDOG_ERROR ? "warning" : "notice")));
  834. }
  835. }
  836. }
  837. /**
  838. * Log a warning (do not print an user message).
  839. *
  840. * Drush see it in verbose mode.
  841. */
  842. function elysia_cron_warning($message, $vars = array()) {
  843. elysia_cron_debug($message, $vars, WATCHDOG_WARNING);
  844. }
  845. function elysia_cron_decode_script($text, $apply = TRUE) {
  846. global $_elysia_cron_settings;
  847. $lines = explode(PHP_EOL, $text);
  848. $lastcomment = '';
  849. $errors = array();
  850. foreach ($lines as $line) {
  851. $line = trim($line);
  852. if (!empty($line)) {
  853. if ($line{0} == '#') {
  854. $lastcomment = trim(substr($line, 1));
  855. }
  856. elseif (preg_match('/^(-[ ]*|)([0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+)[ ]+((?:ctx|ch):([a-zA-Z0-9_-]+)[ ]+|)([^(:]+)(\(.*\);|)$/', $line, $r)) {
  857. $c = array(
  858. 'disabled' => !empty($r[1]),
  859. 'rule' => $r[2],
  860. 'description' => $lastcomment,
  861. 'channel' => $r[4] ? $r[4] : 'default',
  862. );
  863. $lastcomment = '';
  864. if (empty($r[6])) {
  865. if (!isset($_elysia_cron_settings[$r[5]])) {
  866. // Referring a module function that not exists.
  867. $errors[] = $line;
  868. continue;
  869. }
  870. $name = $r[5];
  871. }
  872. else {
  873. // Custom expression, generate a unique name.
  874. $postfix = '';
  875. while (isset($_elysia_cron_settings[$r[5] . $postfix])) {
  876. $postfix = ($postfix ? $postfix : 0) + 1;
  877. }
  878. $name = $r[5] . $postfix;
  879. $c['expression'] = $r[5] . $r[6];
  880. }
  881. if ($apply) {
  882. $_elysia_cron_settings[$name] = isset($_elysia_cron_settings[$name]) ? array_merge($_elysia_cron_settings[$name], $c) : $c;
  883. }
  884. }
  885. else {
  886. $errors[] = $line;
  887. }
  888. }
  889. else {
  890. $lastcomment = '';
  891. }
  892. }
  893. return count($errors) ? $errors : FALSE;
  894. }
  895. /**
  896. * Fetch information about cron jobs from modules.
  897. *
  898. * @return array
  899. * Array with jobs information.
  900. */
  901. function elysia_cron_module_jobs() {
  902. static $jobs;
  903. if (!isset($jobs)) {
  904. $jobs = array();
  905. foreach (module_implements('cron') as $module) {
  906. if ($module != 'elysia_cron') {
  907. $jobs[$module . '_cron'] = array(
  908. 'module' => $module,
  909. 'callback' => $module . '_cron',
  910. 'arguments' => array(),
  911. );
  912. }
  913. }
  914. foreach (module_implements('cronapi') as $module) {
  915. $function = $module . '_cronapi';
  916. if (function_exists($function)) {
  917. $job_list = $function('list');
  918. if (is_array($job_list)) {
  919. foreach ($job_list as $job => $data) {
  920. if (is_array($data)) {
  921. $jobs[$job] = $data;
  922. }
  923. else {
  924. // Compatibility with v1.x settings.
  925. $jobs[$job] = array(
  926. 'description' => $data,
  927. 'rule' => ($rule = $function('rule', $job)) ? $rule : FALSE,
  928. 'weight' => ($weight = $function('weight', $job)) ? $weight : 0,
  929. );
  930. }
  931. $jobs[$job] = $jobs[$job] + array(
  932. 'module' => $module,
  933. 'callback' => is_callable($job) ? $job : $function,
  934. 'arguments' => is_callable($job) ? array() : array('execute', $job),
  935. );
  936. }
  937. }
  938. }
  939. }
  940. if (function_exists('drupal_alter')) {
  941. drupal_alter('cron', $jobs);
  942. }
  943. }
  944. return $jobs;
  945. }
  946. function elysia_cron_initialize($skipscript = FALSE) {
  947. global $_elysia_cron_settings, $_elysia_cron_settings_by_channel;
  948. if (empty($_elysia_cron_settings)) {
  949. $_elysia_cron_settings = array();
  950. $_elysia_cron_settings_by_channel = array();
  951. foreach (elysia_cron_module_jobs() as $job => $jobpars) {
  952. $channel = elysia_cron_get_job_channel($job, 'default');
  953. $defrule = !empty($jobpars['rule']) ? $jobpars['rule'] : elysia_cron_get_channel_rule($channel);
  954. if (!$defrule) {
  955. $defrule = variable_get('elysia_cron_default_rule', '0 * * * *');
  956. }
  957. $defweight = !empty($jobpars['weight']) ? $jobpars['weight'] : 0;
  958. if (!is_numeric($defweight)) {
  959. $defweight = 0;
  960. }
  961. $_elysia_cron_settings[$job] = array(
  962. 'key' => $job,
  963. 'channel' => $channel,
  964. 'rule' => elysia_cron_get_job_rule($job, $defrule),
  965. 'default_rule' => $defrule,
  966. 'weight' => elysia_cron_get_job_weight($job, $defweight),
  967. 'default_weight' => $defweight,
  968. 'disabled' => elysia_cron_is_job_disabled($job),
  969. 'running' => elysia_cron_is_job_running($job),
  970. ) + $jobpars;
  971. }
  972. if (!$skipscript) {
  973. $script = variable_get('elysia_cron_script', FALSE);
  974. if ($script) {
  975. elysia_cron_decode_script($script);
  976. }
  977. }
  978. uasort($_elysia_cron_settings, '_elysia_cron_sort');
  979. foreach ($_elysia_cron_settings as $job => &$conf) {
  980. $stats = elysia_cron_get_job_stats($job);
  981. foreach ($stats as $sk => $sv) {
  982. $conf[$sk] = $sv;
  983. }
  984. $_elysia_cron_settings_by_channel[$conf['channel']][$job] = &$_elysia_cron_settings[$job];
  985. }
  986. foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
  987. uasort($_elysia_cron_settings_by_channel[$channel], '_elysia_cron_sort');
  988. $_elysia_cron_settings_by_channel[$channel]['#data'] = elysia_cron_get_channel_stats($channel);
  989. $_elysia_cron_settings_by_channel[$channel]['#data']['disabled'] = elysia_cron_is_channel_disabled($channel);
  990. }
  991. }
  992. }
  993. function _elysia_cron_sort($a, $b) {
  994. if ((isset($a['weight']) ? $a['weight'] : 0) == (isset($b['weight']) ? $b['weight'] : 0)) {
  995. return strcmp((isset($a['key']) ? $a['key'] : ''), (isset($b['key']) ? $b['key'] : ''));
  996. }
  997. return (isset($a['weight']) ? $a['weight'] : 0) - (isset($b['weight']) ? $b['weight'] : 0);
  998. }
  999. /**
  1000. * Prepare system for a cron execution.
  1001. *
  1002. * This should be called at the start of a cron execution:
  1003. * prepare_run
  1004. * lock_env
  1005. * look for channel/jobs ready for execution and change internal states ...
  1006. * unlock_env
  1007. * before_execution
  1008. * .. execute channel/jobs ..
  1009. * after_execution
  1010. * unprepare_run.
  1011. */
  1012. function elysia_cron_prepare_run($manual_run, $start = TRUE) {
  1013. static $original_user;
  1014. if ($start) {
  1015. // Allow execution to continue even if the request gets canceled.
  1016. @ignore_user_abort(TRUE);
  1017. // Try to allocate enough time to run all the hook_cron implementations.
  1018. if (!ini_get('safe_mode') && _elysia_cron_function_available('set_time_limit')) {
  1019. set_time_limit(variable_get('elysia_cron_time_limit', 240));
  1020. }
  1021. // Prevent session information from being saved while cron is running.
  1022. drupal_save_session(FALSE);
  1023. // Force the current user to anonymous to ensure consistent permissions on
  1024. // cron runs (only if run by interface).
  1025. if ($manual_run) {
  1026. $original_user = $GLOBALS['user'];
  1027. $GLOBALS['user'] = drupal_anonymous_user();
  1028. }
  1029. }
  1030. else {
  1031. if ($manual_run) {
  1032. // Restore the user.
  1033. $GLOBALS['user'] = $original_user;
  1034. /*if (EC_DRUPAL_VERSION >= 7) {
  1035. drupal_save_session(TRUE);
  1036. }*/
  1037. }
  1038. }
  1039. }
  1040. /**
  1041. * Call this after a cron execution, prepared with elysia_cron_prepare_run()
  1042. *
  1043. * @see: elysia_cron_prepare_run()
  1044. */
  1045. function elysia_cron_unprepare_run($manual_run) {
  1046. elysia_cron_prepare_run($manual_run, FALSE);
  1047. }
  1048. /**
  1049. * Use this before checking and modifying environment variables.
  1050. *
  1051. * @see: elysia_cron_prepare_run()
  1052. */
  1053. function elysia_cron_lock_env($channel = NULL) {
  1054. $execute = TRUE;
  1055. $semaphore_name = elysia_cron_build_semaphore_name($channel);
  1056. if (!lock_acquire($semaphore_name, 240)) {
  1057. elysia_cron_warning('Attempting to re-run cron while it is already running.');
  1058. $execute = FALSE;
  1059. }
  1060. return $execute;
  1061. }
  1062. /**
  1063. * Unlock system locked with elysia_cron_lock_env.
  1064. *
  1065. * @see: elysia_cron_prepare_run().
  1066. */
  1067. function elysia_cron_unlock_env($channel = NULL) {
  1068. $semaphore_name = elysia_cron_build_semaphore_name($channel);
  1069. lock_release($semaphore_name);
  1070. }
  1071. /**
  1072. * Return semaphore name by channel.
  1073. *
  1074. * @param string|null $channel
  1075. * Channel name or NULL if channel not set.
  1076. *
  1077. * @return string
  1078. * Semaphore name trimmed by 255 chars length.
  1079. */
  1080. function elysia_cron_build_semaphore_name($channel) {
  1081. $semaphore_name = 'elysia_cron_semaphore';
  1082. if (isset($channel)) {
  1083. $semaphore_name .= ':' . $channel;
  1084. }
  1085. return drupal_substr($semaphore_name, 0, 255);
  1086. }
  1087. /**
  1088. * Public function to invoke a complete cron_run.
  1089. *
  1090. * @param bool $manual_run
  1091. * Consider launched by a user command
  1092. * (don't check for key/ip, protect current user...).
  1093. * @param bool $ignore_disable
  1094. * Run the channel (and all it's jobs) even if disabled.
  1095. * @param bool $ignore_time
  1096. * Run channel (and all it's jobs) job even if not ready.
  1097. * @param bool $ignore_running
  1098. * Run the channel (and all it's jobs) even if already running.
  1099. */
  1100. function elysia_cron_run($manual_run = FALSE, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
  1101. global $_elysia_cron_exit_phase;
  1102. // If DISABLED block the execution.
  1103. if (!$ignore_disable && variable_get('elysia_cron_disabled', FALSE)) {
  1104. elysia_cron_debug('Cron globally disabled, skipping run');
  1105. return;
  1106. }
  1107. if (!empty($_elysia_cron_exit_phase)) {
  1108. // We on "poormanscron" mode,
  1109. // not necessary to do checks for cron_key or host or permission.
  1110. }
  1111. elseif (!$manual_run) {
  1112. // Check for CRON_KEY or ALLOWED_HOSTS.
  1113. if (!drupal_is_cli()) {
  1114. $cron_key = variable_get('cron_key', '');
  1115. if ($cron_key && (empty($_GET['cron_key']) || $_GET['cron_key'] != $cron_key)) {
  1116. elysia_cron_debug('Cron key mismatch, skipping run');
  1117. return;
  1118. }
  1119. $allowed_hosts = variable_get('elysia_cron_allowed_hosts', FALSE);
  1120. if ($allowed_hosts && !in_array(ip_address(), explode(',', $allowed_hosts))) {
  1121. elysia_cron_debug('Cron ip address mismatch, skipping run');
  1122. return;
  1123. }
  1124. }
  1125. }
  1126. elseif (!elysia_cron_access('execute elysia_cron')) {
  1127. elysia_cron_debug('Access denied for cron launching');
  1128. return;
  1129. }
  1130. elysia_cron_prepare_run($manual_run);
  1131. _ec_variable_set('elysia_cron_last_run', time());
  1132. _ec_variable_set('cron_last', time());
  1133. if ($execute = elysia_cron_lock_env()) {
  1134. elysia_cron_initialize();
  1135. $available_channel = elysia_cron_run_available_channel($ignore_disable, $ignore_time, $ignore_running);
  1136. if ($available_channel) {
  1137. // There are jobs ready to be executed
  1138. // elysia_cron_internal_execute_channel calls elysia_cron_unlock_env.
  1139. elysia_cron_internal_execute_channel($available_channel['name'], $available_channel['jobs'], $ignore_running);
  1140. }
  1141. else {
  1142. // No jobs should be executed, i must unlock cron semaphore.
  1143. elysia_cron_unlock_env();
  1144. elysia_cron_debug('No channels ready to be executed, skipping cron.');
  1145. }
  1146. }
  1147. elysia_cron_unprepare_run($manual_run);
  1148. return $execute;
  1149. }
  1150. /**
  1151. * Public function to execute all jobs in a channel.
  1152. *
  1153. * @param bool $ignore_disable
  1154. * Run the channel (and all it's jobs) even if disabled.
  1155. * @param bool $ignore_time
  1156. * Run channel (and all it's jobs) job even if not ready.
  1157. * @param bool $ignore_running
  1158. * Run the channel (and all it's jobs) even if already running.
  1159. */
  1160. function elysia_cron_run_channel($channel, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
  1161. global $_elysia_cron_settings_by_channel;
  1162. // Always $manual_run.
  1163. elysia_cron_prepare_run(TRUE);
  1164. if ($execute = elysia_cron_lock_env($channel)) {
  1165. elysia_cron_initialize();
  1166. $jobs = FALSE;
  1167. if (isset($_elysia_cron_settings_by_channel[$channel])) {
  1168. if ($ignore_disable || empty($_elysia_cron_settings_by_channel[$channel]['#data']['disabled'])) {
  1169. $jobs = elysia_cron_check_run_channel($channel, $ignore_disable, $ignore_time, $ignore_running);
  1170. if ($jobs && count($jobs)) {
  1171. // elysia_cron_internal_execute_channel calls elysia_cron_unlock_env.
  1172. elysia_cron_internal_execute_channel($channel, $jobs, $ignore_running);
  1173. }
  1174. else {
  1175. elysia_cron_debug('Channel already running or no jobs ready to be executed, skipping');
  1176. }
  1177. }
  1178. else {
  1179. elysia_cron_warning('Channel is disabled, skipping');
  1180. }
  1181. }
  1182. else {
  1183. elysia_cron_warning('Channel not found, skipping');
  1184. }
  1185. if (!$jobs) {
  1186. // No jobs should be executed, i must unlock cron semaphore.
  1187. elysia_cron_unlock_env();
  1188. }
  1189. }
  1190. elysia_cron_unprepare_run(TRUE);
  1191. return $execute;
  1192. }
  1193. /**
  1194. * Public function to execute a single job.
  1195. *
  1196. * @param bool $ignore_disable
  1197. * Run the job even if disabled.
  1198. * @param bool $ignore_time
  1199. * Run the job even if not ready.
  1200. * @param bool $ignore_running
  1201. * Run the job even if already running.
  1202. */
  1203. function elysia_cron_run_job($job, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
  1204. global $_cron_completed, $_cron_executing_job, $_elysia_cron_settings;
  1205. elysia_cron_initialize();
  1206. if (isset($_elysia_cron_settings[$job])) {
  1207. if (elysia_cron_should_run($_elysia_cron_settings[$job], -1, $ignore_disable, $ignore_time)) {
  1208. if ($ignore_running || !elysia_cron_check_run_job($job)) {
  1209. $_cron_completed = FALSE;
  1210. $_cron_executing_job = $job;
  1211. // Register shutdown callback.
  1212. register_shutdown_function('elysia_cron_run_job_cleanup');
  1213. elysia_cron_internal_execute_job($job);
  1214. $_cron_completed = TRUE;
  1215. elysia_cron_message('Job executed');
  1216. }
  1217. else {
  1218. elysia_cron_debug('Job is already running, skipping');
  1219. }
  1220. }
  1221. else {
  1222. elysia_cron_debug('Job is disabled or not ready to be executed, skipping');
  1223. }
  1224. }
  1225. else {
  1226. elysia_cron_warning('Job not found, skipping');
  1227. }
  1228. }
  1229. function elysia_cron_run_job_cleanup() {
  1230. global $_cron_completed, $_cron_executing_job;
  1231. if ($_cron_completed) {
  1232. return;
  1233. }
  1234. // See if the semaphore is still locked.
  1235. if (elysia_cron_is_job_running($_cron_executing_job)) {
  1236. elysia_cron_warning('Unexpected termination of cron job %job manually started, aborted.', array('%job' => $_cron_executing_job));
  1237. elysia_cron_set_job_running($_cron_executing_job, 0);
  1238. }
  1239. }
  1240. /**
  1241. * Internal function to execute all jobs in a channel.
  1242. *
  1243. * Function elysia_cron_lock_env() must be called BEFORE calling this method.
  1244. */
  1245. function elysia_cron_internal_execute_channel($channel, $jobs, $ignore_running = FALSE) {
  1246. global $_elysia_cron_current_channel, $_cron_completed, $_cron_completed_time;
  1247. elysia_cron_debug('Cron channel %channel run started.', array('%channel' => $channel));
  1248. $_elysia_cron_current_channel = $channel;
  1249. elysia_cron_set($_elysia_cron_current_channel, TRUE, array(
  1250. 'running' => time(),
  1251. 'last_run' => time(),
  1252. ));
  1253. // Register shutdown callback.
  1254. register_shutdown_function('elysia_cron_internal_execute_channel_cleanup');
  1255. // Now I can unlock cron semaphore.
  1256. elysia_cron_unlock_env();
  1257. foreach ($jobs as $job) {
  1258. if ($ignore_running || !elysia_cron_check_run_job($job)) {
  1259. elysia_cron_internal_execute_job($job);
  1260. }
  1261. }
  1262. $_cron_completed = TRUE;
  1263. $_cron_completed_time = time();
  1264. // Cron is really completed after shutdown functions.
  1265. register_shutdown_function('elysia_cron_internal_execute_channel_completed');
  1266. }
  1267. /**
  1268. * Internal function to execute a single job.
  1269. */
  1270. function elysia_cron_internal_execute_job($job) {
  1271. global $_elysia_cron_settings;
  1272. elysia_cron_debug('Cron job %job started.', array('%job' => $job));
  1273. $time = time();
  1274. elysia_cron_set($job, FALSE, array(
  1275. 'running' => $time,
  1276. 'last_run' => $time,
  1277. ));
  1278. try {
  1279. if (!empty($_elysia_cron_settings[$job]['file'])) {
  1280. include_once((!empty($_elysia_cron_settings[$job]['file path']) ? $_elysia_cron_settings[$job]['file path'] : drupal_get_path('module', $_elysia_cron_settings[$job]['module'])) . DIRECTORY_SEPARATOR . $_elysia_cron_settings[$job]['file']);
  1281. }
  1282. if (!empty($_elysia_cron_settings[$job]['expression'])) {
  1283. eval($_elysia_cron_settings[$job]['expression']);
  1284. }
  1285. elseif (!empty($_elysia_cron_settings[$job]['callback']) && is_callable($_elysia_cron_settings[$job]['callback'])) {
  1286. call_user_func_array($_elysia_cron_settings[$job]['callback'], $_elysia_cron_settings[$job]['arguments']);
  1287. }
  1288. else {
  1289. elysia_cron_error('Execution of ' . $job . ' failed, can\'t find function!', array(), TRUE);
  1290. }
  1291. }
  1292. catch (Exception $e) {
  1293. watchdog_exception('cron', $e);
  1294. }
  1295. $stats = elysia_cron_get_job_stats($job);
  1296. $time = time() - $time;
  1297. elysia_cron_set_job_stats(
  1298. $job,
  1299. -1,
  1300. $time,
  1301. ($c = $stats['execution_count'] + 1),
  1302. round((($stats['avg_execution_time'] * ($c - 1)) + $time) / $c, 2),
  1303. $time > $stats['max_execution_time'] ? $time : -1,
  1304. array('running' => 0)
  1305. );
  1306. elysia_cron_debug('Cron job %job ended in %time secs.', array('%job' => $job, '%time' => $time));
  1307. }
  1308. /**
  1309. * Check if the channel is idle (not running, or stuck). If so set returns available jobs.
  1310. *
  1311. * @return array of jobs ready to be executed, or FALSE if channel is running
  1312. */
  1313. function elysia_cron_check_run_channel($channel, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
  1314. global $_elysia_cron_settings_by_channel;
  1315. $jobs = FALSE;
  1316. $stuck_time = variable_get('elysia_cron_stuck_time', 3600);
  1317. $sem = elysia_cron_is_channel_running($channel);
  1318. if ($sem && (time() - $sem > $stuck_time)) {
  1319. elysia_cron_set_channel_running($channel, 0);
  1320. $last_job = elysia_cron_execute_aborted($channel);
  1321. unset($sem);
  1322. elysia_cron_error('Cron channel (%channel) has been running for more than an %stuck_time secs and is most likely stuck. Last job executed: %job', array('%channel' => $channel, '%stuck_time' => $stuck_time, '%job' => $last_job), TRUE);
  1323. }
  1324. if (($ignore_running || empty($sem)) && ($ignore_disable || !$_elysia_cron_settings_by_channel[$channel]['#data']['disabled'])) {
  1325. $jobs = elysia_cron_active_jobs($channel, $ignore_disable, $ignore_time);
  1326. }
  1327. return $jobs;
  1328. }
  1329. function elysia_cron_check_run_job($job) {
  1330. $job_running = FALSE;
  1331. if (elysia_cron_is_job_running($job)) {
  1332. if (time() - elysia_cron_get_job_last_run($job, 0) > variable_get('elysia_cron_stuck_time', 3600)) {
  1333. elysia_cron_warning('Job %job is already running, but is probably stuck, so i consider it as terminated', array('%job' => $job));
  1334. }
  1335. else {
  1336. elysia_cron_warning('Job %job is already running', array('%job' => $job));
  1337. $job_running = TRUE;
  1338. }
  1339. }
  1340. return $job_running;
  1341. }
  1342. /**
  1343. * Find an idle channel (not running, or stuck). If found one, set it as running and returns available jobs.
  1344. *
  1345. * @return if found returns array { 'name' => name of channel, 'jobs' => array of active jobs }, else return FALSE
  1346. */
  1347. function elysia_cron_run_available_channel($ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
  1348. global $_elysia_cron_settings_by_channel;
  1349. $channels = array_keys($_elysia_cron_settings_by_channel);
  1350. $channel = elysia_cron_last_channel();
  1351. $i = array_search($channel, $channels);
  1352. if ($i === FALSE) {
  1353. $i = -1;
  1354. }
  1355. $k = 0;
  1356. $jobs = FALSE;
  1357. for ($j = ($i + 1) % count($channels); $k < count($channels); $j = ($j + 1) % count($channels)) {
  1358. $jobs = elysia_cron_check_run_channel($channels[$j], $ignore_disable, $ignore_time, $ignore_running);
  1359. if ($jobs && count($jobs)) {
  1360. break;
  1361. }
  1362. $k++;
  1363. }
  1364. if ($jobs) {
  1365. elysia_cron_set_last_channel($channels[$j]);
  1366. }
  1367. return $jobs ? array('name' => $channels[$j], 'jobs' => $jobs) : FALSE;
  1368. }
  1369. function elysia_cron_execute_aborted($channel) {
  1370. global $_elysia_cron_settings_by_channel;
  1371. $last_job = '';
  1372. foreach ($_elysia_cron_settings_by_channel[$channel] as $job => $conf) {
  1373. if ($job != '#data') {
  1374. if (elysia_cron_is_job_running($job)) {
  1375. $last_job .= ' ' . $job;
  1376. elysia_cron_set_job_running($job, 0);
  1377. }
  1378. }
  1379. }
  1380. // In case the last function to run is longer than 128 chars, we cap the
  1381. // length prior to placing in the database.
  1382. $last_job = trim($last_job);
  1383. if (strlen($last_job) > 128) {
  1384. $last_job = substr($last_job, 0, 128);
  1385. }
  1386. elysia_cron_set($channel, TRUE, array(
  1387. 'running' => 0, //time(),.
  1388. 'last_aborted' => 1,
  1389. 'abort_count' => elysia_cron_get_channel_abort_count($channel) + 1,
  1390. 'last_abort_function' => $last_job,
  1391. ));
  1392. return $last_job;
  1393. }
  1394. /**
  1395. * Shutdown function for cron cleanup.
  1396. *
  1397. * Used for unexpected termination of code.
  1398. */
  1399. function elysia_cron_internal_execute_channel_cleanup() {
  1400. global $_elysia_cron_settings, $_elysia_cron_current_channel, $_cron_completed, $_cron_completed_time;
  1401. if ($_cron_completed) {
  1402. return;
  1403. }
  1404. // See if the semaphore is still locked.
  1405. if (elysia_cron_is_channel_running($_elysia_cron_current_channel)) {
  1406. $last_job = elysia_cron_execute_aborted($_elysia_cron_current_channel);
  1407. elysia_cron_warning('Unexpected termination of cron channel %channel, aborted. Last job executed: %job', array('%channel' => $_elysia_cron_current_channel, '%job' => $last_job));
  1408. }
  1409. }
  1410. /**
  1411. * Successful termination (after all shutdown hooks invoked by cron functions).
  1412. */
  1413. function elysia_cron_internal_execute_channel_completed() {
  1414. global $_elysia_cron_current_channel, $_cron_completed_time;
  1415. // Record cron time.
  1416. _ec_variable_set('cron_last', time());
  1417. elysia_cron_debug('Cron channel %channel run completed.', array('%channel' => $_elysia_cron_current_channel));
  1418. $stats = elysia_cron_get_channel_stats($_elysia_cron_current_channel);
  1419. $time = time() - $stats['last_run'];
  1420. $exec_count = $stats['execution_count'] + 1;
  1421. $channel_stats_data = array(
  1422. 'last_execution_time' => $time,
  1423. 'execution_count' => $exec_count,
  1424. 'avg_execution_time' => round((($stats['avg_execution_time'] * ($exec_count - 1)) + $time) / $exec_count, 2),
  1425. 'max_execution_time' => $time > $stats['max_execution_time'] ? $time : $stats['max_execution_time'],
  1426. 'last_shutdown_time' => time() - $_cron_completed_time,
  1427. 'last_aborted' => 0,
  1428. 'running' => 0,
  1429. );
  1430. elysia_cron_set_channel_stats($_elysia_cron_current_channel, $channel_stats_data);
  1431. }
  1432. /**
  1433. * Get all jobs that needs to be executed in a channel
  1434. */
  1435. function elysia_cron_active_jobs($channel, $ignore_disable = FALSE, $ignore_time = FALSE) {
  1436. global $_elysia_cron_settings_by_channel;
  1437. $jobs = array();
  1438. foreach ($_elysia_cron_settings_by_channel[$channel] as $job => $conf) {
  1439. if ($job != '#data') {
  1440. if (elysia_cron_should_run($conf, -1, $ignore_disable, $ignore_time)) {
  1441. $jobs[] = $job;
  1442. }
  1443. }
  1444. }
  1445. return $jobs;
  1446. }
  1447. /**
  1448. * Check if cron is currently running.
  1449. * (Not used by elysia_cron, can be used by external modules)
  1450. */
  1451. function elysia_cron_is_running() {
  1452. global $_elysia_cron_settings_by_channel;
  1453. elysia_cron_initialize();
  1454. $running = array();
  1455. foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
  1456. if (elysia_cron_is_channel_running($channel)) {
  1457. $running[] = $channel;
  1458. }
  1459. }
  1460. return $running;
  1461. }
  1462. function elysia_cron_job_exists($job) {
  1463. global $_elysia_cron_settings;
  1464. return isset($_elysia_cron_settings[$job]);
  1465. }
  1466. function elysia_cron_channel_exists($channel) {
  1467. global $_elysia_cron_settings_by_channel;
  1468. return isset($_elysia_cron_settings_by_channel[$channel]);
  1469. }
  1470. /**
  1471. * Obtain job description (translated)
  1472. */
  1473. function elysia_cron_description($job) {
  1474. global $_elysia_cron_settings;
  1475. if (!empty($_elysia_cron_settings[$job]['description'])) {
  1476. $desc = $_elysia_cron_settings[$job]['description'];
  1477. }
  1478. else {
  1479. $desc = theme('elysia_cron_description', array('job' => $job));
  1480. }
  1481. return t($desc);
  1482. }
  1483. /*******************************************************************************
  1484. * THEMING
  1485. ******************************************************************************/
  1486. /**
  1487. * Implementation of hook_theme().
  1488. */
  1489. function elysia_cron_theme() {
  1490. return array(
  1491. 'elysia_cron_description' => array(
  1492. 'variables' => array('job' => NULL),
  1493. ),
  1494. 'elysia_cron_settings_form' => array(
  1495. 'render element' => 'form',
  1496. ),
  1497. );
  1498. }
  1499. /**
  1500. * You can theme this function to provide your (untranslated) descriptions for cron functions, if they do not provide one.
  1501. */
  1502. function theme_elysia_cron_description($variables) {
  1503. switch ($variables['job']) {
  1504. case 'search_cron':
  1505. return 'Update search database index';
  1506. case 'activitystream_cron':
  1507. return 'Fetch RSS feeds and web calls for activitystream';
  1508. case 'mailhandler_cron':
  1509. return 'Fetch POP3/IMAP accounts managed by MailHandler';
  1510. case 'watchdog_cron':
  1511. return 'Remove expired log messages and flood control events';
  1512. case 'filter_cron':
  1513. return 'Expire outdated filter cache entries';
  1514. case 'node_cron':
  1515. return 'History table cleanup';
  1516. case 'system_cron':
  1517. return 'Remove older rows from flood and batch table. Remove old temporary files.';
  1518. case 'aggregation_cron':
  1519. return 'Fetch RSS feeds for aggregation module';
  1520. case 'amazon_cron':
  1521. return 'Refresh Amazon products';
  1522. case 'image_cron':
  1523. return 'Deletes old temp images';
  1524. case 'persistent_login_cron':
  1525. return 'Expire persistent login';
  1526. case 'trackback_cron':
  1527. return 'Process trackback ping queue';
  1528. case 'update_status_cron':
  1529. return 'Checks for drupal module updates (Note: own frequency check ignore cron rules)';
  1530. case 'user_karma_cron':
  1531. return 'User karma expiration / rebuild (Note: own frequency check ignore cron rules)';
  1532. case 'votingapi_cron':
  1533. return 'Update votes (if not configured for immediate calculation)';
  1534. case 'statistics_cron':
  1535. return 'Reset day counts / Clean expired access logs';
  1536. case 'googleanalytics_cron':
  1537. return 'Delete cached version of ga.js/urchin.js.';
  1538. case 'xmlsitemap_cron':
  1539. return 'XML sitemap ping.';
  1540. case 'xmlsitemap_node_cron':
  1541. return 'Update XML sitemap with new nodes';
  1542. case 'xmlsitemap_term_cron':
  1543. return 'Update XML sitemap with new terms';
  1544. case 'lm_paypal_cron':
  1545. return 'Remove old IPN records';
  1546. case 'user_import_cron':
  1547. return 'Continue partial imports';
  1548. case 'dblog_cron':
  1549. return 'Remove expired log messages and flood control events';
  1550. case 'field_cron':
  1551. return 'Purges some deleted Field API data, if any exists';
  1552. case 'trigger_cron':
  1553. return 'Triggers cron actions';
  1554. case 'update_cron':
  1555. return 'Checks for available updates of Drupal core, contributed modules and themes';
  1556. case 'search_api_cron':
  1557. return 'Will index items for each enabled index.';
  1558. case 'redirect_cron':
  1559. return 'Purge inactive self-managed redirects from the database.';
  1560. case 'ctools_cron':
  1561. return 'Clean up old caches';
  1562. case 'l10n_update_cron':
  1563. return 'Check one project/language at a time, download and import if update available';
  1564. default:
  1565. return '-';
  1566. }
  1567. }
  1568. /*******************************************************************************
  1569. * PING SUPPORT
  1570. ******************************************************************************/
  1571. /**
  1572. * Page callback for ping page. Throws 404 if cron hasn't been called within configured time period.
  1573. */
  1574. function elysia_cron_ping_page() {
  1575. $last_run = _ec_variable_get('elysia_cron_last_run', 0);
  1576. $diff = time() - $last_run;
  1577. $max_interval = variable_get('elysia_cron_alert_interval', 60) * 60;
  1578. if ($diff > $max_interval) {
  1579. return drupal_not_found();
  1580. }
  1581. else {
  1582. $aoutput = array();
  1583. $aoutput[] = array(
  1584. '#type' => 'markup',
  1585. '#markup' => t('Cron has been called within maximum lapse time.'),
  1586. );
  1587. return $aoutput;
  1588. }
  1589. }
  1590. /*******************************************************************************
  1591. * DRUPAL QUEUE SUPPORT
  1592. * ONLY FOR D7
  1593. ******************************************************************************/
  1594. function elysia_cron_cronapi($op, $job = FALSE) {
  1595. $items = array();
  1596. $queues = module_invoke_all('cron_queue_info');
  1597. drupal_alter('cron_queue_info', $queues);
  1598. foreach ($queues as $queue_name => $info) {
  1599. if (!empty($info['skip on cron'])) {
  1600. // Do not run if queue wants to skip.
  1601. continue;
  1602. }
  1603. // Make sure every queue exists. There is no harm in trying to recreate an
  1604. // existing queue.
  1605. $queue = DrupalQueue::get($queue_name);
  1606. $queue->createQueue();
  1607. // Some queue backends may have performance issues with counting items.
  1608. if (variable_get('elysia_cron_queue_show_count', TRUE)) {
  1609. $queue_description = $queue_name . ' queue processing (Items count: <strong>' . $queue->numberOfItems() . '</strong>, worker max duration: <strong>' . (isset($info['time']) ? $info['time'] . 's' : t('unspecified') . ' (15s)') . '</strong>)';
  1610. }
  1611. else {
  1612. $queue_description = $queue_name . ' queue processing (Worker max duration: <strong>' . (isset($info['time']) ? $info['time'] . 's' : t('unspecified') . ' (15s)') . '</strong>)';
  1613. }
  1614. $items['queue_' . $queue_name] = array(
  1615. 'description' => $queue_description,
  1616. 'rule' => variable_get('elysia_cron_queue_default_rule', FALSE),
  1617. 'weight' => variable_get('elysia_cron_queue_default_weight', 100),
  1618. 'callback' => 'elysia_cron_queue_exec',
  1619. 'arguments' => array($queue_name, $info),
  1620. );
  1621. }
  1622. return $items;
  1623. }
  1624. function elysia_cron_queue_exec($queue_name, $info) {
  1625. $function = $info['worker callback'];
  1626. $end = time() + (isset($info['time']) ? $info['time'] : 15);
  1627. $queue = DrupalQueue::get($queue_name);
  1628. while (time() < $end && ($item = $queue->claimItem())) {
  1629. try {
  1630. $function($item->data);
  1631. $queue->deleteItem($item);
  1632. }
  1633. catch (Exception $e) {
  1634. // In case of exception log it and leave the item in the queue
  1635. // to be processed again later.
  1636. watchdog_exception('cron', $e);
  1637. }
  1638. }
  1639. }
  1640. /*******************************************************************************
  1641. * ADMIN_MENU SUPPORT
  1642. ******************************************************************************/
  1643. // @todo Deve gestire hook_admin_menu_map, vedi hook_admin_menu.php e admin_menu.map.inc
  1644. /**
  1645. * Implements hook_admin_menu().
  1646. *
  1647. * @param array &$deleted
  1648. * Array of links under admin/* that were removed by
  1649. * admin_menu_adjust_items(). If one of these links is added back,
  1650. * it should be removed from the array.
  1651. */
  1652. function elysia_cron_admin_menu(&$deleted) {
  1653. $links = array();
  1654. elysia_cron_initialize();
  1655. global $_elysia_cron_settings_by_channel;
  1656. // Add link to manually run cron.
  1657. $links[] = array(
  1658. 'title' => 'Elysia cron manual',
  1659. 'path' => 'admin/config/system/cron',
  1660. 'weight' => 50,
  1661. 'has_children' => FALSE,
  1662. 'parent_path' => '<front>',
  1663. );
  1664. foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
  1665. foreach ($data as $job => $conf) {
  1666. if ($job != '#data') {
  1667. $links[] = array(
  1668. 'title' => "Elysia cron run manual !title",
  1669. 'path' => 'admin/config/system/cron/execute/' . $job,
  1670. 'weight' => -50,
  1671. 'query' => 'destination',
  1672. 'parent_path' => 'admin/config/system/cron',
  1673. 'options' => array('t' => array('!title' => $conf['module'])),
  1674. );
  1675. }
  1676. }
  1677. }
  1678. return $links;
  1679. }
  1680. /**
  1681. * Check if function available to use.
  1682. *
  1683. * Some native php function can be disabled by configuration,
  1684. * to avoid a lot of notices we check availability of such functions.
  1685. *
  1686. * @param string $func
  1687. * Function to check.
  1688. *
  1689. * @return bool
  1690. * TRUE if function available for use, FALSE otherwise.
  1691. */
  1692. function _elysia_cron_function_available($func) {
  1693. $disabled = explode(',', ini_get('disable_functions'));
  1694. return !in_array($func, $disabled);
  1695. }
  1696. /**
  1697. * Get list of columns from elysia_cron table.
  1698. *
  1699. * @return array
  1700. * List of columns.
  1701. */
  1702. function _elysia_cron_columns() {
  1703. return array(
  1704. 'name',
  1705. 'disable',
  1706. 'rule',
  1707. 'weight',
  1708. 'context',
  1709. 'running',
  1710. 'last_run',
  1711. 'last_aborted',
  1712. 'abort_count',
  1713. 'last_abort_function',
  1714. 'last_execution_time',
  1715. 'execution_count',
  1716. 'avg_execution_time',
  1717. 'max_execution_time',
  1718. 'last_shutdown_time',
  1719. );
  1720. }
  1721. /**
  1722. * Get list of predefined cron rules.
  1723. *
  1724. * @return array
  1725. * List of predefined rules.
  1726. */
  1727. function _elysia_cron_default_rules() {
  1728. return array(
  1729. '*/15 * * * *' => 'Every 15 minutes',
  1730. '*/30 * * * *' => 'Every 30 minutes',
  1731. '0 * * * *' => 'Every hour',
  1732. '0 */6 * * *' => 'Every 6 hours',
  1733. '4 0 * * *' => 'Once a day',
  1734. '4 0 * * 0' => 'Once a week',
  1735. '4 0 1 * *' => 'Once a month',
  1736. );
  1737. }