MDL-64417 message: allow processors to run their own cleanup tasks.

This commit is contained in:
Paul Holden 2020-02-06 07:59:57 +00:00
parent 788dfb9c7d
commit db8bda61a3
3 changed files with 42 additions and 5 deletions

View file

@ -38,7 +38,7 @@ class messaging_cleanup_task extends scheduled_task {
} }
/** /**
* Do the job. * Do the job. Each message processor also gets the chance to perform it's own cleanup.
* Throw exceptions on errors (the job will be retried). * Throw exceptions on errors (the job will be retried).
*/ */
public function execute() { public function execute() {
@ -46,9 +46,17 @@ class messaging_cleanup_task extends scheduled_task {
$timenow = time(); $timenow = time();
$processors = get_message_processors(true);
// Cleanup read and unread notifications. // Cleanup read and unread notifications.
if (!empty($CFG->messagingdeleteallnotificationsdelay)) { if (!empty($CFG->messagingdeleteallnotificationsdelay)) {
$notificationdeletetime = $timenow - $CFG->messagingdeleteallnotificationsdelay; $notificationdeletetime = $timenow - $CFG->messagingdeleteallnotificationsdelay;
/** @var \message_output $processor */
foreach (array_column($processors, 'object') as $processor) {
$processor->cleanup_all_notifications($notificationdeletetime);
}
$params = array('notificationdeletetime' => $notificationdeletetime); $params = array('notificationdeletetime' => $notificationdeletetime);
$DB->delete_records_select('notifications', 'timecreated < :notificationdeletetime', $params); $DB->delete_records_select('notifications', 'timecreated < :notificationdeletetime', $params);
} }
@ -56,6 +64,12 @@ class messaging_cleanup_task extends scheduled_task {
// Cleanup read notifications. // Cleanup read notifications.
if (!empty($CFG->messagingdeletereadnotificationsdelay)) { if (!empty($CFG->messagingdeletereadnotificationsdelay)) {
$notificationdeletetime = $timenow - $CFG->messagingdeletereadnotificationsdelay; $notificationdeletetime = $timenow - $CFG->messagingdeletereadnotificationsdelay;
/** @var \message_output $processor */
foreach (array_column($processors, 'object') as $processor) {
$processor->cleanup_read_notifications($notificationdeletetime);
}
$params = array('notificationdeletetime' => $notificationdeletetime); $params = array('notificationdeletetime' => $notificationdeletetime);
$DB->delete_records_select('notifications', 'timeread < :notificationdeletetime', $params); $DB->delete_records_select('notifications', 'timeread < :notificationdeletetime', $params);
} }

View file

@ -121,7 +121,26 @@ abstract class message_output {
public function force_process_messages() { public function force_process_messages() {
return false; return false;
} }
/**
* Allow processors to perform cleanup tasks for all notifications by overriding this method
*
* @since Moodle 3.9
* @param int $notificationdeletetime
* @return void
*/
public function cleanup_all_notifications(int $notificationdeletetime): void {
return;
}
/**
* Allow processors to perform cleanup tasks for read notifications by overriding this method
*
* @since Moodle 3.9
* @param int $notificationdeletetime
* @return void
*/
public function cleanup_read_notifications(int $notificationdeletetime): void {
return;
}
} }

View file

@ -2,6 +2,7 @@ This files describes API changes in /message/ messaging system,
information provided here is intended especially for developers. information provided here is intended especially for developers.
=== 3.9 === === 3.9 ===
* Removed the following deprecated functions: * Removed the following deprecated functions:
- message_move_userfrom_unread2read - message_move_userfrom_unread2read
- message_get_blocked_users - message_get_blocked_users
@ -9,7 +10,10 @@ information provided here is intended especially for developers.
- message_mark_message_read - message_mark_message_read
- message_can_delete_message - message_can_delete_message
- message_delete_message - message_delete_message
* mark_all_read_for_user() - mark_all_read_for_user()
* Message processors can implement the following methods which will be executed as part of the messaging cleanup task:
- cleanup_all_notifications
- cleanup_read_notifications
=== 3.8 === === 3.8 ===