From 51e488eaf31307319e34c8b56a85c019016c7871 Mon Sep 17 00:00:00 2001 From: Josh Willcock Date: Wed, 20 May 2015 13:08:19 +0100 Subject: [PATCH] MDL-50287 completion: Separated slow task out from cron 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. --- completion/cron.php | 24 +++++++- lang/en/admin.php | 2 + ...ask.php => completion_cron_daily_task.php} | 12 ++-- .../task/completion_cron_regular_task.php | 56 +++++++++++++++++++ lib/db/tasks.php | 11 +++- 5 files changed, 96 insertions(+), 9 deletions(-) rename lib/classes/task/{completion_cron_task.php => completion_cron_daily_task.php} (77%) create mode 100644 lib/classes/task/completion_cron_regular_task.php diff --git a/completion/cron.php b/completion/cron.php index 23e682c84c695..c252de4ed3a1c 100644 --- a/completion/cron.php +++ b/completion/cron.php @@ -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 * diff --git a/lang/en/admin.php b/lang/en/admin.php index 7e7f4c71476c1..de8b022f37fd2 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -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'; diff --git a/lib/classes/task/completion_cron_task.php b/lib/classes/task/completion_cron_daily_task.php similarity index 77% rename from lib/classes/task/completion_cron_task.php rename to lib/classes/task/completion_cron_daily_task.php index b34860a801484..ace3a5bbada78 100644 --- a/lib/classes/task/completion_cron_task.php +++ b/lib/classes/task/completion_cron_daily_task.php @@ -24,9 +24,11 @@ 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). @@ -34,7 +36,7 @@ class completion_cron_task extends scheduled_task { * @return string */ public function get_name() { - return get_string('taskcompletioncron', 'admin'); + return get_string('taskcompletioncrondaily', 'admin'); } /** @@ -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(); } } diff --git a/lib/classes/task/completion_cron_regular_task.php b/lib/classes/task/completion_cron_regular_task.php new file mode 100644 index 0000000000000..29a0c52744250 --- /dev/null +++ b/lib/classes/task/completion_cron_regular_task.php @@ -0,0 +1,56 @@ +. + +/** + * 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(); + } + } + +} diff --git a/lib/db/tasks.php b/lib/db/tasks.php index 6c4d7d203daf4..9954d5926a8d1 100644 --- a/lib/db/tasks.php +++ b/lib/db/tasks.php @@ -159,7 +159,7 @@ 'month' => '*' ), array( - 'classname' => 'core\task\completion_cron_task', + 'classname' => 'core\task\completion_cron_regular_task', 'blocking' => 0, 'minute' => '*', 'hour' => '*', @@ -167,6 +167,15 @@ '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,