diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 938d8dead0f..5ad20b5bca7 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -893,6 +893,11 @@ $string['explanationdigitalminor'] = 'This information is required to determine
$string['extendperiod'] = 'Extended period';
$string['favourites'] = 'Starred';
$string['failedloginattempts'] = '{$a->attempts} failed logins since your last login';
+$string['failedtaskbody'] = '
Hi {$a->firstname},
+The task {$a->taskname} has failed multiple times and requires attention.
+See task
';
+$string['failedtasksubject'] = 'Task failed: {$a}';
+$string['failedtaskcontexturlname'] = 'Status report';
$string['feedback'] = 'Feedback';
$string['file'] = 'File';
$string['fileexists'] = 'There is already a file called {$a}';
diff --git a/lib/classes/task/failed_task_callbacks.php b/lib/classes/task/failed_task_callbacks.php
new file mode 100644
index 00000000000..482e0aab1b5
--- /dev/null
+++ b/lib/classes/task/failed_task_callbacks.php
@@ -0,0 +1,70 @@
+.
+
+namespace core\task;
+
+use core\hook\task\after_failed_task_max_delay;
+use core_user;
+use stdClass;
+
+/**
+ * Hook listener callbacks for tasks in core
+ *
+ * @package core
+ * @category task
+ * @copyright 2024 Raquel Ortega
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class failed_task_callbacks {
+
+ /**
+ * Callback to send a notification when the max fail delay of a task has been reached.
+ *
+ * @param after_failed_task_max_delay $hook
+ */
+ public static function send_failed_task_max_delay_message(after_failed_task_max_delay $hook): void {
+ $task = $hook->get_task();
+
+ $admins = get_admins();
+ if (empty($admins)) {
+ return;
+ }
+ foreach ($admins as $admin) {
+ $a = new stdClass();
+ $a->firstname = $admin->firstname;
+ $a->taskname = $task->get_name();
+ $a->link = new \moodle_url('/report/status/index.php', ['detail' => 'tool_task_maxfaildelay']);
+ $messagetxt = get_string('failedtaskbody', 'moodle', $a);
+ // Create message.
+ $message = new \core\message\message();
+ $message->component = 'moodle';
+ $message->name = 'failedtaskmaxdelay';
+ $message->userfrom = core_user::get_noreply_user();
+ $message->userto = $admin;
+ $message->subject = get_string('failedtasksubject', 'moodle', $task->get_name());
+ $message->fullmessage = html_to_text($messagetxt);
+ $message->fullmessageformat = FORMAT_MARKDOWN;
+ $message->fullmessagehtml = text_to_html($messagetxt);
+ $message->smallmessage = get_string('failedtasksubject', 'moodle', $task->get_name());
+ $message->notification = 1;
+ $message->contexturl = (
+ new \moodle_url('/report/status/index.php', ['detail' => 'tool_task_maxfaildelay']))->out(false);
+ $message->contexturlname = get_string('failedtaskcontexturlname', 'moodle');
+ // Actually send the message.
+ message_send($message);
+ }
+ }
+}
diff --git a/lib/db/hooks.php b/lib/db/hooks.php
index 1e2dc77d6bc..8869b1ca756 100644
--- a/lib/db/hooks.php
+++ b/lib/db/hooks.php
@@ -93,4 +93,8 @@ $callbacks = [
'hook' => \core_enrol\hook\before_user_enrolment_remove::class,
'callback' => \core_communication\hook_listener::class . '::remove_communication_membership_for_unenrolled_user',
],
+ [
+ 'hook' => \core\hook\task\after_failed_task_max_delay::class,
+ 'callback' => core\task\failed_task_callbacks::class . '::send_failed_task_max_delay_message',
+ ],
];