Merge branch 'wip-MDL-22183-30' of git://github.com/abgreeve/moodle into MOODLE_30_STABLE

This commit is contained in:
Dan Poltawski
2016-08-22 09:12:54 +01:00
7 changed files with 78 additions and 61 deletions
-1
View File
@@ -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);
+7 -16
View File
@@ -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();
}
}
}
+1 -1
View File
@@ -288,7 +288,7 @@ $tasks = array(
'classname' => 'core\task\stats_cron_task',
'blocking' => 0,
'minute' => '0',
'hour' => '*',
'hour' => '0',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
+41
View File
@@ -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;
}
+12 -38
View File
@@ -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)) {
@@ -1041,13 +1024,10 @@ function stats_get_base_monthly($time=0) {
*/
function stats_get_next_day_start($time) {
$next = stats_get_base_daily($time);
$next = $next + 60*60*26;
$next = stats_get_base_daily($next);
if ($next <= $time) {
//DST trouble - prevent infinite loops
$next = $next + 60*60*24;
}
return $next;
$nextdate = new DateTime();
$nextdate->setTimestamp($next);
$nextdate->add(new DateInterval('P1D'));
return $nextdate->getTimestamp();
}
/**
@@ -1057,13 +1037,10 @@ function stats_get_next_day_start($time) {
*/
function stats_get_next_week_start($time) {
$next = stats_get_base_weekly($time);
$next = $next + 60*60*24*9;
$next = stats_get_base_weekly($next);
if ($next <= $time) {
//DST trouble - prevent infinite loops
$next = $next + 60*60*24*7;
}
return $next;
$nextdate = new DateTime();
$nextdate->setTimestamp($next);
$nextdate->add(new DateInterval('P1W'));
return $nextdate->getTimestamp();
}
/**
@@ -1073,13 +1050,10 @@ function stats_get_next_week_start($time) {
*/
function stats_get_next_month_start($time) {
$next = stats_get_base_monthly($time);
$next = $next + 60*60*24*33;
$next = stats_get_base_monthly($next);
if ($next <= $time) {
//DST trouble - prevent infinite loops
$next = $next + 60*60*24*31;
}
return $next;
$nextdate = new DateTime();
$nextdate->setTimestamp($next);
$nextdate->add(new DateInterval('P1M'));
return $nextdate->getTimestamp();
}
/**
+16 -4
View File
@@ -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;
}
@@ -393,6 +389,22 @@ class core_statslib_testcase extends advanced_testcase {
public function test_statslib_get_next_day_start() {
$this->setTimezone(0);
$this->assertEquals(1272758400, stats_get_next_day_start(1272686410));
// Try setting timezone to some place in the US.
$this->setTimezone('America/New_York', 'America/New_York');
// Then set the time for midnight before daylight savings.
// 1425790800 is midnight in New York (2015-03-08) Daylight saving will occur in 2 hours time.
// 1425873600 is midnight the next day.
$this->assertEquals(1425873600, stats_get_next_day_start(1425790800));
$this->assertEquals(23, ((1425873600 - 1425790800) / 60 ) / 60);
// Then set the time for midnight before daylight savings ends.
// 1446350400 is midnight in New York (2015-11-01) Daylight saving will finish in 2 hours time.
// 1446440400 is midnight the next day.
$this->assertEquals(1446440400, stats_get_next_day_start(1446350400));
$this->assertEquals(25, ((1446440400 - 1446350400) / 60 ) / 60);
// The next day should be normal.
$this->assertEquals(1446526800, stats_get_next_day_start(1446440400));
$this->assertEquals(24, ((1446526800 - 1446440400) / 60 ) / 60);
}
/**
+1 -1
View File
@@ -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.