Merge branch 'MDL-79131-main' of https://github.com/raortegar/moodle

This commit is contained in:
Huong Nguyen
2024-04-04 10:31:46 +08:00
committed by Andrew Nicols
10 changed files with 297 additions and 3 deletions
+6
View File
@@ -887,6 +887,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'] = '<p>Hi {$a->firstname},</p>
<p>The task <b>{$a->taskname}</b> has failed multiple times and requires attention.</p>
<p><a href="{$a->link}">See task</a></p>';
$string['failedtasksubject'] = 'Task failed: {$a}';
$string['failedtaskcontexturlname'] = 'Status report';
$string['feedback'] = 'Feedback';
$string['file'] = 'File';
$string['fileexists'] = 'There is already a file called {$a}';
@@ -1314,6 +1319,7 @@ $string['messageprovider:insights'] = 'Insights generated by prediction models';
$string['messageprovider:instantmessage'] = 'Personal messages between users';
$string['messageprovider:instantmessage_help'] = 'This section configures what happens to messages that are sent to you directly from other users on this site.';
$string['messageprovider:reportbuilderschedule'] = 'Custom report builder schedules';
$string['messageprovider:failedtaskmaxdelay'] = 'Task reaches its maximum fail delay';
$string['messageselect'] = 'Select this user as a message recipient';
$string['messageselectadd'] = 'Send a message';
$string['middlename'] = 'Middle name';
@@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\hook\task;
use core\hook\described_hook;
use core\task\task_base;
/**
* Hook to allow plugins to get information when a task has reached the maximum fail delay in adhoc and scheduled task
*
* @package core
* @copyright 2024 Raquel Ortega <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\tags('task')]
#[\core\attribute\label('Allow plugins to get information when a task reaches its maximum failure delay.')]
final class after_failed_task_max_delay {
/**
* Constructor.
* @param task_base $task failed task.
*/
public function __construct(
protected task_base $task,
) {
}
/**
* Get the task object.
*
* @return task_base Task object.
*/
public function get_task(): task_base {
return $this->task;
}
}
@@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <[email protected]>
* @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);
}
}
}
+14 -2
View File
@@ -1175,8 +1175,14 @@ class manager {
}
// Max of 24 hour delay.
if ($delay > 86400) {
if ($delay >= 86400) {
$delay = 86400;
// Dispatch hook when max fail delay has reached.
$hook = new \core\hook\task\after_failed_task_max_delay(
task: $task,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
}
// Reschedule and then release the locks.
@@ -1281,8 +1287,14 @@ class manager {
}
// Max of 24 hour delay.
if ($delay > 86400) {
if ($delay >= 86400) {
$delay = 86400;
// Dispatch hook when max fail delay has reached.
$hook = new \core\hook\task\after_failed_task_max_delay(
task: $task,
);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
}
$task->set_timestarted();
+4
View File
@@ -102,4 +102,8 @@ $callbacks = [
'callback' => \core_message\hook_callbacks::class . '::add_messaging_widget',
'priority' => 0,
],
[
'hook' => \core\hook\task\after_failed_task_max_delay::class,
'callback' => core\task\failed_task_callbacks::class . '::send_failed_task_max_delay_message',
],
];
+8
View File
@@ -216,4 +216,12 @@ $messageproviders = array (
'email' => MESSAGE_FORCED,
],
],
// Task has reached the maximum fail delay.
'failedtaskmaxdelay' => [
'defaults' => [
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
],
'capability' => 'moodle/site:config',
],
);
+73
View File
@@ -917,4 +917,77 @@ final class adhoc_task_test extends \advanced_testcase {
$output
);
}
/**
* Test send messages when adhoc task reaches the max fail delay time.
*
* @covers ::adhoc_task_failed
* @covers ::send_failed_task_max_delay_message
*/
public function test_adhoc_message_max_fail_delay(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Redirect messages.
$messagesink = $this->redirectMessages();
// Create an adhoc task.
$task = new adhoc_test_task();
manager::queue_adhoc_task($task);
$now = time();
// Get it from the scheduler, execute it, and mark it as failed.
$task = manager::get_next_adhoc_task($now);
$taskid = $task->get_id();
$task->execute();
// Catch the message. The task has not reach the max time delay yet.
manager::adhoc_task_failed($task);
$messages = $messagesink->get_messages();
$this->assertCount(0, $messages);
// Should get the adhoc task immediately.
$task = manager::get_adhoc_task($taskid);
$task->set_fail_delay(86400);
$this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
$this->assertEquals($taskid, $task->get_id());
$task->execute();
// Catch the message.
manager::adhoc_task_failed($task);
$messages = $messagesink->get_messages();
$this->assertCount(1, $messages);
// Get the task and execute it second time.
$task = manager::get_adhoc_task($taskid);
// Set the fail delay to 12 hours.
$task->set_fail_delay(43200);
$task->execute();
manager::adhoc_task_failed($task);
// Catch the message.
$messages = $messagesink->get_messages();
$this->assertCount(2, $messages);
// Get the task and execute it third time.
$task = manager::get_adhoc_task($taskid);
// Set the fail delay to 48 hours.
$task->set_fail_delay(172800);
$task->execute();
manager::adhoc_task_failed($task);
// Catch the message.
$messages = $messagesink->get_messages();
$this->assertCount(3, $messages);
// Check first message information.
$this->assertStringContainsString('Task failed: Test adhoc class', $messages[0]->subject);
$this->assertEquals('failedtaskmaxdelay', $messages[0]->eventtype);
$this->assertEquals('-10', $messages[0]->useridfrom);
$this->assertEquals('2', $messages[0]->useridto);
// Close sink.
$messagesink->close();
}
}
+69
View File
@@ -1035,4 +1035,73 @@ class scheduled_task_test extends \advanced_testcase {
$this->assertEquals(1, $task->get_hour());
$this->assertEquals(false, $task->has_default_configuration());
}
/**
* Test send messages when a task reaches the max fail delay time.
*
* @covers ::scheduled_task_failed
* @covers ::send_failed_task_max_delay_message
*/
public function test_message_max_fail_delay(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Redirect messages.
$messagesink = $this->redirectMessages();
$cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
// Get an example task to use for testing. Task is set to run every minute by default.
$taskname = '\core\task\send_new_user_passwords_task';
$task = manager::get_scheduled_task($taskname);
$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10);
$task->set_lock($lock);
// Catch the message.
manager::scheduled_task_failed($task);
$messages = $messagesink->get_messages();
$this->assertCount(0, $messages);
// Set the max fail delay time.
$task = manager::get_scheduled_task($taskname);
$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10);
$task->set_lock($lock);
$task->set_fail_delay(86400);
$task->execute();
// Catch the message.
manager::scheduled_task_failed($task);
$messages = $messagesink->get_messages();
$this->assertCount(1, $messages);
// Get the task and execute it second time.
$task = manager::get_scheduled_task($taskname);
$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10);
$task->set_lock($lock);
// Set the fail delay to 12 hours.
$task->set_fail_delay(43200);
$task->execute();
manager::scheduled_task_failed($task);
// Catch the message.
$messages = $messagesink->get_messages();
$this->assertCount(2, $messages);
// Get the task and execute it third time.
$task = manager::get_scheduled_task($taskname);
$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10);
$task->set_lock($lock);
// Set the fail delay to 48 hours.
$task->set_fail_delay(172800);
$task->execute();
manager::scheduled_task_failed($task);
// Catch the message.
$messages = $messagesink->get_messages();
$this->assertCount(3, $messages);
// Check first message information.
$this->assertStringContainsString('Task failed: Send new user passwords', $messages[0]->subject);
$this->assertEquals('failedtaskmaxdelay', $messages[0]->eventtype);
$this->assertEquals('-10', $messages[0]->useridfrom);
$this->assertEquals('2', $messages[0]->useridto);
// Close sink.
$messagesink->close();
}
}
+1
View File
@@ -143,6 +143,7 @@ information provided here is intended especially for developers.
because there aren't cases in core.
- Deprecation: Cannot use the "Test" suffix on abstract test case classes. Proceed to
rename them to end with "TestCase" instead.
* A new hook called after_failed_task_max_delay has been created. This hook will be triggered when an Ad-hoc or Scheduled task reaches its maximum failure delay.
=== 4.3 ===
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024040200.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024040200.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev+ (Build: 20240402)'; // Human-friendly version name