From 9cc8421456b059db0a44a6514f89b5c06e1e6152 Mon Sep 17 00:00:00 2001 From: Dan Kiplangat Date: Mon, 17 Aug 2026 20:50:56 +0300 Subject: [PATCH] [14.0][IMP] scheduler_error_mailer - add retries --- scheduler_error_mailer/README.rst | 7 +++- scheduler_error_mailer/__manifest__.py | 2 +- scheduler_error_mailer/models/__init__.py | 1 + .../models/ir_actions_server.py | 13 +++++++ scheduler_error_mailer/models/ir_cron.py | 24 ++++++++++++ scheduler_error_mailer/readme/CONFIGURE.rst | 1 + .../static/description/index.html | 39 ++++++++++++------- .../tests/test_scheduler_error_mailer.py | 27 +++++++++++++ scheduler_error_mailer/views/ir_cron.xml | 8 ++++ 9 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 scheduler_error_mailer/models/ir_actions_server.py diff --git a/scheduler_error_mailer/README.rst b/scheduler_error_mailer/README.rst index 33c3c9e59fe..b1bf727e7bd 100644 --- a/scheduler_error_mailer/README.rst +++ b/scheduler_error_mailer/README.rst @@ -1,3 +1,7 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + ====================== Scheduler Error Mailer ====================== @@ -13,7 +17,7 @@ Scheduler Error Mailer .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github @@ -43,6 +47,7 @@ To configure this module, you need to: #. Go to Settings -> Technical -> Automation -> Scheduled Actions #. Choose the scheduled Actions you want to send the error email and select the E-mail Template in the Error E-mail Template field. +#. Optionally set the Email Retries field to the number of consecutive failures allowed before the error email is sent (1 by default, i.e. the email is sent on the first failure). Bug Tracker =========== diff --git a/scheduler_error_mailer/__manifest__.py b/scheduler_error_mailer/__manifest__.py index ecfcb87746d..34617d0b078 100644 --- a/scheduler_error_mailer/__manifest__.py +++ b/scheduler_error_mailer/__manifest__.py @@ -6,7 +6,7 @@ { "name": "Scheduler Error Mailer", - "version": "14.0.1.2.1", + "version": "14.0.1.3.0", "category": "Extra Tools", "license": "AGPL-3", "author": "Akretion,Sodexis,Odoo Community Association (OCA)", diff --git a/scheduler_error_mailer/models/__init__.py b/scheduler_error_mailer/models/__init__.py index b365c0e973b..6a099d23e8b 100644 --- a/scheduler_error_mailer/models/__init__.py +++ b/scheduler_error_mailer/models/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import ir_actions_server from . import ir_cron diff --git a/scheduler_error_mailer/models/ir_actions_server.py b/scheduler_error_mailer/models/ir_actions_server.py new file mode 100644 index 00000000000..b5f78137a2e --- /dev/null +++ b/scheduler_error_mailer/models/ir_actions_server.py @@ -0,0 +1,13 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class IrActionsServer(models.Model): + _inherit = "ir.actions.server" + + failure_count = fields.Integer( + default=0, + readonly=True, + help="Number of consecutive failures of the related scheduled action.", + ) diff --git a/scheduler_error_mailer/models/ir_cron.py b/scheduler_error_mailer/models/ir_cron.py index bbe24ad82b8..948af720a78 100644 --- a/scheduler_error_mailer/models/ir_cron.py +++ b/scheduler_error_mailer/models/ir_cron.py @@ -20,6 +20,24 @@ class IrCron(models.Model): help="Select the email template that will be sent when " "this scheduler fails.", ) + email_retries = fields.Integer( + string="Email Retries", + default=1, + help="Number of consecutive failures allowed " + "before the error email is sent.", + ) + + @api.model + def _callback(self, cron_name, server_action_id, job_id): + # failure_count is stored on the delegated ir.actions.server row: + # the scheduler locks the ir_cron row while the job runs, so the + # cron record itself cannot be written from within the job + action = self.env["ir.actions.server"].sudo().browse(server_action_id) + failures_before = action.failure_count + res = super()._callback(cron_name, server_action_id, job_id) + if failures_before and action.failure_count == failures_before: + action.failure_count = 0 + return res @api.model def _handle_callback_exception( @@ -31,6 +49,12 @@ def _handle_callback_exception( my_cron = self.browse(job_id) if my_cron.email_template_id: + action = my_cron.ir_actions_server_id.sudo() + action.failure_count += 1 + if action.failure_count < my_cron.email_retries: + return + action.failure_count = 0 + # we put the job_exception in context to be able to print it inside # the email template context = { diff --git a/scheduler_error_mailer/readme/CONFIGURE.rst b/scheduler_error_mailer/readme/CONFIGURE.rst index baf4c1a6da0..902da70c00d 100644 --- a/scheduler_error_mailer/readme/CONFIGURE.rst +++ b/scheduler_error_mailer/readme/CONFIGURE.rst @@ -2,3 +2,4 @@ To configure this module, you need to: #. Go to Settings -> Technical -> Automation -> Scheduled Actions #. Choose the scheduled Actions you want to send the error email and select the E-mail Template in the Error E-mail Template field. +#. Optionally set the Email Retries field to the number of consecutive failures allowed before the error email is sent (1 by default, i.e. the email is sent on the first failure). diff --git a/scheduler_error_mailer/static/description/index.html b/scheduler_error_mailer/static/description/index.html index 561819f8813..dca61f5a1cf 100644 --- a/scheduler_error_mailer/static/description/index.html +++ b/scheduler_error_mailer/static/description/index.html @@ -1,18 +1,18 @@ - -Scheduler Error Mailer +README.rst -
-

