Skip to content

Commit

Permalink
MDL-66694 mod_forum: Updated adhoc task to requeue until completion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mickhawkins committed Oct 17, 2019
1 parent 7d8f604 commit 3c36cdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 15 additions & 2 deletions mod/forum/classes/task/refresh_forum_post_counts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
9 changes: 6 additions & 3 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

0 comments on commit 3c36cdb

Please sign in to comment.