MDL-64417 message: allow processors to run their own cleanup tasks.
authorPaul Holden <paulh@moodle.com>
Thu, 6 Feb 2020 07:59:57 +0000 (07:59 +0000)
committerPaul Holden <paulh@moodle.com>
Tue, 21 Apr 2020 13:48:22 +0000 (14:48 +0100)
lib/classes/task/messaging_cleanup_task.php
message/output/lib.php
message/upgrade.txt

index 15b781f..eecc092 100644 (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).
      */
     public function execute() {
@@ -46,9 +46,17 @@ class messaging_cleanup_task extends scheduled_task {
 
         $timenow = time();
 
+        $processors = get_message_processors(true);
+
         // Cleanup read and unread notifications.
         if (!empty($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);
             $DB->delete_records_select('notifications', 'timecreated < :notificationdeletetime', $params);
         }
@@ -56,6 +64,12 @@ class messaging_cleanup_task extends scheduled_task {
         // Cleanup read notifications.
         if (!empty($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);
             $DB->delete_records_select('notifications', 'timeread < :notificationdeletetime', $params);
         }
index ce7fd76..bd29b25 100644 (file)
@@ -121,7 +121,26 @@ abstract class message_output {
     public function force_process_messages() {
         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;
+    }
+}
index efc1f23..886e788 100644 (file)
@@ -2,6 +2,7 @@ This files describes API changes in /message/ messaging system,
 information provided here is intended especially for developers.
 
 === 3.9 ===
+
 * Removed the following deprecated functions:
   - message_move_userfrom_unread2read
   - message_get_blocked_users
@@ -9,7 +10,10 @@ information provided here is intended especially for developers.
   - message_mark_message_read
   - message_can_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 ===