Scheduler Error Mailer

+
+ + +Odoo Community Association + +
+

Scheduler Error Mailer

-

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

This module adds the possibility to send an e-mail when a scheduler raises an error.

Table of contents

@@ -386,15 +391,16 @@

Scheduler Error Mailer

-

Configuration

+

Configuration

To configure this module, you need to:

  1. Go to Settings -> Technical -> Automation -> Scheduled Actions
  2. Choose the scheduled Actions you want to send the error email and select the E-mail Template in the Error E-mail Template field.
  3. +
  4. Optionally set the Email Retries field to the number of consecutive failures allowed before the error email is sent (1 by default, i.e. the email is sent on the first failure).
-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -402,16 +408,16 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Akretion
  • Sodexis
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

-Odoo Community Association + +Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

@@ -438,5 +446,6 @@

Maintainers

+
diff --git a/scheduler_error_mailer/tests/test_scheduler_error_mailer.py b/scheduler_error_mailer/tests/test_scheduler_error_mailer.py index d5ceda3bacc..6d5345b6840 100644 --- a/scheduler_error_mailer/tests/test_scheduler_error_mailer.py +++ b/scheduler_error_mailer/tests/test_scheduler_error_mailer.py @@ -1,6 +1,8 @@ # Copyright 2021 Akretion - Chafique Delli # Copyright 2021 Alfredo Zamora # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from unittest.mock import patch + from odoo.tests.common import TransactionCase from odoo.tools import mute_logger @@ -29,3 +31,28 @@ def test_scheduler_failure(self): "[DB " + self.cr.dbname + "] Scheduler 'Test Scheduler Error Mailer' FAILED" ) self.assertEqual(mails.subject, subject) + + @mute_logger("odoo.addons.base.models.ir_cron") + def test_scheduler_failure_retries(self): + action = self.cron.ir_actions_server_id + action.failure_count = 0 + self.cron.email_retries = 2 + with patch.object(self.env.cr, "rollback"), patch.object( + self.registry["mail.template"], "send_mail" + ) as send_mail: + self.env["ir.cron"]._handle_callback_exception( + self.cron.name, + action.id, + self.cron.id, + Exception("hello world"), + ) + send_mail.assert_not_called() + self.assertEqual(action.failure_count, 1) + self.env["ir.cron"]._handle_callback_exception( + self.cron.name, + action.id, + self.cron.id, + Exception("hello world"), + ) + send_mail.assert_called_once() + self.assertEqual(action.failure_count, 0) diff --git a/scheduler_error_mailer/views/ir_cron.xml b/scheduler_error_mailer/views/ir_cron.xml index 731c34f1cc7..57b92e65e21 100644 --- a/scheduler_error_mailer/views/ir_cron.xml +++ b/scheduler_error_mailer/views/ir_cron.xml @@ -15,6 +15,14 @@ + +