12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042 |
- <?php
- /**
- * @file
- * Elysia cron primary module file.
- */
- require_once 'elysia_cron_scheduler.inc';
- require_once 'elysia_cron.admin.inc';
- require_once 'elysia_cron.ctools.inc';
- /**
- * Implements hook_menu().
- */
- function elysia_cron_menu() {
- $items['admin/config/system/cron'] = array(
- 'title' => 'Cron Settings',
- 'description' => 'View and manage cron table',
- 'page callback' => 'elysia_cron_admin_page',
- 'access callback' => 'elysia_cron_access',
- 'access arguments' => array('view elysia_cron'),
- );
- $items['admin/config/system/cron/status'] = array(
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'title' => 'Status',
- 'weight' => 1,
- );
- $items['admin/config/system/cron/settings'] = array(
- 'type' => MENU_LOCAL_TASK,
- 'title' => 'Settings',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('elysia_cron_settings_form'),
- 'access callback' => 'elysia_cron_access',
- 'access arguments' => array('administer elysia_cron'),
- 'weight' => 2,
- );
- $items['admin/config/system/cron/maintenance'] = array(
- 'type' => MENU_LOCAL_TASK,
- 'title' => 'Maintenance',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('elysia_cron_reset_statistics_form'),
- 'access callback' => 'elysia_cron_access',
- 'access arguments' => array('administer elysia_cron'),
- 'weight' => 3,
- );
- $items['admin/config/system/cron/execute/%'] = array(
- 'type' => MENU_CALLBACK,
- 'page callback' => 'elysia_cron_execute_page',
- 'page arguments' => array(5),
- 'access callback' => 'elysia_cron_access',
- 'access arguments' => array('execute elysia_cron'),
- );
- $items['admin/build/cron/ping'] = array(
- 'type' => MENU_CALLBACK,
- 'page callback' => 'elysia_cron_ping_page',
- 'access callback' => TRUE,
- );
- return $items;
- }
- /**
- * Implements hook_menu_alter().
- */
- function elysia_cron_menu_alter(&$items) {
- // Override default cron page.
- $items['admin/config/system/cron']['title'] = 'Cron Settings';
- $items['admin/config/system/cron']['description'] = 'View and manage cron table';
- $items['admin/config/system/cron']['page callback'] = 'elysia_cron_admin_page';
- $items['admin/config/system/cron']['access callback'] = 'elysia_cron_access';
- $items['admin/config/system/cron']['access arguments'] = array('view elysia_cron');
- $items['admin/config/system/cron']['file'] = NULL;
- $items['admin/reports/status/run-cron']['page callback'] = 'elysia_cron_run_manually';
- $items['admin/reports/status/run-cron']['file'] = NULL;
- }
- /**
- * Check access to operation inside elysia cron module.
- *
- * Permission have nested structure, for example: "administer elysia_cron" allow
- * to do anything: view, execute and configure cron jobs.
- *
- * @param string $perm
- * Permission name.
- *
- * @return bool
- * Access granted or not.
- */
- function elysia_cron_access($perm) {
- $access = FALSE;
- switch ($perm) {
- case 'administer elysia_cron':
- $access = user_access($perm);
- break;
- case 'execute elysia_cron':
- $access = user_access($perm) || user_access('administer elysia_cron');
- break;
- case 'view elysia_cron';
- $access = user_access($perm) || user_access('execute elysia_cron') || user_access('administer elysia_cron');
- }
- return $access;
- }
- /**
- * Implements hook_permission().
- */
- function elysia_cron_permission() {
- return array(
- 'administer elysia_cron' => array(
- 'title' => t('Administer elysia cron'),
- 'description' => t('Perform changes to cron jobs timings, disable cron or single jobs and access cron execution statistics'),
- 'restrict access' => TRUE,
- ),
- 'execute elysia_cron' => array(
- 'title' => t('Execute elysia cron jobs'),
- 'description' => t('Allow users to view statistics, execution status and do manually execute cron jobs'),
- ),
- 'view elysia_cron' => array(
- 'title' => t('View elysia cron stats'),
- 'description' => t('Allows users to view statistics and execution status of cron jobs'),
- ),
- );
- }
- /**
- * Implements hook_exit().
- */
- function elysia_cron_exit() {
- global $_elysia_cron_exit_phase;
- $_elysia_cron_exit_phase = TRUE;
- }
- /**
- * Implements hook_cron().
- *
- * Hook cron is invoked only by standard drupal cron.
- * It's used to replace drupal cron.
- */
- function elysia_cron_cron() {
- global $_elysia_cron_exit_phase, $_elysia_cron_drush;
- // First cron run is executed in standard drupal way.
- // This is to enable the use of install profiles.
- if (variable_get('cron_last', 0) <= variable_get('install_time', 0)) {
- return;
- }
- // If cron has been executed via "drush core-cron" or any other custom drush
- // command then we run internal cron handler which is designed to handle
- // cron executions from drush.
- if (function_exists('elysia_cron_drush_detect') && elysia_cron_drush_detect()) {
- elysia_cron_drush_invoke();
- }
- // If the path is 'admin/*', or if the user is not anonymous,
- // this is a manual cron run (probably by admin/logs/status),
- // but not if we are in the exit phase (= this a "poormanscron" run).
- $manual_run = empty($_elysia_cron_exit_phase) && ((arg(0) == 'admin') || !empty($GLOBALS['user']->uid));
- $result = elysia_cron_run($manual_run);
- if ($manual_run) {
- if ($result) {
- elysia_cron_message('Cron ran successfully');
- }
- else {
- elysia_cron_message('Cron run failed, disabled or nothing to do');
- }
- // In manual execution it's better to set cron_last variable in standard way
- // (so the user sees the execution time updates).
- // This invalidates variable cache, but it's a manual execution,
- // it should be not a great performance problem.
- if (empty($_elysia_cron_drush)) {
- variable_set('cron_last', time());
- }
- drupal_goto('admin/reports/status');
- }
- // If we are in "poormanscron" mode it's better to force setting of cron_last.
- // This invalidates variable cache, but is needed for right execution check.
- // @see system_run_automated_cron() in system.module
- if (!empty($_elysia_cron_exit_phase)) {
- variable_set('cron_last', time());
- }
- drupal_exit();
- }
- /**
- * Implements hook_cron().
- *
- * I use help section for admin/build/modules page to check if elysia_cron
- * is the module with the smallest weight.
- * If it's not i'll set it and print a message.
- */
- function elysia_cron_help($section, $arg = FALSE) {
- if ($section == 'admin/modules') {
- $query = db_select('system', 's');
- $query->addExpression('MIN(weight)');
- $min = $query->condition('s.name', 'elysia_cron', '!=')
- ->execute()
- ->fetchField();
- $weight = db_select('system', 's')
- ->fields('s', array('weight'))
- ->condition('s.name', 'elysia_cron')
- ->execute()
- ->fetchField();
- if ($min <= $weight) {
- elysia_cron_message('Elysia cron module is not the module with the smallest weight (and it must be). Updating weight...');
- db_update('system')
- ->fields(array('weight' => $min - 1))
- ->condition('name', 'elysia_cron')
- ->execute();
- }
- }
- }
- // Variables managed by _ec_variable method,
- // because are setted during cron execution handling
- // (with standard variable_set this will invalidate variable
- // cache in EVERY cron clock).
- $GLOBALS['_ec_variables_allowed'] = array(
- 'elysia_cron_semaphore',
- 'elysia_cron_last_run',
- 'elysia_cron_last_channel',
- 'cron_semaphore',
- 'cron_last',
- );
- /**
- * Initialise global variables.
- */
- function _ec_variable_init() {
- global $_ec_variables, $_ec_variables_allowed;
- $_ec_variables = array();
- foreach ($_ec_variables_allowed as $name) {
- $_ec_variables[$name] = variable_get($name);
- }
- }
- /**
- * A substitute for variable_get to avoid cache management.
- *
- * Use ONLY for variables set during cron execution:
- * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
- * cron_semaphore, cron_last
- * WARN_UPGRADE.
- */
- function _ec_variable_get($name, $default) {
- global $_ec_variables, $_ec_variables_allowed;
- if (!in_array($name, $_ec_variables_allowed)) {
- elysia_cron_error('Wrong variable passed to _ec_variable_get: !var', array('!var' => $name), TRUE);
- return variable_get($name, $default);
- }
- if (!is_array($_ec_variables)) {
- _ec_variable_init();
- }
- // If there is a $GLOBALS['original_conf'] = $conf; at the end of settings.php
- // i consider it.
- global $_original_conf;
- if (isset($_original_conf[$name])) {
- return $_original_conf[$name];
- }
- if (isset($_ec_variables[$name])) {
- return $_ec_variables[$name];
- }
- return $default;
- }
- /**
- * A substitute for variable_set to avoid cache management.
- *
- * Use ONLY for variables set during cron execution:
- * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
- * cron_semaphore, cron_last.
- * WARN_UPGRADE.
- */
- function _ec_variable_set($name, $value) {
- global $_ec_variables, $_ec_variables_allowed;
- if (!in_array($name, $_ec_variables_allowed)) {
- elysia_cron_error('Wrong variable passed to _ec_variable_set: !var', array('!var' => $name), TRUE);
- variable_set($name, $value);
- }
- if (!is_array($_ec_variables)) {
- _ec_variable_init();
- }
- db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
- $_ec_variables[$name] = $value;
- }
- /**
- * A substitute for variable_del to avoid cache management.
- *
- * Use ONLY for variables set during cron execution:
- * elysia_cron_semaphore, elysia_cron_last_run, elysia_cron_last_channel,
- * cron_semaphore, cron_last
- * WARN_UPGRADE.
- */
- function _ec_variable_del($name) {
- global $_ec_variables, $_ec_variables_allowed;
- if (!in_array($name, $_ec_variables_allowed)) {
- elysia_cron_error('Wrong variable passed to _ec_variable_del: !var', array('!var' => $name), TRUE);
- variable_del($name);
- return;
- }
- if (!is_array($_ec_variables)) {
- _ec_variable_init();
- }
- db_delete('variable')->condition('name', $name)->execute();
- unset($_ec_variables[$name]);
- }
- function _ec_get_name($name) {
- $maxlen = 120;
- if (strlen($name) < $maxlen) {
- return $name;
- }
- $border = ($maxlen - 32) / 2;
- return substr($name, 0, $border) . md5($name) . substr($name, -$border);
- }
- function elysia_cron_set($name, $channel = FALSE, $values = array()) {
- if ($channel) {
- $name = ':' . $name;
- }
- db_merge('elysia_cron')->key(array('name' => $name))->fields($values)->execute();
- global $_elysia_cron_db_cache;
- unset($_elysia_cron_db_cache[$name]);
- }
- function elysia_cron_get($name, $channel = FALSE, $key = FALSE, $default = FALSE, $refresh = FALSE) {
- global $_elysia_cron_db_cache;
- static $elysia_cron_defaults;
- if (!isset($elysia_cron_defaults)) {
- $elysia_cron_defaults = function_exists('elysia_cron_get_ctools_defaults') ? elysia_cron_get_ctools_defaults() : array();
- }
- if ($channel) {
- $name = ':' . $name;
- }
- if ($refresh || !isset($_elysia_cron_db_cache[$name])) {
- $_elysia_cron_db_cache[$name] = db_select('elysia_cron', 'ec')
- ->fields('ec', _elysia_cron_columns())
- ->condition('name', $name)
- ->execute()
- ->fetchAssoc();
- if (!$_elysia_cron_db_cache[$name] && isset($elysia_cron_defaults[$name])) {
- $_elysia_cron_db_cache[$name] = (array) $elysia_cron_defaults[$name];
- }
- elseif ($_elysia_cron_db_cache[$name] && isset($elysia_cron_defaults[$name])) {
- foreach ($elysia_cron_defaults[$name] as $k => $v) {
- if (!isset($_elysia_cron_db_cache[$name][$k]) || is_null($_elysia_cron_db_cache[$name][$k])) {
- $_elysia_cron_db_cache[$name][$k] = $v;
- }
- }
- }
- }
- 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];
- }
- /**
- * Get status of specified channel.
- *
- * @param string $channel
- * Channel name.
- * @param bool $default
- * Default value for channel status.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return bool
- * Channel status.
- */
- function elysia_cron_is_channel_disabled($channel, $default = FALSE, $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ecc_' . _ec_get_name($channel) . '_d'])) {
- return $GLOBALS['conf'][$n];
- }
- return elysia_cron_get($channel, TRUE, 'disable', $default, $refresh);
- }
- /**
- * Update channel status.
- *
- * @param string $channel
- * Channel name.
- * @param bool $value
- * New channel status:
- * TRUE - enabled
- * FALSE - disabled.
- */
- function elysia_cron_set_channel_disabled($channel, $value) {
- elysia_cron_set($channel, TRUE, array('disable' => $value ? 1 : 0));
- }
- /**
- * Reset channel status to default (disabled).
- *
- * @param string $channel
- * Channel name.
- */
- function elysia_cron_reset_channel_disabled($channel) {
- elysia_cron_set($channel, TRUE, array('disable' => 0));
- }
- /**
- * Get channel's rule.
- *
- * @param string $channel
- * Channel name.
- * @param string $default
- * Default rule.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return string
- * Formatted rule for specified channel.
- */
- function elysia_cron_get_channel_rule($channel, $default = '', $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ecc_' . _ec_get_name($channel) . '_rul'])) {
- return $GLOBALS['conf'][$n];
- }
- return elysia_cron_get($channel, TRUE, 'rule', $default, $refresh);
- }
- /**
- * Set new rule for channel.
- *
- * @param string $channel
- * Channel name.
- * @param string $rule
- * Crontab formatted rule.
- */
- function elysia_cron_set_channel_rule($channel, $rule) {
- elysia_cron_set($channel, TRUE, array('rule' => $rule));
- }
- /**
- * Reset channel's rule to default.
- *
- * @param string $channel
- * Channel name.
- */
- function elysia_cron_reset_channel_rule($channel) {
- elysia_cron_set($channel, TRUE, array('rule' => NULL));
- }
- /**
- * Check if channel is running now.
- *
- * @param string $channel
- * Channel name.
- * @param int $default
- * Default value.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return int
- * Timestamp from beginning of running or zero.
- */
- function elysia_cron_is_channel_running($channel, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($channel, TRUE, 'running', $default, $refresh);
- }
- /**
- * Updated channel running value.
- *
- * @param string $channel
- * Channel name.
- * @param int $value
- * New running value.
- */
- function elysia_cron_set_channel_running($channel, $value) {
- elysia_cron_set($channel, TRUE, array('running' => $value));
- }
- /**
- * Get channel's last run value.
- *
- * @param string $channel
- * Channel name.
- * @param int $default
- * Default value.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return int
- * Last run timestamp or zero.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_get_channel_last_run($channel, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($channel, TRUE, 'last_run', $default, $refresh);
- }
- /**
- * Set channel's last run value.
- *
- * @param string $channel
- * Channel name.
- * @param int $value
- * New last run timestamp.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_set_channel_last_run($channel, $value) {
- elysia_cron_set($channel, TRUE, array('last_run' => $value));
- }
- /**
- * Get channel's last aborted value.
- *
- * @param string $channel
- * Channel name.
- * @param int $default
- * Default value.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return int
- * Last aborted timestamp or zero.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_get_channel_last_aborted($channel, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($channel, TRUE, 'last_aborted', $default, $refresh);
- }
- /**
- * Set channel's last aborted value.
- *
- * @param string $channel
- * Channel name.
- * @param int $value
- * New last aborted timestamp.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_set_channel_last_aborted($channel, $value) {
- elysia_cron_set($channel, TRUE, array('last_aborted' => $value ? 1 : 0));
- }
- /**
- * Get channel's about count.
- *
- * @param string $channel
- * Channel name.
- * @param int $default
- * Default value.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return int
- * Abort count for specified channel.
- */
- function elysia_cron_get_channel_abort_count($channel, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($channel, TRUE, 'abort_count', $default, $refresh);
- }
- /**
- * Set channel's abort count.
- *
- * @param string $channel
- * Channel name.
- * @param int $value
- * New abort count.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_set_channel_abort_count($channel, $value) {
- elysia_cron_set($channel, TRUE, array('abort_count' => $value));
- }
- /**
- * Get channel's last about function.
- *
- * @param string $channel
- * Channel name.
- * @param string $default
- * Default value.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return string
- * Last abort function or empty string.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_get_channel_last_abort_function($channel, $default = '', $refresh = FALSE) {
- return elysia_cron_get($channel, TRUE, 'last_abort_function', $default, $refresh);
- }
- /**
- * Set channel's last abort function.
- *
- * @param string $channel
- * Channel name.
- * @param string $value
- * Last abort function name.
- *
- * @todo Not used, to delete?
- */
- function elysia_cron_set_channel_last_abort_function($channel, $value) {
- elysia_cron_set($channel, TRUE, array('last_abort_function' => $value));
- }
- /**
- * Grab statistics by channel.
- *
- * @param string $channel
- * Channel name.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return array
- * Statistics for specified channel.
- */
- function elysia_cron_get_channel_stats($channel, $refresh = FALSE) {
- return array(
- 'last_run' => elysia_cron_get($channel, TRUE, 'last_run', 0, $refresh),
- 'last_execution_time' => elysia_cron_get($channel, TRUE, 'last_execution_time', 0, $refresh),
- 'execution_count' => elysia_cron_get($channel, TRUE, 'execution_count', 0, $refresh),
- 'avg_execution_time' => elysia_cron_get($channel, TRUE, 'avg_execution_time', 0, $refresh),
- 'max_execution_time' => elysia_cron_get($channel, TRUE, 'max_execution_time', 0, $refresh),
- 'last_shutdown_time' => elysia_cron_get($channel, TRUE, 'last_shutdown_time', 0, $refresh),
- 'last_aborted' => elysia_cron_get($channel, TRUE, 'last_aborted', 0, $refresh),
- 'abort_count' => elysia_cron_get($channel, TRUE, 'abort_count', 0, $refresh),
- 'last_abort_function' => elysia_cron_get($channel, TRUE, 'last_abort_function', 0, ''),
- );
- }
- /**
- * Update channel's stats.
- *
- * @param string $channel
- * Channel name.
- * @param array $data
- * Associative array of values.
- */
- function elysia_cron_set_channel_stats($channel, array $data = array()) {
- elysia_cron_set($channel, TRUE, $data);
- }
- /**
- * Get formatted crontab rule for specified job.
- *
- * @param string $job
- * Job name.
- * @param string $default
- * Default rule.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return string
- * Formatted job rule.
- */
- function elysia_cron_get_job_rule($job, $default = '', $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_rul'])) {
- return $GLOBALS['conf'][$n];
- }
- return elysia_cron_get($job, FALSE, 'rule', $default, $refresh);
- }
- /**
- * Set formatted crontab rule for specified job.
- *
- * @param string $job
- * Job name.
- * @param string $value
- * New crontab rule for job.
- */
- function elysia_cron_set_job_rule($job, $value) {
- elysia_cron_set($job, FALSE, array('rule' => $value));
- }
- /**
- * Reset crontab job rule to default.
- *
- * @param string $job
- * Job name.
- */
- function elysia_cron_reset_job_rule($job) {
- elysia_cron_set($job, FALSE, array('rule' => NULL));
- }
- /**
- * Get weight of specified job.
- *
- * @param string $job
- * Job name.
- * @param string $default
- * Default weight.
- * @param bool $refresh
- * If TRUE - fresh status will be get from database.
- *
- * @return string
- * Job weight or empty string.
- */
- function elysia_cron_get_job_weight($job, $default = '', $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_w'])) {
- return $GLOBALS['conf'][$n];
- }
- return elysia_cron_get($job, FALSE, 'weight', $default, $refresh);
- }
- /**
- * Set weight specified job.
- *
- * @param string $job
- * Job name.
- * @param string $value
- * New weight for job.
- */
- function elysia_cron_set_job_weight($job, $value) {
- elysia_cron_set($job, FALSE, array('weight' => $value));
- }
- /**
- * Reset job weight to default.
- *
- * @param string $job
- * Job name.
- */
- function elysia_cron_reset_job_weight($job) {
- elysia_cron_set($job, FALSE, array('weight' => NULL));
- }
- function elysia_cron_is_job_disabled($job, $default = FALSE, $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_d'])) {
- return $GLOBALS['conf'][$n];
- }
- return elysia_cron_get($job, FALSE, 'disable', $default, $refresh);
- }
- function elysia_cron_set_job_disabled($job, $value) {
- elysia_cron_set($job, FALSE, array('disable' => $value ? 1 : 0));
- }
- function elysia_cron_reset_job_disabled($job) {
- elysia_cron_set($job, FALSE, array('disable' => NULL));
- }
- function elysia_cron_get_job_channel($job, $default = '', $refresh = FALSE) {
- // May be overridden by a static conf.
- if (isset($GLOBALS['conf'][$n = 'ec_' . _ec_get_name($job) . '_c'])) {
- $c = $GLOBALS['conf'][$n];
- }
- else {
- // In this place, 'context' means 'channel'.
- $c = elysia_cron_get($job, FALSE, 'context', $default, $refresh);
- }
- return !$c ? $default : $c;
- }
- function elysia_cron_set_job_channel($job, $value) {
- // In this place, 'context' means 'channel'.
- elysia_cron_set($job, FALSE, array('context' => $value));
- }
- function elysia_cron_reset_job_channel($job) {
- // In this place, 'context' means 'channel'.
- elysia_cron_set($job, FALSE, array('context' => NULL));
- }
- function elysia_cron_is_job_running($job, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($job, FALSE, 'running', $default, $refresh);
- }
- function elysia_cron_set_job_running($job, $value) {
- elysia_cron_set($job, FALSE, array('running' => $value));
- }
- function elysia_cron_get_job_last_run($job, $default = 0, $refresh = FALSE) {
- return elysia_cron_get($job, FALSE, 'last_run', $default, $refresh);
- }
- function elysia_cron_set_job_last_run($job, $value) {
- elysia_cron_set($job, FALSE, array('last_run' => $value));
- }
- function elysia_cron_get_job_stats($job, $refresh = FALSE) {
- return array(
- 'last_run' => elysia_cron_get($job, FALSE, 'last_run', 0, $refresh),
- 'last_execution_time' => elysia_cron_get($job, FALSE, 'last_execution_time', 0, $refresh),
- 'execution_count' => elysia_cron_get($job, FALSE, 'execution_count', 0, $refresh),
- 'avg_execution_time' => elysia_cron_get($job, FALSE, 'avg_execution_time', 0, $refresh),
- 'max_execution_time' => elysia_cron_get($job, FALSE, 'max_execution_time', 0, $refresh),
- );
- }
- function elysia_cron_set_job_stats(
- $job,
- $last_run = -1,
- $last_execution_time = -1,
- $execution_count = -1,
- $avg_execution_time = -1,
- $max_execution_time = -1,
- $data = array()
- ) {
- if ($last_run != -1) {
- $data['last_run'] = $last_run;
- }
- if ($last_execution_time != -1) {
- $data['last_execution_time'] = $last_execution_time;
- }
- if ($execution_count != -1) {
- $data['execution_count'] = $execution_count;
- }
- if ($avg_execution_time != -1) {
- $data['avg_execution_time'] = $avg_execution_time;
- }
- if ($max_execution_time != -1) {
- $data['max_execution_time'] = $max_execution_time;
- }
- elysia_cron_set($job, FALSE, $data);
- }
- function elysia_cron_last_channel() {
- return _ec_variable_get('elysia_cron_last_channel', '');
- }
- function elysia_cron_set_last_channel($channel) {
- _ec_variable_set('elysia_cron_last_channel', $channel);
- }
- function elysia_cron_reset_stats() {
- global $_elysia_cron_settings, $_elysia_cron_settings_by_channel;
- elysia_cron_initialize();
- foreach ($_elysia_cron_settings as $job => $conf) {
- elysia_cron_set_job_stats($job, 0, 0, 0, 0, 0);
- }
- $channel_reset_data = array(
- 'last_run' => 0,
- 'last_execution_time' => 0,
- 'execution_count' => 0,
- 'avg_execution_time' => 0,
- 'max_execution_time' => 0,
- 'last_shutdown_time' => 0,
- 'last_aborted' => 0,
- 'abort_count' => 0,
- 'last_abort_function' => 0,
- );
- foreach ($_elysia_cron_settings_by_channel as $channel => $conf) {
- elysia_cron_set_channel_stats($channel, $channel_reset_data);
- }
- }
- /*******************************************************************************
- * INTERNAL
- *
- * WARN: Below this point the word "context" should be avoided (use channel)
- * Disabled should always be referenced as "disabled" (in db is "disable" for
- * compatibility with Ctools )
- *******************************************************************************/
- /**
- * Sends a standard message to user.
- *
- * Drush see it.
- */
- function elysia_cron_message($message, $vars = array()) {
- global $_elysia_cron_drush;
- if (empty($_elysia_cron_drush)) {
- drupal_set_message(t($message, $vars));
- }
- else {
- drush_log(strip_tags(dt($message, $vars)), 'ok');
- }
- }
- /**
- * Send an error message to user (and log also an error).
- *
- * Drush see it (even if $skip_user_message)
- */
- function elysia_cron_error($message, $vars = array(), $skip_user_message = FALSE) {
- global $_elysia_cron_drush;
- if (empty($_elysia_cron_drush) && !$skip_user_message) {
- drupal_set_message(t($message, $vars), 'error');
- }
- /* @todo remove following code if not necessary.
- if (!empty($_elysia_cron_drush)) {
- drush_log(strip_tags(dt($message, $vars)), "error");
- }*/
- watchdog('cron', $message, $vars, WATCHDOG_ERROR);
- }
- /**
- * Log a debug in watchdog (do not print an user message).
- *
- * Drush see it in verbose mode.
- */
- function elysia_cron_debug($message, $vars = array(), $type = WATCHDOG_NOTICE) {
- global $_elysia_cron_drush;
- if ($type < WATCHDOG_NOTICE || variable_get('elysia_cron_debug_messages', 0)) {
- watchdog('cron', $message, $vars, $type);
- }
- if (!empty($_elysia_cron_drush) && $_elysia_cron_drush >= 2 && ($type >= WATCHDOG_NOTICE || drush_get_option("verbose", FALSE))) {
- if ($type >= WATCHDOG_NOTICE) {
- drush_print(strip_tags(dt($message, $vars)));
- }
- else {
- drush_log(strip_tags(dt($message, $vars)), $type == WATCHDOG_ERROR ? "error" : (($type == WATCHDOG_ERROR ? "warning" : "notice")));
- }
- }
- }
- /**
- * Log a warning (do not print an user message).
- *
- * Drush see it in verbose mode.
- */
- function elysia_cron_warning($message, $vars = array()) {
- elysia_cron_debug($message, $vars, WATCHDOG_WARNING);
- }
- function elysia_cron_decode_script($text, $apply = TRUE) {
- global $_elysia_cron_settings;
- $lines = explode(PHP_EOL, $text);
- $lastcomment = '';
- $errors = array();
- foreach ($lines as $line) {
- $line = trim($line);
- if (!empty($line)) {
- if ($line{0} == '#') {
- $lastcomment = trim(substr($line, 1));
- }
- elseif (preg_match('/^(-[ ]*|)([0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+)[ ]+((?:ctx|ch):([a-zA-Z0-9_-]+)[ ]+|)([^(:]+)(\(.*\);|)$/', $line, $r)) {
- $c = array(
- 'disabled' => !empty($r[1]),
- 'rule' => $r[2],
- 'description' => $lastcomment,
- 'channel' => $r[4] ? $r[4] : 'default',
- );
- $lastcomment = '';
- if (empty($r[6])) {
- if (!isset($_elysia_cron_settings[$r[5]])) {
- // Referring a module function that not exists.
- $errors[] = $line;
- continue;
- }
- $name = $r[5];
- }
- else {
- // Custom expression, generate a unique name.
- $postfix = '';
- while (isset($_elysia_cron_settings[$r[5] . $postfix])) {
- $postfix = ($postfix ? $postfix : 0) + 1;
- }
- $name = $r[5] . $postfix;
- $c['expression'] = $r[5] . $r[6];
- }
- if ($apply) {
- $_elysia_cron_settings[$name] = isset($_elysia_cron_settings[$name]) ? array_merge($_elysia_cron_settings[$name], $c) : $c;
- }
- }
- else {
- $errors[] = $line;
- }
- }
- else {
- $lastcomment = '';
- }
- }
- return count($errors) ? $errors : FALSE;
- }
- /**
- * Fetch information about cron jobs from modules.
- *
- * @return array
- * Array with jobs information.
- */
- function elysia_cron_module_jobs() {
- static $jobs;
- if (!isset($jobs)) {
- $jobs = array();
- foreach (module_implements('cron') as $module) {
- if ($module != 'elysia_cron') {
- $jobs[$module . '_cron'] = array(
- 'module' => $module,
- 'callback' => $module . '_cron',
- 'arguments' => array(),
- );
- }
- }
- foreach (module_implements('cronapi') as $module) {
- $function = $module . '_cronapi';
- if (function_exists($function)) {
- $job_list = $function('list');
- if (is_array($job_list)) {
- foreach ($job_list as $job => $data) {
- if (is_array($data)) {
- $jobs[$job] = $data;
- }
- else {
- // Compatibility with v1.x settings.
- $jobs[$job] = array(
- 'description' => $data,
- 'rule' => ($rule = $function('rule', $job)) ? $rule : FALSE,
- 'weight' => ($weight = $function('weight', $job)) ? $weight : 0,
- );
- }
- $jobs[$job] = $jobs[$job] + array(
- 'module' => $module,
- 'callback' => $job,
- 'arguments' => array(),
- );
- }
- }
- }
- }
- if (function_exists('drupal_alter')) {
- drupal_alter('cron', $jobs);
- }
- }
- return $jobs;
- }
- function elysia_cron_initialize($skipscript = FALSE) {
- global $_elysia_cron_settings, $_elysia_cron_settings_by_channel;
- if (empty($_elysia_cron_settings)) {
- $_elysia_cron_settings = array();
- $_elysia_cron_settings_by_channel = array();
- foreach (elysia_cron_module_jobs() as $job => $jobpars) {
- $channel = elysia_cron_get_job_channel($job, 'default');
- $defrule = !empty($jobpars['rule']) ? $jobpars['rule'] : elysia_cron_get_channel_rule($channel);
- if (!$defrule) {
- $defrule = variable_get('elysia_cron_default_rule', '0 * * * *');
- }
- $defweight = !empty($jobpars['weight']) ? $jobpars['weight'] : 0;
- if (!is_numeric($defweight)) {
- $defweight = 0;
- }
- $_elysia_cron_settings[$job] = array(
- 'key' => $job,
- 'channel' => $channel,
- 'rule' => elysia_cron_get_job_rule($job, $defrule),
- 'default_rule' => $defrule,
- 'weight' => elysia_cron_get_job_weight($job, $defweight),
- 'default_weight' => $defweight,
- 'disabled' => elysia_cron_is_job_disabled($job),
- 'running' => elysia_cron_is_job_running($job),
- ) + $jobpars;
- }
- if (!$skipscript) {
- $script = variable_get('elysia_cron_script', FALSE);
- if ($script) {
- elysia_cron_decode_script($script);
- }
- }
- uasort($_elysia_cron_settings, '_elysia_cron_sort');
- foreach ($_elysia_cron_settings as $job => &$conf) {
- $stats = elysia_cron_get_job_stats($job);
- foreach ($stats as $sk => $sv) {
- $conf[$sk] = $sv;
- }
- $_elysia_cron_settings_by_channel[$conf['channel']][$job] = &$_elysia_cron_settings[$job];
- }
- foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
- uasort($_elysia_cron_settings_by_channel[$channel], '_elysia_cron_sort');
- $_elysia_cron_settings_by_channel[$channel]['#data'] = elysia_cron_get_channel_stats($channel);
- $_elysia_cron_settings_by_channel[$channel]['#data']['disabled'] = elysia_cron_is_channel_disabled($channel);
- }
- }
- }
- function _elysia_cron_sort($a, $b) {
- if ((isset($a['weight']) ? $a['weight'] : 0) == (isset($b['weight']) ? $b['weight'] : 0)) {
- return strcmp((isset($a['key']) ? $a['key'] : ''), (isset($b['key']) ? $b['key'] : ''));
- }
- return (isset($a['weight']) ? $a['weight'] : 0) - (isset($b['weight']) ? $b['weight'] : 0);
- }
- /**
- * Prepare system for a cron execution.
- *
- * This should be called at the start of a cron execution:
- * prepare_run
- * lock_env
- * look for channel/jobs ready for execution and change internal states ...
- * unlock_env
- * before_execution
- * .. execute channel/jobs ..
- * after_execution
- * unprepare_run.
- */
- function elysia_cron_prepare_run($manual_run, $start = TRUE) {
- static $original_user;
- if ($start) {
- // Allow execution to continue even if the request gets canceled.
- @ignore_user_abort(TRUE);
- // Try to allocate enough time to run all the hook_cron implementations.
- if (!ini_get('safe_mode') && _elysia_cron_function_available('set_time_limit')) {
- @set_time_limit(variable_get('elysia_cron_time_limit', 240));
- }
- // Prevent session information from being saved while cron is running.
- drupal_save_session(FALSE);
- // Force the current user to anonymous to ensure consistent permissions on
- // cron runs (only if run by interface).
- if ($manual_run) {
- $original_user = $GLOBALS['user'];
- $GLOBALS['user'] = drupal_anonymous_user();
- }
- }
- else {
- if ($manual_run) {
- // Restore the user.
- $GLOBALS['user'] = $original_user;
- /*if (EC_DRUPAL_VERSION >= 7) {
- drupal_save_session(TRUE);
- }*/
- }
- }
- }
- /**
- * Call this after a cron execution, prepared with elysia_cron_prepare_run()
- *
- * @see: elysia_cron_prepare_run()
- */
- function elysia_cron_unprepare_run($manual_run) {
- elysia_cron_prepare_run($manual_run, FALSE);
- }
- /**
- * Use this before checking and modifying environment variables.
- *
- * @see: elysia_cron_prepare_run()
- */
- function elysia_cron_lock_env($channel = NULL) {
- $execute = TRUE;
- $semaphore_name = elysia_cron_build_semaphore_name($channel);
- if (!lock_acquire($semaphore_name, 240)) {
- elysia_cron_warning('Attempting to re-run cron while it is already running.');
- $execute = FALSE;
- }
- return $execute;
- }
- /**
- * Unlock system locked with elysia_cron_lock_env.
- *
- * @see: elysia_cron_prepare_run().
- */
- function elysia_cron_unlock_env($channel = NULL) {
- $semaphore_name = elysia_cron_build_semaphore_name($channel);
- lock_release($semaphore_name);
- }
- /**
- * Return semaphore name by channel.
- *
- * @param string|null $channel
- * Channel name or NULL if channel not set.
- *
- * @return string
- * Semaphore name trimmed by 255 chars length.
- */
- function elysia_cron_build_semaphore_name($channel) {
- $semaphore_name = 'elysia_cron_semaphore';
- if (isset($channel)) {
- $semaphore_name .= ':' . $channel;
- }
- return drupal_substr($semaphore_name, 0, 255);
- }
- /**
- * Public function to invoke a complete cron_run.
- *
- * @param bool $manual_run
- * Consider launched by a user command
- * (don't check for key/ip, protect current user...).
- * @param bool $ignore_disable
- * Run the channel (and all it's jobs) even if disabled.
- * @param bool $ignore_time
- * Run channel (and all it's jobs) job even if not ready.
- * @param bool $ignore_running
- * Run the channel (and all it's jobs) even if already running.
- */
- function elysia_cron_run($manual_run = FALSE, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
- global $_elysia_cron_exit_phase;
- // If DISABLED block the execution.
- if (!$ignore_disable && variable_get('elysia_cron_disabled', FALSE)) {
- elysia_cron_debug('Cron globally disabled, skipping run');
- return;
- }
- if (!empty($_elysia_cron_exit_phase)) {
- // We on "poormanscron" mode,
- // not necessary to do checks for cron_key or host or permission.
- }
- elseif (!$manual_run) {
- // Check for CRON_KEY or ALLOWED_HOSTS.
- if (!drupal_is_cli()) {
- $cron_key = variable_get('cron_key', '');
- if ($cron_key && (empty($_GET['cron_key']) || $_GET['cron_key'] != $cron_key)) {
- elysia_cron_debug('Cron key mismatch, skipping run');
- return;
- }
- $allowed_hosts = variable_get('elysia_cron_allowed_hosts', FALSE);
- if ($allowed_hosts && !in_array(ip_address(), explode(',', $allowed_hosts))) {
- elysia_cron_debug('Cron ip address mismatch, skipping run');
- return;
- }
- }
- }
- elseif (!elysia_cron_access('execute elysia_cron')) {
- elysia_cron_debug('Access denied for cron launching');
- return;
- }
- elysia_cron_prepare_run($manual_run);
- _ec_variable_set('elysia_cron_last_run', time());
- _ec_variable_set('cron_last', time());
- if ($execute = elysia_cron_lock_env()) {
- elysia_cron_initialize();
- $available_channel = elysia_cron_run_available_channel($ignore_disable, $ignore_time, $ignore_running);
- if ($available_channel) {
- // There are jobs ready to be executed
- // elysia_cron_internal_execute_channel calls elysia_cron_unlock_env.
- elysia_cron_internal_execute_channel($available_channel['name'], $available_channel['jobs'], $ignore_running);
- }
- else {
- // No jobs should be executed, i must unlock cron semaphore.
- elysia_cron_unlock_env();
- elysia_cron_debug('No channels ready to be executed, skipping cron.');
- }
- }
- elysia_cron_unprepare_run($manual_run);
- return $execute;
- }
- /**
- * Menu callback: run cron manually.
- */
- function elysia_cron_run_manually() {
- // Run cron manually
- if (elysia_cron_run(TRUE)) {
- drupal_set_message(t('Cron ran successfully.'));
- }
- else {
- drupal_set_message(t('Cron run failed.'), 'error');
- }
- drupal_goto('admin/reports/status');
- }
- /**
- * Public function to execute all jobs in a channel.
- *
- * @param bool $ignore_disable
- * Run the channel (and all it's jobs) even if disabled.
- * @param bool $ignore_time
- * Run channel (and all it's jobs) job even if not ready.
- * @param bool $ignore_running
- * Run the channel (and all it's jobs) even if already running.
- *
- * @return bool
- * Result of execution.
- */
- function elysia_cron_run_channel($channel, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
- global $_elysia_cron_settings_by_channel;
- // Always $manual_run.
- elysia_cron_prepare_run(TRUE);
- if ($execute = elysia_cron_lock_env($channel)) {
- elysia_cron_initialize();
- $jobs = FALSE;
- if (isset($_elysia_cron_settings_by_channel[$channel])) {
- if ($ignore_disable || empty($_elysia_cron_settings_by_channel[$channel]['#data']['disabled'])) {
- $jobs = elysia_cron_check_run_channel($channel, $ignore_disable, $ignore_time, $ignore_running);
- if ($jobs && count($jobs)) {
- // elysia_cron_internal_execute_channel calls elysia_cron_unlock_env.
- elysia_cron_internal_execute_channel($channel, $jobs, $ignore_running);
- }
- else {
- elysia_cron_debug('Channel already running or no jobs ready to be executed, skipping');
- }
- }
- else {
- elysia_cron_warning('Channel is disabled, skipping');
- }
- }
- else {
- elysia_cron_warning('Channel not found, skipping');
- }
- if (!$jobs) {
- // No jobs should be executed, i must unlock cron semaphore.
- elysia_cron_unlock_env();
- }
- }
- elysia_cron_unprepare_run(TRUE);
- return $execute;
- }
- /**
- * Public function to execute a single job.
- *
- * @param bool $ignore_disable
- * Run the job even if disabled.
- * @param bool $ignore_time
- * Run the job even if not ready.
- * @param bool $ignore_running
- * Run the job even if already running.
- */
- function elysia_cron_run_job($job, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
- global $_cron_completed, $_cron_executing_job, $_elysia_cron_settings;
- elysia_cron_initialize();
- if (isset($_elysia_cron_settings[$job])) {
- if (elysia_cron_should_run($_elysia_cron_settings[$job], -1, $ignore_disable, $ignore_time)) {
- if ($ignore_running || !elysia_cron_check_run_job($job)) {
- $_cron_completed = FALSE;
- $_cron_executing_job = $job;
- // Register shutdown callback.
- register_shutdown_function('elysia_cron_run_job_cleanup');
- elysia_cron_internal_execute_job($job);
- $_cron_completed = TRUE;
- elysia_cron_message('Job executed');
- }
- else {
- elysia_cron_debug('Job is already running, skipping');
- }
- }
- else {
- elysia_cron_debug('Job is disabled or not ready to be executed, skipping');
- }
- }
- else {
- elysia_cron_warning('Job not found, skipping');
- }
- }
- function elysia_cron_run_job_cleanup() {
- global $_cron_completed, $_cron_executing_job;
- if ($_cron_completed) {
- return;
- }
- // See if the semaphore is still locked.
- if (elysia_cron_is_job_running($_cron_executing_job)) {
- elysia_cron_warning('Unexpected termination of cron job %job manually started, aborted.', array('%job' => $_cron_executing_job));
- elysia_cron_set_job_running($_cron_executing_job, 0);
- }
- }
- /**
- * Internal function to execute all jobs in a channel.
- *
- * Function elysia_cron_lock_env() must be called BEFORE calling this method.
- */
- function elysia_cron_internal_execute_channel($channel, $jobs, $ignore_running = FALSE) {
- global $_elysia_cron_current_channel, $_cron_completed, $_cron_completed_time;
- elysia_cron_debug('Cron channel %channel run started.', array('%channel' => $channel));
- $_elysia_cron_current_channel = $channel;
- elysia_cron_set($_elysia_cron_current_channel, TRUE, array(
- 'running' => time(),
- 'last_run' => time(),
- ));
- // Register shutdown callback.
- register_shutdown_function('elysia_cron_internal_execute_channel_cleanup');
- // Now I can unlock cron semaphore.
- elysia_cron_unlock_env();
- foreach ($jobs as $job) {
- if ($ignore_running || !elysia_cron_check_run_job($job)) {
- elysia_cron_internal_execute_job($job);
- }
- }
- $_cron_completed = TRUE;
- $_cron_completed_time = time();
- // Cron is really completed after shutdown functions.
- register_shutdown_function('elysia_cron_internal_execute_channel_completed');
- }
- /**
- * Internal function to execute a single job.
- */
- function elysia_cron_internal_execute_job($job) {
- global $_elysia_cron_settings;
- elysia_cron_debug('Cron job %job started.', array('%job' => $job));
- $time = time();
- elysia_cron_set($job, FALSE, array(
- 'running' => $time,
- 'last_run' => $time,
- ));
- try {
- if (!empty($_elysia_cron_settings[$job]['file'])) {
- $file_path = !empty($_elysia_cron_settings[$job]['file path']) ? $_elysia_cron_settings[$job]['file path'] : drupal_get_path('module', $_elysia_cron_settings[$job]['module']);
- $file_path .= DIRECTORY_SEPARATOR . $_elysia_cron_settings[$job]['file'];
- include_once $file_path;
- }
- if (!empty($_elysia_cron_settings[$job]['expression'])) {
- eval($_elysia_cron_settings[$job]['expression']);
- }
- elseif (!empty($_elysia_cron_settings[$job]['callback']) && is_callable($_elysia_cron_settings[$job]['callback'])) {
- call_user_func_array($_elysia_cron_settings[$job]['callback'], $_elysia_cron_settings[$job]['arguments']);
- }
- else {
- $function = $_elysia_cron_settings[$job]['module'] . '_cronapi';
- $arguments = array('execute', $job);
- if (is_callable($function)) {
- call_user_func_array($function, $arguments);
- }
- else {
- elysia_cron_error('Execution of ' . $job . ' failed, can\'t find function!', array(), TRUE);
- }
- }
- }
- catch (Exception $e) {
- watchdog_exception('cron', $e);
- }
- $stats = elysia_cron_get_job_stats($job);
- $time = time() - $time;
- elysia_cron_set_job_stats(
- $job,
- -1,
- $time,
- ($c = $stats['execution_count'] + 1),
- round((($stats['avg_execution_time'] * ($c - 1)) + $time) / $c, 2),
- $time > $stats['max_execution_time'] ? $time : -1,
- array('running' => 0)
- );
- elysia_cron_debug('Cron job %job ended in %time secs.', array('%job' => $job, '%time' => $time));
- }
- /**
- * Check if the channel is idle (not running, or stuck). If so set returns available jobs.
- *
- * @return array of jobs ready to be executed, or FALSE if channel is running
- */
- function elysia_cron_check_run_channel($channel, $ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
- global $_elysia_cron_settings_by_channel;
- $jobs = FALSE;
- $stuck_time = variable_get('elysia_cron_stuck_time', 3600);
- $sem = elysia_cron_is_channel_running($channel);
- if ($sem && (time() - $sem > $stuck_time)) {
- elysia_cron_set_channel_running($channel, 0);
- $last_job = elysia_cron_execute_aborted($channel);
- unset($sem);
- 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);
- }
- if (($ignore_running || empty($sem)) && ($ignore_disable || !$_elysia_cron_settings_by_channel[$channel]['#data']['disabled'])) {
- $jobs = elysia_cron_active_jobs($channel, $ignore_disable, $ignore_time);
- }
- return $jobs;
- }
- function elysia_cron_check_run_job($job) {
- $job_running = FALSE;
- if (elysia_cron_is_job_running($job)) {
- if (time() - elysia_cron_get_job_last_run($job, 0) > variable_get('elysia_cron_stuck_time', 3600)) {
- elysia_cron_warning('Job %job is already running, but is probably stuck, so i consider it as terminated', array('%job' => $job));
- }
- else {
- elysia_cron_warning('Job %job is already running', array('%job' => $job));
- $job_running = TRUE;
- }
- }
- return $job_running;
- }
- /**
- * Find an idle channel (not running, or stuck). If found one, set it as running and returns available jobs.
- *
- * @return array|bool
- * If found returns array { 'name' => name of channel, 'jobs' => array of active jobs }, else return FALSE.
- */
- function elysia_cron_run_available_channel($ignore_disable = FALSE, $ignore_time = FALSE, $ignore_running = FALSE) {
- global $_elysia_cron_settings_by_channel;
- $channels = array_keys($_elysia_cron_settings_by_channel);
- $channel = elysia_cron_last_channel();
- $i = array_search($channel, $channels);
- if ($i === FALSE) {
- $i = -1;
- }
- $k = 0;
- $jobs = FALSE;
- for ($j = ($i + 1) % count($channels); $k < count($channels); $j = ($j + 1) % count($channels)) {
- $jobs = elysia_cron_check_run_channel($channels[$j], $ignore_disable, $ignore_time, $ignore_running);
- if ($jobs && count($jobs)) {
- break;
- }
- $k++;
- }
- if ($jobs) {
- elysia_cron_set_last_channel($channels[$j]);
- }
- return $jobs ? array('name' => $channels[$j], 'jobs' => $jobs) : FALSE;
- }
- function elysia_cron_execute_aborted($channel) {
- global $_elysia_cron_settings_by_channel;
- $last_job = '';
- foreach ($_elysia_cron_settings_by_channel[$channel] as $job => $conf) {
- if ($job != '#data') {
- if (elysia_cron_is_job_running($job)) {
- $last_job .= ' ' . $job;
- elysia_cron_set_job_running($job, 0);
- }
- }
- }
- // In case the last function to run is longer than 128 chars, we cap the
- // length prior to placing in the database.
- $last_job = trim($last_job);
- if (strlen($last_job) > 128) {
- $last_job = substr($last_job, 0, 128);
- }
- elysia_cron_set($channel, TRUE, array(
- 'running' => 0, //time(),.
- 'last_aborted' => 1,
- 'abort_count' => elysia_cron_get_channel_abort_count($channel) + 1,
- 'last_abort_function' => $last_job,
- ));
- return $last_job;
- }
- /**
- * Shutdown function for cron cleanup.
- *
- * Used for unexpected termination of code.
- */
- function elysia_cron_internal_execute_channel_cleanup() {
- global $_elysia_cron_current_channel, $_cron_completed;
- if ($_cron_completed) {
- return;
- }
- // See if the semaphore is still locked.
- if (elysia_cron_is_channel_running($_elysia_cron_current_channel)) {
- $last_job = elysia_cron_execute_aborted($_elysia_cron_current_channel);
- elysia_cron_warning('Unexpected termination of cron channel %channel, aborted. Last job executed: %job', array('%channel' => $_elysia_cron_current_channel, '%job' => $last_job));
- }
- }
- /**
- * Successful termination (after all shutdown hooks invoked by cron functions).
- */
- function elysia_cron_internal_execute_channel_completed() {
- global $_elysia_cron_current_channel, $_cron_completed_time;
- // Record cron time.
- _ec_variable_set('cron_last', time());
- elysia_cron_debug('Cron channel %channel run completed.', array('%channel' => $_elysia_cron_current_channel));
- $stats = elysia_cron_get_channel_stats($_elysia_cron_current_channel);
- $time = time() - $stats['last_run'];
- $exec_count = $stats['execution_count'] + 1;
- $channel_stats_data = array(
- 'last_execution_time' => $time,
- 'execution_count' => $exec_count,
- 'avg_execution_time' => round((($stats['avg_execution_time'] * ($exec_count - 1)) + $time) / $exec_count, 2),
- 'max_execution_time' => $time > $stats['max_execution_time'] ? $time : $stats['max_execution_time'],
- 'last_shutdown_time' => time() - $_cron_completed_time,
- 'last_aborted' => 0,
- 'running' => 0,
- );
- elysia_cron_set_channel_stats($_elysia_cron_current_channel, $channel_stats_data);
- }
- /**
- * Get all jobs that needs to be executed in a channel
- */
- function elysia_cron_active_jobs($channel, $ignore_disable = FALSE, $ignore_time = FALSE) {
- global $_elysia_cron_settings_by_channel;
- $jobs = array();
- foreach ($_elysia_cron_settings_by_channel[$channel] as $job => $conf) {
- if ($job != '#data') {
- if (elysia_cron_should_run($conf, -1, $ignore_disable, $ignore_time)) {
- $jobs[] = $job;
- }
- }
- }
- return $jobs;
- }
- /**
- * Check if cron is currently running.
- * (Not used by elysia_cron, can be used by external modules)
- */
- function elysia_cron_is_running() {
- global $_elysia_cron_settings_by_channel;
- elysia_cron_initialize();
- $running = array();
- foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
- if (elysia_cron_is_channel_running($channel)) {
- $running[] = $channel;
- }
- }
- return $running;
- }
- function elysia_cron_job_exists($job) {
- global $_elysia_cron_settings;
- return isset($_elysia_cron_settings[$job]);
- }
- function elysia_cron_channel_exists($channel) {
- global $_elysia_cron_settings_by_channel;
- return isset($_elysia_cron_settings_by_channel[$channel]);
- }
- /**
- * Obtain job description (translated)
- */
- function elysia_cron_description($job) {
- global $_elysia_cron_settings;
- if (!empty($_elysia_cron_settings[$job]['description'])) {
- $desc = $_elysia_cron_settings[$job]['description'];
- }
- else {
- $desc = theme('elysia_cron_description', array('job' => $job));
- }
- return t($desc);
- }
- /*******************************************************************************
- * THEMING
- ******************************************************************************/
- /**
- * Implementation of hook_theme().
- */
- function elysia_cron_theme() {
- return array(
- 'elysia_cron_description' => array(
- 'variables' => array('job' => NULL),
- ),
- 'elysia_cron_settings_form' => array(
- 'render element' => 'form',
- ),
- );
- }
- /**
- * You can theme this function to provide your (untranslated) descriptions for cron functions, if they do not provide one.
- */
- function theme_elysia_cron_description($variables) {
- switch ($variables['job']) {
- case 'search_cron':
- return 'Update search database index';
- case 'activitystream_cron':
- return 'Fetch RSS feeds and web calls for activitystream';
- case 'mailhandler_cron':
- return 'Fetch POP3/IMAP accounts managed by MailHandler';
- case 'watchdog_cron':
- return 'Remove expired log messages and flood control events';
- case 'filter_cron':
- return 'Expire outdated filter cache entries';
- case 'node_cron':
- return 'History table cleanup';
- case 'system_cron':
- return 'Remove older rows from flood and batch table. Remove old temporary files.';
- case 'aggregation_cron':
- return 'Fetch RSS feeds for aggregation module';
- case 'amazon_cron':
- return 'Refresh Amazon products';
- case 'image_cron':
- return 'Deletes old temp images';
- case 'persistent_login_cron':
- return 'Expire persistent login';
- case 'trackback_cron':
- return 'Process trackback ping queue';
- case 'update_status_cron':
- return 'Checks for drupal module updates (Note: own frequency check ignore cron rules)';
- case 'user_karma_cron':
- return 'User karma expiration / rebuild (Note: own frequency check ignore cron rules)';
- case 'votingapi_cron':
- return 'Update votes (if not configured for immediate calculation)';
- case 'statistics_cron':
- return 'Reset day counts / Clean expired access logs';
- case 'googleanalytics_cron':
- return 'Delete cached version of ga.js/urchin.js.';
- case 'xmlsitemap_cron':
- return 'XML sitemap ping.';
- case 'xmlsitemap_node_cron':
- return 'Update XML sitemap with new nodes';
- case 'xmlsitemap_term_cron':
- return 'Update XML sitemap with new terms';
- case 'lm_paypal_cron':
- return 'Remove old IPN records';
- case 'user_import_cron':
- return 'Continue partial imports';
- case 'dblog_cron':
- return 'Remove expired log messages and flood control events';
- case 'field_cron':
- return 'Purges some deleted Field API data, if any exists';
- case 'trigger_cron':
- return 'Triggers cron actions';
- case 'update_cron':
- return 'Checks for available updates of Drupal core, contributed modules and themes';
- case 'search_api_cron':
- return 'Will index items for each enabled index.';
- case 'redirect_cron':
- return 'Purge inactive self-managed redirects from the database.';
- case 'ctools_cron':
- return 'Clean up old caches';
- case 'l10n_update_cron':
- return 'Check one project/language at a time, download and import if update available';
- default:
- return '-';
- }
- }
- /*******************************************************************************
- * PING SUPPORT
- ******************************************************************************/
- /**
- * Page callback for ping page. Throws 404 if cron hasn't been called within configured time period.
- */
- function elysia_cron_ping_page() {
- $last_run = _ec_variable_get('elysia_cron_last_run', 0);
- $diff = time() - $last_run;
- $max_interval = variable_get('elysia_cron_alert_interval', 60) * 60;
- if ($diff > $max_interval) {
- drupal_not_found();
- drupal_exit();
- }
- $aoutput = array();
- $aoutput[] = array(
- '#type' => 'markup',
- '#markup' => t('Cron has been called within maximum lapse time.'),
- );
- return $aoutput;
- }
- /*******************************************************************************
- * DRUPAL QUEUE SUPPORT
- * ONLY FOR D7
- ******************************************************************************/
- /**
- * Implements hook_cronapi().
- */
- function elysia_cron_cronapi($op, $job = FALSE) {
- $items = array();
- $queues = module_invoke_all('cron_queue_info');
- drupal_alter('cron_queue_info', $queues);
- foreach ($queues as $queue_name => $info) {
- if (!empty($info['skip on cron'])) {
- // Do not run if queue wants to skip.
- continue;
- }
- // Make sure every queue exists. There is no harm in trying to recreate an
- // existing queue.
- $queue = DrupalQueue::get($queue_name);
- $queue->createQueue();
- // Some queue backends may have performance issues with counting items.
- if (variable_get('elysia_cron_queue_show_count', TRUE)) {
- $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>)';
- }
- else {
- $queue_description = $queue_name . ' queue processing (Worker max duration: <strong>' . (isset($info['time']) ? $info['time'] . 's' : t('unspecified') . ' (15s)') . '</strong>)';
- }
- $items['queue_' . $queue_name] = array(
- 'description' => $queue_description,
- 'rule' => variable_get('elysia_cron_queue_default_rule', FALSE),
- 'weight' => variable_get('elysia_cron_queue_default_weight', 100),
- 'callback' => 'elysia_cron_queue_exec',
- 'arguments' => array($queue_name, $info),
- );
- }
- return $items;
- }
- /**
- * Callback for cronapi.
- *
- * @param string $queue_name
- * Queue name.
- * @param array $info
- * Info about queue.
- */
- function elysia_cron_queue_exec($queue_name, $info) {
- $function = $info['worker callback'];
- $end = time() + (isset($info['time']) ? $info['time'] : 15);
- $queue = DrupalQueue::get($queue_name);
- while (time() < $end && ($item = $queue->claimItem())) {
- try {
- $function($item->data);
- $queue->deleteItem($item);
- }
- catch (Exception $e) {
- // In case of exception log it and leave the item in the queue
- // to be processed again later.
- watchdog_exception('cron', $e);
- }
- }
- }
- /*******************************************************************************
- * ADMIN_MENU SUPPORT
- ******************************************************************************/
- // @todo Deve gestire hook_admin_menu_map, vedi hook_admin_menu.php e admin_menu.map.inc
- /**
- * Implements hook_admin_menu().
- *
- * @param array &$deleted
- * Array of links under admin/* that were removed by
- * admin_menu_adjust_items(). If one of these links is added back,
- * it should be removed from the array.
- *
- * @return array
- * Links array.
- */
- function elysia_cron_admin_menu(&$deleted) {
- $links = array();
- elysia_cron_initialize();
- global $_elysia_cron_settings_by_channel;
- // Add link to manually run cron.
- $links[] = array(
- 'title' => 'Elysia cron manual',
- 'path' => 'admin/config/system/cron',
- 'weight' => 50,
- 'has_children' => FALSE,
- 'parent_path' => '<front>',
- );
- foreach ($_elysia_cron_settings_by_channel as $channel => $data) {
- foreach ($data as $job => $conf) {
- if ($job != '#data') {
- $links[] = array(
- 'title' => "Elysia cron run manual !title",
- 'path' => 'admin/config/system/cron/execute/' . $job,
- 'weight' => -50,
- 'query' => 'destination',
- 'parent_path' => 'admin/config/system/cron',
- 'options' => array('t' => array('!title' => $conf['module'])),
- );
- }
- }
- }
- return $links;
- }
- /**
- * Check if function available to use.
- *
- * Some native php function can be disabled by configuration,
- * to avoid a lot of notices we check availability of such functions.
- *
- * @param string $func
- * Function to check.
- *
- * @return bool
- * TRUE if function available for use, FALSE otherwise.
- */
- function _elysia_cron_function_available($func) {
- $disabled = explode(',', ini_get('disable_functions'));
- return !in_array($func, $disabled);
- }
- /**
- * Get list of columns from elysia_cron table.
- *
- * @return array
- * List of columns.
- */
- function _elysia_cron_columns() {
- return array(
- 'name',
- 'disable',
- 'rule',
- 'weight',
- 'context',
- 'running',
- 'last_run',
- 'last_aborted',
- 'abort_count',
- 'last_abort_function',
- 'last_execution_time',
- 'execution_count',
- 'avg_execution_time',
- 'max_execution_time',
- 'last_shutdown_time',
- );
- }
- /**
- * Get list of predefined cron rules.
- *
- * @return array
- * List of predefined rules.
- */
- function _elysia_cron_default_rules() {
- return array(
- '*/15 * * * *' => 'Every 15 minutes',
- '*/30 * * * *' => 'Every 30 minutes',
- '0 * * * *' => 'Every hour',
- '0 */6 * * *' => 'Every 6 hours',
- '4 0 * * *' => 'Once a day',
- '4 0 * * 0' => 'Once a week',
- '4 0 1 * *' => 'Once a month',
- );
- }
|