From 496be783b3700c3fb66527e4a2c66ca24acba059 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Thu, 4 Aug 2016 15:38:12 +0800 Subject: [PATCH] MDL-22183 lib: Time settings removed for stats cron. Old settings that should be ignored have been removed from the statistics cron code. --- admin/settings/server.php | 1 - lib/classes/task/stats_cron_task.php | 23 +++++----------- lib/db/tasks.php | 2 +- lib/db/upgrade.php | 41 ++++++++++++++++++++++++++++ lib/statslib.php | 17 ------------ lib/tests/statslib_test.php | 4 --- version.php | 2 +- 7 files changed, 50 insertions(+), 40 deletions(-) diff --git a/admin/settings/server.php b/admin/settings/server.php index 573ed4b196f..d6d43479fd5 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -78,7 +78,6 @@ $temp->add(new admin_setting_configselect('statsmaxruntime', new lang_string('st 60*60*7 => '7 '.new lang_string('hours'), 60*60*8 => '8 '.new lang_string('hours') ))); $temp->add(new admin_setting_configtext('statsruntimedays', new lang_string('statsruntimedays', 'admin'), new lang_string('configstatsruntimedays', 'admin'), 31, PARAM_INT)); -$temp->add(new admin_setting_configtime('statsruntimestarthour', 'statsruntimestartminute', new lang_string('statsruntimestart', 'admin'), new lang_string('configstatsruntimestart', 'admin'), array('h' => 0, 'm' => 0))); $temp->add(new admin_setting_configtext('statsuserthreshold', new lang_string('statsuserthreshold', 'admin'), new lang_string('configstatsuserthreshold', 'admin'), 0, PARAM_INT)); $ADMIN->add('server', $temp); diff --git a/lib/classes/task/stats_cron_task.php b/lib/classes/task/stats_cron_task.php index 5ce0791ba79..f9f54652375 100644 --- a/lib/classes/task/stats_cron_task.php +++ b/lib/classes/task/stats_cron_task.php @@ -44,28 +44,19 @@ class stats_cron_task extends scheduled_task { public function execute() { global $CFG; - $timenow = time(); // Run stats as at the end because they are known to take very long time on large sites. if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) { require_once($CFG->dirroot.'/lib/statslib.php'); - // Check we're not before our runtime. - $timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour * 60 * 60 + $CFG->statsruntimestartminute * 60; - - if ($timenow > $timetocheck) { - // Process configured number of days as max (defaulting to 31). - $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays); - if (stats_cron_daily($maxdays)) { - if (stats_cron_weekly()) { - if (stats_cron_monthly()) { - stats_clean_old(); - } + // Process configured number of days as max (defaulting to 31). + $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays); + if (stats_cron_daily($maxdays)) { + if (stats_cron_weekly()) { + if (stats_cron_monthly()) { + stats_clean_old(); } } - \core_php_time_limit::raise(); - } else { - mtrace('Next stats run after:'. userdate($timetocheck)); } + \core_php_time_limit::raise(); } } - } diff --git a/lib/db/tasks.php b/lib/db/tasks.php index 9723d334beb..6c04e8d2438 100644 --- a/lib/db/tasks.php +++ b/lib/db/tasks.php @@ -288,7 +288,7 @@ $tasks = array( 'classname' => 'core\task\stats_cron_task', 'blocking' => 0, 'minute' => '0', - 'hour' => '*', + 'hour' => '0', 'day' => '*', 'dayofweek' => '*', 'month' => '*' diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index b6d19603349..64fe2567f95 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4659,5 +4659,46 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2015111604.07); } + if ($oldversion < 2015111605.07) { + // Default schedule values. + $hour = 0; + $minute = 0; + + // Get the old settings. + if (isset($CFG->statsruntimestarthour)) { + $hour = $CFG->statsruntimestarthour; + } + if (isset($CFG->statsruntimestartminute)) { + $minute = $CFG->statsruntimestartminute; + } + + // Retrieve the scheduled task record first. + $stattask = $DB->get_record('task_scheduled', array('component' => 'moodle', 'classname' => '\core\task\stats_cron_task')); + + // Don't touch customised scheduling. + if ($stattask && !$stattask->customised) { + + $nextruntime = mktime($hour, $minute, 0, date('m'), date('d'), date('Y')); + if ($nextruntime < $stattask->lastruntime) { + // Add 24 hours to the next run time. + $newtime = new DateTime(); + $newtime->setTimestamp($nextruntime); + $newtime->add(new DateInterval('P1D')); + $nextruntime = $newtime->getTimestamp(); + } + $stattask->nextruntime = $nextruntime; + $stattask->minute = $minute; + $stattask->hour = $hour; + $stattask->customised = 1; + $DB->update_record('task_scheduled', $stattask); + } + // These settings are no longer used. + unset_config('statsruntimestarthour'); + unset_config('statsruntimestartminute'); + unset_config('statslastexecution'); + + upgrade_main_savepoint(true, 2015111605.07); + } + return true; } diff --git a/lib/statslib.php b/lib/statslib.php index 23fa5555775..043b54cb659 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -138,22 +138,6 @@ 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 (hopefully, 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) && ((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? @@ -161,7 +145,6 @@ 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)) { diff --git a/lib/tests/statslib_test.php b/lib/tests/statslib_test.php index 2e295096815..0819877056b 100644 --- a/lib/tests/statslib_test.php +++ b/lib/tests/statslib_test.php @@ -61,7 +61,6 @@ class core_statslib_testcase extends advanced_testcase { core_date::set_default_server_timezone(); $CFG->statsfirstrun = 'all'; $CFG->statslastdaily = 0; - $CFG->statslastexecution = 0; // Figure out the broken day start so I can figure out when to the start time should be. $time = time(); @@ -74,9 +73,6 @@ class core_statslib_testcase extends advanced_testcase { $shour = intval(($time - $stime) / (60*60)); - $CFG->statsruntimestarthour = $shour; - $CFG->statsruntimestartminute = 0; - if ($DB->record_exists('user', array('username' => 'user1'))) { return; } diff --git a/version.php b/version.php index 854a9b61d79..b2c00e10e08 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015111605.06; // 20151116 = branching date YYYYMMDD - do not modify! +$version = 2015111605.07; // 20151116 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes.