MDL-86970 core_sms: Resend async SMS when gateway fails

This commit is contained in:
Misha Golenkov
2026-03-12 16:16:14 +11:00
parent 6f7cb5ee55
commit edb7b3236d
3 changed files with 53 additions and 1 deletions
@@ -100,6 +100,7 @@ final class message_output_sms_test extends \advanced_testcase {
$this->assertCount(1, $adhoctask);
// Now lets run the task and check if SMS is sent.
$this->expectOutputRegex('/SMS send status: gateway_sent/');
$this->run_all_adhoc_tasks();
$message = $manager->get_message(['id' => $message->id]);
+14 -1
View File
@@ -18,6 +18,7 @@ namespace core_sms\task;
use core\task\adhoc_task;
use core_sms\message;
use core_sms\message_status;
/**
* Ad-hoc task to send an SMS.
@@ -39,11 +40,23 @@ class send_sms_task extends adhoc_task {
$message = $manager->get_message(
filter: ['id' => $smsdata->messageid],
);
$manager->send_message(
$message = $manager->send_message(
message: $message,
async: false,
);
$status = $message?->status;
$retryfailures = [
message_status::GATEWAY_FAILED,
message_status::GATEWAY_NOT_AVAILABLE,
];
if (in_array($status, $retryfailures)) {
// Only retry if the failure encountered will possibly resolve in subsequent attempts.
mtrace("SMS failed status: {$status?->value} - task will retry");
throw new \moodle_exception('SMS Gateway encountered a failure');
} else {
mtrace("SMS send status: " . ($status?->value ?? message_status::UNKNOWN->value));
}
}
/**
+38
View File
@@ -465,6 +465,7 @@ final class manager_test extends \advanced_testcase {
$this->assertCount(1, $adhoctask);
// Now lets run the task and check if SMS is sent.
$this->expectOutputRegex('/SMS send status: gateway_sent/');
$this->run_all_adhoc_tasks();
$message = $manager->get_message(['id' => $message->id]);
@@ -551,4 +552,41 @@ final class manager_test extends \advanced_testcase {
$this->assertCount(1, $messages);
array_walk($messages, fn ($message) => $this->assertInstanceOf(message::class, $message));
}
/**
* Test async message is resent when gateway is not available.
*/
public function test_resend_when_gateway_not_available(): void {
$this->resetAfterTest();
$manager = \core\di::get(\core_sms\manager::class);
$message = $manager->send(
recipientnumber: '+447123456789',
content: 'Hello, world!',
component: 'core',
messagetype: 'test',
recipientuserid: null,
async: true,
);
$messagedbrecords = $manager->get_messages();
$messages = iterator_to_array($messagedbrecords);
$this->assertCount(1, $messages);
$adhoctask = \core\task\manager::get_adhoc_tasks(send_sms_task::class);
$this->assertCount(1, $adhoctask);
try {
$this->expectOutputRegex('/SMS failed status: gateway_not_available - task will retry/');
$this->run_all_adhoc_tasks();
$this->fail('Exception expected');
} catch (\moodle_exception $e) {
$message = $manager->get_message(['id' => $message->id]);
$this->assertEquals(message_status::GATEWAY_NOT_AVAILABLE, $message->status);
$adhoctask = \core\task\manager::get_adhoc_tasks(send_sms_task::class);
$this->assertCount(1, $adhoctask);
}
}
}