From 3ee2fb0433e697fc592d2b9e9c768384e99cfd29 Mon Sep 17 00:00:00 2001
From: Justus Dieckmann <justusdieckmann@wwu.de>
Date: Wed, 12 Jan 2022 15:27:22 +0100
Subject: [PATCH] Only notify for new errors every cron

---
 classes/local/manager/process_manager.php    |  3 +++
 classes/task/lifecycle_error_notify_task.php | 11 ++++++++++-
 db/install.xml                               |  1 +
 db/upgrade.php                               |  1 +
 lang/de/tool_lifecycle.php                   |  6 +++---
 lang/en/tool_lifecycle.php                   |  6 +++---
 6 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/classes/local/manager/process_manager.php b/classes/local/manager/process_manager.php
index 96e43b4..1c3c74a 100644
--- a/classes/local/manager/process_manager.php
+++ b/classes/local/manager/process_manager.php
@@ -260,6 +260,7 @@ class process_manager {
         $procerror = (object) clone $process;
         $procerror->errormessage = get_class($e) . ': ' . $e->getMessage();
         $procerror->errortrace = $e->getTraceAsString();
+        $procerror->errortime = time();
         $m = '';
         foreach ($e->getTrace() as $v) {
             $m .= $v['file'] . ':' . $v['line'] . '::';
@@ -283,6 +284,7 @@ class process_manager {
         unset($process->errormessage);
         unset($process->errortrace);
         unset($process->errorhash);
+        unset($process->errortime);
 
         $DB->insert_record_raw('tool_lifecycle_process', $process, false, false, true);
         $DB->delete_records('tool_lifecycle_proc_error', ['id' => $process->id]);
@@ -301,6 +303,7 @@ class process_manager {
         unset($process->errormessage);
         unset($process->errortrace);
         unset($process->errorhash);
+        unset($process->errortime);
 
         $DB->insert_record_raw('tool_lifecycle_process', $process, false, false, true);
         $DB->delete_records('tool_lifecycle_proc_error', ['id' => $process->id]);
diff --git a/classes/task/lifecycle_error_notify_task.php b/classes/task/lifecycle_error_notify_task.php
index 7c15fa8..ec631a4 100644
--- a/classes/task/lifecycle_error_notify_task.php
+++ b/classes/task/lifecycle_error_notify_task.php
@@ -50,7 +50,16 @@ class lifecycle_error_notify_task extends \core\task\scheduled_task {
     public function execute() {
         global $DB, $CFG;
 
-        $errorcount = $DB->count_records('tool_lifecycle_proc_error');
+        $lastrun = get_config('tool_lifecycle', 'adminerrornotifylastrun');
+        if (!$lastrun) {
+            $lastrun = 0;
+        }
+
+        $currenttime = time();
+
+        $errorcount = $DB->count_records_select('tool_lifecycle_proc_error', 'errortime > :lastrun', ['lastrun' => $lastrun]);
+
+        set_config('adminerrornotifylastrun', $currenttime, 'tool_lifecycle');
 
         if (!$errorcount) {
             return;
diff --git a/db/install.xml b/db/install.xml
index 4c08580..22f2fa0 100644
--- a/db/install.xml
+++ b/db/install.xml
@@ -157,6 +157,7 @@
         <FIELD NAME="errormessage" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Message of the error"/>
         <FIELD NAME="errortrace" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
         <FIELD NAME="errorhash" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Where the error occured in the form 'path/to/filename.php:line'"/>
+        <FIELD NAME="errortime" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false" COMMENT="unix timestamp - time the error occured"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
diff --git a/db/upgrade.php b/db/upgrade.php
index c0b808b..a0a11f6 100644
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -461,6 +461,7 @@ function xmldb_tool_lifecycle_upgrade($oldversion) {
         $table->add_field('errormessage', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
         $table->add_field('errortrace', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
         $table->add_field('errorhash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
+        $table->add_field('errortime', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
 
         // Adding keys to table tool_lifecycle_proc_error.
         $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
diff --git a/lang/de/tool_lifecycle.php b/lang/de/tool_lifecycle.php
index 17706b0..fa77816 100644
--- a/lang/de/tool_lifecycle.php
+++ b/lang/de/tool_lifecycle.php
@@ -183,6 +183,6 @@ $string['forselected'] = 'Für alle ausgewählten Prozesse';
 $string['noprocesserrors'] = 'Es gibt keine fehlerhaften Prozesse, die behandelt werden müssen!';
 
 $string['lifecycle_error_notify_task'] = 'Benachrichtigt die Administratoren bei Fehlern in tool_lifecycle-Prozessen.';
-$string['notifyerrorsemailsubject'] = '{$a->amount} fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!';
-$string['notifyerrorsemailcontent'] = '{$a->amount} fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!' . "\n" . 'Bitte besuchen Sie {$a->url}.';
-$string['notifyerrorsemailcontenthtml'] = '{$a->amount} fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!<br>Bitte besuchen Sie <a href="{$a->url}">die Übersichtsseite</a>.';
+$string['notifyerrorsemailsubject'] = '{$a->amount} neue fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!';
+$string['notifyerrorsemailcontent'] = '{$a->amount} neue fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!' . "\n" . 'Bitte besuchen Sie {$a->url}.';
+$string['notifyerrorsemailcontenthtml'] = '{$a->amount} neue fehlerhafte tool_lifecycle Prozesse warten darauf, behandelt zu werden!<br>Bitte besuchen Sie <a href="{$a->url}">die Übersichtsseite</a>.';
diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php
index b1149de..420eb26 100644
--- a/lang/en/tool_lifecycle.php
+++ b/lang/en/tool_lifecycle.php
@@ -219,6 +219,6 @@ $string['forselected'] = 'For all selected processes';
 $string['noprocesserrors'] = 'There are no process errors to handle!';
 
 $string['lifecycle_error_notify_task'] = 'Notify the admin upon errors in tool_lifecycle processes';
-$string['notifyerrorsemailsubject'] = 'There are {$a->amount} tool_lifecycle process errors waiting to be fixed!';
-$string['notifyerrorsemailcontent'] = 'There are {$a->amount} tool_lifecycle process errors waiting to be fixed!' . "\n" . 'Please review them at {$a->url}.';
-$string['notifyerrorsemailcontenthtml'] = 'There are {$a->amount} tool_lifecycle process errors waiting to be fixed!<br>Please review them at the <a href="{$a->url}">error handling overview</a>.';
+$string['notifyerrorsemailsubject'] = 'There are {$a->amount} new tool_lifecycle process errors waiting to be fixed!';
+$string['notifyerrorsemailcontent'] = 'There are {$a->amount} new tool_lifecycle process errors waiting to be fixed!' . "\n" . 'Please review them at {$a->url}.';
+$string['notifyerrorsemailcontenthtml'] = 'There are {$a->amount} new tool_lifecycle process errors waiting to be fixed!<br>Please review them at the <a href="{$a->url}">error handling overview</a>.';
-- 
GitLab