Merge branch 'MDL-77593_improve-performance-of-adding-members-to-communication-room' of https://github.com/ziegenberg/moodle

This commit is contained in:
Huong Nguyen
2025-08-29 08:36:04 +07:00
3 changed files with 36 additions and 28 deletions
@@ -32,14 +32,14 @@ class add_members_to_room_task extends adhoc_task {
$data = $this->get_custom_data();
// Call the communication api to action the operation.
$communication = processor::load_by_id($data->id);
$communication = processor::load_by_id($data->commid);
if ($communication === null) {
mtrace("Skipping room creation because the instance does not exist");
mtrace("Skipping adding members to room because the instance does not exist");
return;
}
$communication->get_room_user_provider()->add_members_to_room($communication->get_instance_userids());
$communication->get_room_user_provider()->add_members_to_room($data->userids);
}
/**
@@ -51,13 +51,16 @@ class add_members_to_room_task extends adhoc_task {
processor $communication
): void {
// Add ad-hoc task to update the provider room.
$task = new self();
$task->set_custom_data([
'id' => $communication->get_id(),
]);
foreach (array_chunk($communication->get_instance_userids(), 30) as $userids) {
// Add ad-hoc task to update the provider room.
$task = new self();
$task->set_custom_data([
'commid' => $communication->get_id(),
'userids' => $userids,
]);
// Queue the task for the next run.
\core\task\manager::queue_adhoc_task($task);
// Queue the task for the next run.
\core\task\manager::queue_adhoc_task($task);
}
}
}
@@ -32,21 +32,21 @@ class remove_members_from_room extends adhoc_task {
$data = $this->get_custom_data();
// Call the communication api to action the operation.
$communication = processor::load_by_id($data->id);
$communication = processor::load_by_id($data->commid);
if ($communication === null) {
mtrace("Skipping room creation because the instance does not exist");
mtrace("Skipping removing members from room because the instance does not exist");
return;
}
$communication->get_room_user_provider()->remove_members_from_room($communication->get_all_delete_flagged_userids());
$communication->get_room_user_provider()->remove_members_from_room($data->userids);
// Now remove any mapping for users who are not in the room.
$communication->delete_instance_non_synced_user_mapping($communication->get_instance_userids(false, true));
}
/**
* Queue the task for the next run.
* Queue the tasks for the next run.
*
* @param processor $communication The communication processor to perform the action on
*/
@@ -54,13 +54,16 @@ class remove_members_from_room extends adhoc_task {
processor $communication
): void {
// Add ad-hoc task to update the provider room.
$task = new self();
$task->set_custom_data([
'id' => $communication->get_id(),
]);
foreach (array_chunk($communication->get_all_delete_flagged_userids(), 30) as $userids) {
// Add ad-hoc task to update the provider room.
$task = new self();
$task->set_custom_data([
'commid' => $communication->get_id(),
'userids' => $userids,
]);
// Queue the task for the next run.
\core\task\manager::queue_adhoc_task($task);
// Queue the task for the next run.
\core\task\manager::queue_adhoc_task($task);
}
}
}
+9 -7
View File
@@ -285,8 +285,10 @@ final class api_test extends \advanced_testcase {
*/
public function test_adding_and_removing_of_room_membership(): void {
$course = $this->get_course();
$userid = $this->get_user()->id;
$userids = [];
for ($i = 0; $i < 40; $i++) {
$userids[] = $this->get_user('Samplefn' . $i, 'Sampleln' . $i, 'sampleun' . $i)->id;
}
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
context: \core\context\course::instance($course->id),
@@ -294,18 +296,18 @@ final class api_test extends \advanced_testcase {
instancetype: 'coursecommunication',
instanceid: $course->id,
);
$communication->add_members_to_room([$userid]);
$communication->add_members_to_room($userids);
// Test the tasks added.
$adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task');
$this->assertCount(1, $adhoctask);
$this->assertCount(2, $adhoctask);
// Now test the removing members from a room.
$communication->remove_members_from_room([$userid]);
$communication->remove_members_from_room($userids);
// Test the tasks added.
$adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room');
$this->assertCount(1, $adhoctask);
$this->assertCount(2, $adhoctask);
}
/**
@@ -441,7 +443,7 @@ final class api_test extends \advanced_testcase {
users: [$userid],
);
// Test that no communicaiton task is added.
// Test that no communication task is added.
$adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
$this->assertCount(0, $adhoctask);