From 790bb5a31bcd8a166ec1f466e2d42b6e8f4786b5 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 13 Oct 2008 23:24:45 +0000 Subject: [PATCH] MDL-16709. Improve scheduling of stats. They only will run if: - 20 hours has passes since last execution. - We are in a 4 hours time window since last scheduled time. Should work for 99.99% os sites (running cron in < 4h intervals) Merged from 19_STABLE --- lib/statslib.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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)) {