From 28d6f1adfcb28688276240775d2a140e53048b0c Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Fri, 19 Feb 2010 17:56:20 +0000
Subject: [PATCH] Improve notification in case of failed activity

* Change subject from "Failed Processing Activity" to
  "Activity failed: <path>/<method>"
* The subject starts with "Pending activity already failed <failures> times:"
  if CMFActivity is going to reexecute indefinitely.
* Drop the first line of the body. It was repeating the the subject.
* Add number of failures.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32880 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/CMFActivity/Activity/SQLBase.py      |  8 ++++----
 product/CMFActivity/ActivityTool.py          | 21 ++++++++++++--------
 product/CMFActivity/tests/testCMFActivity.py |  6 +++---
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/product/CMFActivity/Activity/SQLBase.py b/product/CMFActivity/Activity/SQLBase.py
index 7e9ea825ad..a0613cf932 100644
--- a/product/CMFActivity/Activity/SQLBase.py
+++ b/product/CMFActivity/Activity/SQLBase.py
@@ -171,13 +171,13 @@ class SQLBase:
           retry = m.line.retry
           if max_retry is not None and retry >= max_retry:
             # Always notify when we stop retrying.
-            notify_user_list.append(m)
+            notify_user_list.append((m, False))
             final_error_uid_list.append(uid)
             continue
           # In case of infinite retry, notify the user
           # when the default limit is reached.
           if max_retry is None and retry == m.__class__.max_retry:
-            notify_user_list.append(m)
+            notify_user_list.append((m, True))
           delay = m.delay
           if delay is None:
             # By default, make delay quadratic to the number of retries.
@@ -235,8 +235,8 @@ class SQLBase:
       else:
         self._log(TRACE, 'Freed messages %r' % make_available_uid_list)
     try:
-      for m in notify_user_list:
-        m.notifyUser(activity_tool)
+      for m, retry in notify_user_list:
+        m.notifyUser(activity_tool, retry)
     except:
       # Notification failures must not cause this method to raise.
       self._log(WARNING,
diff --git a/product/CMFActivity/ActivityTool.py b/product/CMFActivity/ActivityTool.py
index cdb7c8ede4..4a1ad6b60d 100644
--- a/product/CMFActivity/ActivityTool.py
+++ b/product/CMFActivity/ActivityTool.py
@@ -310,7 +310,7 @@ class Message(BaseMessage):
   def getDependentMessageList(self, activity, activity_tool):
     return activity.getDependentMessageList(activity_tool, self, **self.activity_kw)
 
-  def notifyUser(self, activity_tool, message="Failed Processing Activity"):
+  def notifyUser(self, activity_tool, retry=False):
     """Notify the user that the activity failed."""
     portal = activity_tool.getPortalObject()
     user_email = portal.getProperty('email_to_address',
@@ -322,13 +322,18 @@ class Message(BaseMessage):
     if self.call_traceback:
       call_traceback = 'Created at:\n%s' % self.call_traceback
 
+    fail_count = self.line.retry + 1
+    if retry:
+      message = "Pending activity already failed %s times" % fail_count
+    else:
+      message = "Activity failed"
+    path = '/'.join(self.object_path)
     mail_text = """From: %s <%s>
 To: %s
-Subject: %s
-
-%s
+Subject: %s: %s/%s
 
 Node: %s
+Failures: %s
 User name: %r
 Document: %s
 Method: %s
@@ -339,10 +344,10 @@ Named Parameters: %r
 Exception: %s %s
 
 %s
-""" % (email_from_name, activity_tool.email_from_address, 
-       user_email, message, message,
-       activity_tool.getCurrentNode(), self.user_name,
-       '/'.join(self.object_path), self.method_id, self.args, self.kw,
+""" % (email_from_name, activity_tool.email_from_address, user_email,
+       message, path, self.method_id,
+       activity_tool.getCurrentNode(), fail_count,
+       self.user_name, path, self.method_id, self.args, self.kw,
        call_traceback, self.exc_type, self.exc_value, self.traceback)
 
     if isinstance(mail_text, unicode):
diff --git a/product/CMFActivity/tests/testCMFActivity.py b/product/CMFActivity/tests/testCMFActivity.py
index 7d6b05b5b0..92bf264887 100644
--- a/product/CMFActivity/tests/testCMFActivity.py
+++ b/product/CMFActivity/tests/testCMFActivity.py
@@ -1562,7 +1562,7 @@ class TestCMFActivity(ERP5TypeTestCase):
     # Monkey patch Message not to send failure notification emails
     from Products.CMFActivity.ActivityTool import Message
     originalNotifyUser = Message.notifyUser
-    def notifyUserSilent(self, activity_tool, message=''):
+    def notifyUserSilent(self, *args, **kw):
       pass
     Message.notifyUser = notifyUserSilent
 
@@ -1645,7 +1645,7 @@ class TestCMFActivity(ERP5TypeTestCase):
     # Monkey patch Message not to send failure notification emails
     from Products.CMFActivity.ActivityTool import Message
     originalNotifyUser = Message.notifyUser
-    def notifyUserSilent(self, activity_tool, message=''):
+    def notifyUserSilent(self, *args, **kw):
       pass
     Message.notifyUser = notifyUserSilent
 
@@ -2324,7 +2324,7 @@ class TestCMFActivity(ERP5TypeTestCase):
     # monkeypatch method.
     notification_done = []
     from Products.CMFActivity.ActivityTool import Message
-    def fake_notifyUser(self, activity_tool):
+    def fake_notifyUser(self, *args, **kw):
       notification_done.append(True)
     original_notifyUser = Message.notifyUser
     def failingMethod(self):
-- 
2.30.9