From 3c36cdb1501834e56e2ae216b390c8f0cff78eb1 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Mon, 7 Oct 2019 14:00:22 +0800 Subject: [PATCH] MDL-66694 mod_forum: Updated adhoc task to requeue until completion The adhoc task run during upgrade will now be requeued until no more word/char counts require updating. Previously it was only run once, so only the first 5000 posts would be updated. --- .../classes/task/refresh_forum_post_counts.php | 17 +++++++++++++++-- mod/forum/lib.php | 9 ++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/mod/forum/classes/task/refresh_forum_post_counts.php b/mod/forum/classes/task/refresh_forum_post_counts.php index 012b751c900..afc5213f3f2 100644 --- a/mod/forum/classes/task/refresh_forum_post_counts.php +++ b/mod/forum/classes/task/refresh_forum_post_counts.php @@ -39,10 +39,23 @@ class refresh_forum_post_counts extends \core\task\adhoc_task { * Run the task to refresh calendar events. */ public function execute() { - global $CFG; + global $CFG, $DB; require_once($CFG->dirroot . '/mod/forum/lib.php'); - mod_forum_update_null_forum_post_counts(5000); + $recordsfound = mod_forum_update_null_forum_post_counts(5000); + + // Re-queue this adhoc task if records were found during the current run, + // since there may be more records to update. + if ($recordsfound) { + $record = new \stdClass(); + $record->classname = '\mod_forum\task\refresh_forum_post_counts'; + $record->component = 'mod_forum'; + + // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task(). + $nextruntime = time() - 1; + $record->nextruntime = $nextruntime; + $DB->insert_record('task_adhoc', $record); + } } } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 7e5d423cf27..2216f29ea99 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -6732,7 +6732,7 @@ function mod_forum_user_preferences() { * Updates null forum post counts according to the post message. * * @param int $limit The number of records to update - * @return null + * @return bool Whether any records were found and updated */ function mod_forum_update_null_forum_post_counts(int $limit) { global $DB; @@ -6741,12 +6741,15 @@ function mod_forum_update_null_forum_post_counts(int $limit) { $recordset = $DB->get_recordset_select('forum_posts', $select, null, 'discussion', 'id, message', 0, $limit); if (!$recordset->valid()) { $recordset->close(); - return; + return false; } foreach ($recordset as $record) { $countsupdate = \mod_forum\local\entities\post::add_message_counts($record); $DB->update_record('forum_posts', $countsupdate); } + $recordset->close(); -} \ No newline at end of file + + return true; +}