Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion scheduler_error_mailer/README.rst
Original file line number Diff line number Diff line change
@@ -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
======================
Expand All @@ -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
Expand Down Expand Up @@ -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
===========
Expand Down
2 changes: 1 addition & 1 deletion scheduler_error_mailer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
1 change: 1 addition & 0 deletions scheduler_error_mailer/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions scheduler_error_mailer/models/ir_actions_server.py
Original file line number Diff line number Diff line change
@@ -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.",
)
24 changes: 24 additions & 0 deletions scheduler_error_mailer/models/ir_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions scheduler_error_mailer/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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).
39 changes: 24 additions & 15 deletions scheduler_error_mailer/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>Scheduler Error Mailer</title>
<title>README.rst</title>
<style type="text/css">

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -360,16 +360,21 @@
</style>
</head>
<body>
<div class="document" id="scheduler-error-mailer">
<h1 class="title">Scheduler Error Mailer</h1>
<div class="document">


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="scheduler-error-mailer">
<h1>Scheduler Error Mailer</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:d1c4142c705cf5db93d256ea27868cb3aa617f9f70599c1b364806d67b46ccd4
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-tools/tree/14.0/scheduler_error_mailer"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-tools-14-0/server-tools-14-0-scheduler_error_mailer"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-tools/tree/14.0/scheduler_error_mailer"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-tools-14-0/server-tools-14-0-scheduler_error_mailer"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds the possibility to send an e-mail when a scheduler raises
an error.</p>
<p><strong>Table of contents</strong></p>
Expand All @@ -386,32 +391,33 @@ <h1 class="title">Scheduler Error Mailer</h1>
</ul>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
<p>To configure this module, you need to:</p>
<ol class="arabic simple">
<li>Go to Settings -&gt; Technical -&gt; Automation -&gt; Scheduled Actions</li>
<li>Choose the scheduled Actions you want to send the error email and select the E-mail Template in the Error E-mail Template field.</li>
<li>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).</li>
</ol>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<h2><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h2>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20scheduler_error_mailer%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
<h2><a class="toc-backref" href="#toc-entry-3">Credits</a></h2>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<h3><a class="toc-backref" href="#toc-entry-4">Authors</a></h3>
<ul class="simple">
<li>Akretion</li>
<li>Sodexis</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
<ul class="simple">
<li>Sébastien BEAU &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li>
<li>David Beal &lt;<a class="reference external" href="mailto:bealdavid&#64;gmail.com">bealdavid&#64;gmail.com</a>&gt;</li>
Expand All @@ -427,9 +433,11 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>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.</p>
Expand All @@ -438,5 +446,6 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
</div>
</div>
</div>
</div>
</body>
</html>
27 changes: 27 additions & 0 deletions scheduler_error_mailer/tests/test_scheduler_error_mailer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2021 Akretion - Chafique Delli
# Copyright 2021 Alfredo Zamora <alfredo.zamora@agilebg.com>
# 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

Expand Down Expand Up @@ -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)
8 changes: 8 additions & 0 deletions scheduler_error_mailer/views/ir_cron.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
<field name="arch" type="xml">
<field name="doall" position="after">
<field name="email_template_id" />
<field
name="email_retries"
attrs="{'invisible': [('email_template_id', '=', False)]}"
/>
<field
name="failure_count"
attrs="{'invisible': [('email_template_id', '=', False)]}"
/>
</field>
</field>
</record>
Expand Down
Loading