diff --git a/lib/statslib.php b/lib/statslib.php index 42d08a2e7ea..61547f0e29e 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -86,6 +86,22 @@ function stats_cron_daily($maxdays=1) { set_config('statslastdaily', $timestart); } + // calculate scheduled time + $scheduledtime = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60; + + // Note: This will work fine for sites running cron each 4 hours or less (hoppefully, 99.99% of sites). MDL-16709 + // check to make sure we're due to run, at least 20 hours after last run + if (isset($CFG->statslastexecution) and ((time() - 20*60*60) < $CFG->statslastexecution)) { + mtrace("...preventing stats to run, last execution was less than 20 hours ago."); + return false; + // also check that we are a max of 4 hours after scheduled time, stats won't run after that + } else if (time() > $scheduledtime + 4*60*60) { + mtrace("...preventing stats to run, more than 4 hours since scheduled time."); + return false; + } else { + set_config('statslastexecution', time()); /// Grab this execution as last one + } + $nextmidnight = stats_get_next_day_start($timestart); // are there any days that need to be processed? @@ -93,6 +109,7 @@ function stats_cron_daily($maxdays=1) { return true; // everything ok and up-to-date } + $timeout = empty($CFG->statsmaxruntime) ? 60*60*24 : $CFG->statsmaxruntime; if (!set_cron_lock('statsrunning', $now + $timeout)) {