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.
This commit is contained in:
Michael Hawkins 2019-10-07 14:00:22 +08:00
parent 7d8f604b3c
commit 3c36cdb150
2 changed files with 21 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -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();
}
return true;
}