Skip to content

Commit

Permalink
MDL-50287 completion: Separated slow task out from cron
Browse files Browse the repository at this point in the history
The mark started function for the completions scheduled task can be very
slow and causes performance issues when running. By splitting this into
it's own task it can be managed independantly from the other parts which
update regularly.
  • Loading branch information
joshwillcock authored and danpoltawski committed Aug 27, 2015
1 parent 6d392b3 commit 51e488e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
24 changes: 21 additions & 3 deletions completion/cron.php
Expand Up @@ -28,20 +28,38 @@
require_once($CFG->libdir.'/completionlib.php');

/**
* Legacy - here for plugin use only, not used in scheduled tasks.
* Update user's course completion statuses
*
* First update all criteria completions, then aggregate all criteria completions
* and update overall course completions
* and update overall course completions.
*/
function completion_cron() {

completion_cron_mark_started();

completion_cron_criteria();

completion_cron_completions();
}

/**
* Update user's course completion statuses
*
* First update all criteria completions, then aggregate all criteria completions
* and update overall course completions
*/
function completion_regular_cron() {
// Two Functions which check criteria and learner completions regularly for accurate data.
completion_cron_criteria();
completion_cron_completions();
}
/**
* Marks users as started if the config option is set.
*
* One function which takes a long time 60+ minutes to enrol users onto completion started.
*/
function completion_daily_cron() {
completion_cron_mark_started();
}
/**
* Mark users as started if the config option is set
*
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Expand Up @@ -1020,6 +1020,8 @@
$string['taskcalendarcron'] = 'Send calendar notifications';
$string['taskcheckforupdates'] = 'Check for updates';
$string['taskcompletioncron'] = 'Calculate completion data';
$string['taskcompletioncronregular'] = 'Calculate regular completion data';
$string['taskcompletioncrondaily'] = 'Completion mark as started';
$string['taskcontextcleanup'] = 'Cleanup contexts';
$string['taskcreatecontexts'] = 'Create missing contexts';
$string['taskdeletecachetext'] = 'Delete old text cache records';
Expand Down
Expand Up @@ -24,17 +24,19 @@
namespace core\task;

/**
* Simple task to run the completion cron.
* Simple task to run the daily completion cron.
* @copyright 2013 onwards Martin Dougiamas http://dougiamas.com.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class completion_cron_task extends scheduled_task {
class completion_cron_daily_task extends scheduled_task {

/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskcompletioncron', 'admin');
return get_string('taskcompletioncrondaily', 'admin');
}

/**
Expand All @@ -45,9 +47,9 @@ public function execute() {
global $CFG;

if ($CFG->enablecompletion) {
// Completion cron.
// Daily Completion cron.
require_once($CFG->dirroot.'/completion/cron.php');
completion_cron();
completion_daily_cron();
}
}

Expand Down
56 changes: 56 additions & 0 deletions lib/classes/task/completion_cron_regular_task.php
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* A scheduled task.
*
* @package core
* @copyright 2013 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\task;

/**
* Simple task to run the regular completion cron.
* @copyright 2013 onwards Martin Dougiamas http://dougiamas.com.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class completion_cron_regular_task extends scheduled_task {

/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskcompletioncronregular', 'admin');
}

/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $CFG;

if ($CFG->enablecompletion) {
// Regular Completion cron.
require_once($CFG->dirroot.'/completion/cron.php');
completion_regular_cron();
}
}

}
11 changes: 10 additions & 1 deletion lib/db/tasks.php
Expand Up @@ -159,14 +159,23 @@
'month' => '*'
),
array(
'classname' => 'core\task\completion_cron_task',
'classname' => 'core\task\completion_cron_regular_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'core\task\completion_cron_daily_task',
'blocking' => 0,
'minute' => 'R',
'hour' => 'R',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
),
array(
'classname' => 'core\task\portfolio_cron_task',
'blocking' => 0,
Expand Down

0 comments on commit 51e488e

Please sign in to comment.