elysia_cron.module 58 KB

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