Skip to content

Commit

Permalink
MDL-49070 core_registration: improved site registration scheduled task
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Mar 20, 2015
1 parent a149d6a commit f47e4eb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
46 changes: 22 additions & 24 deletions admin/registration/lib.php
Expand Up @@ -53,32 +53,30 @@ class registration_manager {
public function cron() {
global $CFG;
if (extension_loaded('xmlrpc')) {
//check if the last registration cron update was less than a week ago
$lastcron = get_config('registration', 'crontime');
if ($lastcron === false or $lastcron < strtotime("-7 day")) { //set to a week, see MDL-23704
$function = 'hub_update_site_info';
require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
$function = 'hub_update_site_info';
require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");

//update all hub where the site is registered on
$hubs = $this->get_registered_on_hubs();
foreach ($hubs as $hub) {
//update the registration
$siteinfo = $this->get_site_info($hub->huburl);
$params = array('siteinfo' => $siteinfo);
$serverurl = $hub->huburl . "/local/hub/webservice/webservices.php";
$xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
try {
$result = $xmlrpcclient->call($function, $params);
$this->update_registeredhub($hub); // To update timemodified.
mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname));
} catch (Exception $e) {
$errorparam = new stdClass();
$errorparam->errormessage = $e->getMessage();
$errorparam->hubname = $hub->hubname;
mtrace(get_string('errorcron', 'hub', $errorparam));
}
// Update all hubs where the site is registered.
$hubs = $this->get_registered_on_hubs();
if (empty($hubs)) {
mtrace(get_string('registrationwarning', 'admin'));
}
foreach ($hubs as $hub) {
// Update the registration.
$siteinfo = $this->get_site_info($hub->huburl);
$params = array('siteinfo' => $siteinfo);
$serverurl = $hub->huburl . "/local/hub/webservice/webservices.php";
$xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
try {
$result = $xmlrpcclient->call($function, $params);
$this->update_registeredhub($hub); // To update timemodified.
mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname));
} catch (Exception $e) {
$errorparam = new stdClass();
$errorparam->errormessage = $e->getMessage();
$errorparam->hubname = $hub->hubname;
mtrace(get_string('errorcron', 'hub', $errorparam));
}
set_config('crontime', time(), 'registration');
}
} else {
mtrace(get_string('errorcronnoxmlrpc', 'hub'));
Expand Down
9 changes: 9 additions & 0 deletions lib/classes/task/scheduled_task.php
Expand Up @@ -35,11 +35,17 @@ abstract class scheduled_task extends task_base {
const MINUTEMIN = 0;
/** Maximum minute value. */
const MINUTEMAX = 59;

/** Minimum hour value. */
const HOURMIN = 0;
/** Maximum hour value. */
const HOURMAX = 23;

/** Minimum dayofweek value. */
const DAYOFWEEKMIN = 0;
/** Maximum dayofweek value. */
const DAYOFWEEKMAX = 6;

/** @var string $hour - Pattern to work out the valid hours */
private $hour = '*';

Expand Down Expand Up @@ -173,6 +179,9 @@ public function get_day() {
* @param string $dayofweek
*/
public function set_day_of_week($dayofweek) {
if ($dayofweek === 'R') {
$dayofweek = mt_rand(self::DAYOFWEEKMIN, self::DAYOFWEEKMAX);
}
$this->dayofweek = $dayofweek;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/db/tasks.php
Expand Up @@ -215,10 +215,10 @@
array(
'classname' => 'core\task\registration_cron_task',
'blocking' => 0,
'minute' => '0',
'hour' => '3',
'minute' => 'R',
'hour' => 'R',
'day' => '*',
'dayofweek' => '*',
'dayofweek' => 'R',
'month' => '*'
),
array(
Expand Down
7 changes: 7 additions & 0 deletions lib/tests/scheduled_task_test.php
Expand Up @@ -329,6 +329,7 @@ public function test_random_time_specification() {
// Set a random value.
$testclass->set_minute('R');
$testclass->set_hour('R');
$testclass->set_day_of_week('R');

// Verify the minute has changed within allowed bounds.
$minute = $testclass->get_minute();
Expand All @@ -341,6 +342,12 @@ public function test_random_time_specification() {
$this->assertInternalType('int', $hour);
$this->assertGreaterThanOrEqual(0, $hour);
$this->assertLessThanOrEqual(23, $hour);

// Verify the dayofweek has changed within allowed bounds.
$dayofweek = $testclass->get_day_of_week();
$this->assertInternalType('int', $dayofweek);
$this->assertGreaterThanOrEqual(0, $dayofweek);
$this->assertLessThanOrEqual(6, $dayofweek);
}

/**
Expand Down

0 comments on commit f47e4eb

Please sign in to comment.