Merge lp:~akretion-team/server-env-tools/70-scheduler-error-mailer into lp:~server-env-tools-core-editors/server-env-tools/7.0

Proposed by Alexis de Lattre
Status: Merged
Merged at revision: 50
Proposed branch: lp:~akretion-team/server-env-tools/70-scheduler-error-mailer
Merge into: lp:~server-env-tools-core-editors/server-env-tools/7.0
Diff against target: 282 lines (+243/-0)
6 files modified
scheduler_error_mailer/__init__.py (+24/-0)
scheduler_error_mailer/__openerp__.py (+48/-0)
scheduler_error_mailer/ir_cron.py (+70/-0)
scheduler_error_mailer/ir_cron.xml (+24/-0)
scheduler_error_mailer/ir_cron_demo.xml (+27/-0)
scheduler_error_mailer/ir_cron_email_tpl.xml (+50/-0)
To merge this branch: bzr merge lp:~akretion-team/server-env-tools/70-scheduler-error-mailer
Reviewer Review Type Date Requested Status
Sébastien BEAU - http://www.akretion.com Approve
Pedro Manuel Baeza code review and test Approve
Stefan Rijnhart (Opener) Approve
Review via email: mp+188240@code.launchpad.net

Description of the change

Propose to add the "scheduler_error_mailer" module to lp:server-env-tools.

This is a small and very usefull module that sends an email (from an email template) when a scheduler raises an error. We use it on almost all our openerp deployments.

To post a comment you must log in.
Revision history for this message
Sébastien BEAU - http://www.akretion.com (sebastien.beau) wrote :

Thanks for your work Alexis
I just see error in the file "ir_cron_email_tpl.xml" indeed the noupdate option is set to 0 instead of 1.

Regarding the test, maybe it's not the best way to test it, but I don't know how we can do it correctly with a test on a cron @reviewer any idea? Yaml test, unit test?

Thanks

6. By Alexis de Lattre

FIX the noupdate flag on the email template.

Revision history for this message
Alexis de Lattre (alexis-via) wrote :

I just fixed the noupdate flag in ir_cron_email_tpl.xml (I set it to 0 to during the development, and forgot to put it back to 1 before my commit).

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Looks neat! No nits to pick, but only what might be a stupid question about the todo in line 269: can't you put the name of the database in the context like you do with the job_exception?

review: Needs Information
7. By Alexis de Lattre

Implement the suggestion of Stefan Rijnhart to get the name of the DB in the email.

Revision history for this message
Alexis de Lattre (alexis-via) wrote :

Thanks for your suggestion Stefan ; I was focused on trying to make the old solution work, but I didn't even thought about the easy solution you proposed !

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Great, thanks for the update!

review: Approve
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

It works like a charm, although the method not return error information when it'a code error, but that's not a problem of the module, but the callback core method.

Thanks for your work.

review: Approve (code review and test)
Revision history for this message
Sébastien BEAU - http://www.akretion.com (sebastien.beau) wrote :

Thanks for the update Alexis

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added directory 'scheduler_error_mailer'
=== added file 'scheduler_error_mailer/__init__.py'
--- scheduler_error_mailer/__init__.py 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/__init__.py 2013-09-30 22:06:15 +0000
@@ -0,0 +1,24 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# Scheduler Error Mailer module for OpenERP
5# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
6# @author: Sébastien Beau <sebastien.beau@akretion.com>
7# @author Alexis de Lattre <alexis.delattre@akretion.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU Affero General Public License as
11# published by the Free Software Foundation, either version 3 of the
12# License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU Affero General Public License for more details.
18#
19# You should have received a copy of the GNU Affero General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21#
22##############################################################################
23
24from . import ir_cron
025
=== added file 'scheduler_error_mailer/__openerp__.py'
--- scheduler_error_mailer/__openerp__.py 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/__openerp__.py 2013-09-30 22:06:15 +0000
@@ -0,0 +1,48 @@
1# -*- encoding: utf-8 -*-
2##############################################################################
3#
4# Scheduler Error Mailer module for OpenERP
5# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
6# @author: Sébastien Beau <sebastien.beau@akretion.com>
7# @author David Beal <bealdavid@gmail.com>
8# @author Alexis de Lattre <alexis.delattre@akretion.com>
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU Affero General Public License as
12# published by the Free Software Foundation, either version 3 of the
13# License, or (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU Affero General Public License for more details.
19#
20# You should have received a copy of the GNU Affero General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
23##############################################################################
24
25
26{
27 'name': 'Scheduler Error Mailer',
28 'summary': 'Send an e-mail when a scheduler fails',
29 'version': '1.0',
30 'category': 'Extra Tools',
31 'license': 'AGPL-3',
32 'description': """
33Scheduler Error Mailer
34======================
35
36This module adds the possibility to send an e-mail when a scheduler raises an error.""",
37 'author': 'Akretion',
38 'website': 'http://www.akretion.com/',
39 'depends': ['email_template'],
40 'data': [
41 'ir_cron.xml',
42 'ir_cron_email_tpl.xml',
43 ],
44 'demo': ['ir_cron_demo.xml'],
45 'images': ['images/scheduler_error_mailer.jpg'],
46 'installable': True,
47 'active': False,
48}
049
=== added directory 'scheduler_error_mailer/images'
=== added file 'scheduler_error_mailer/images/scheduler_error_mailer.jpg'
1Binary files scheduler_error_mailer/images/scheduler_error_mailer.jpg 1970-01-01 00:00:00 +0000 and scheduler_error_mailer/images/scheduler_error_mailer.jpg 2013-09-30 22:06:15 +0000 differ50Binary files scheduler_error_mailer/images/scheduler_error_mailer.jpg 1970-01-01 00:00:00 +0000 and scheduler_error_mailer/images/scheduler_error_mailer.jpg 2013-09-30 22:06:15 +0000 differ
=== added file 'scheduler_error_mailer/ir_cron.py'
--- scheduler_error_mailer/ir_cron.py 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/ir_cron.py 2013-09-30 22:06:15 +0000
@@ -0,0 +1,70 @@
1# -*- encoding: utf-8 -*-
2#################################################################################
3#
4# Scheduler Error Mailer module for OpenERP
5# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
6# @author: Sébastien Beau <sebastien.beau@akretion.com>
7# @author David Beal <bealdavid@gmail.com>
8# @author Alexis de Lattre <alexis.delattre@akretion.com>
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU Affero General Public License as
12# published by the Free Software Foundation, either version 3 of the
13# License, or (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU Affero General Public License for more details.
19#
20# You should have received a copy of the GNU Affero General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
23##############################################################################
24
25from openerp.osv import orm, fields
26from openerp.tools.translate import _
27import logging
28
29logger = logging.getLogger(__name__)
30
31class ir_cron(orm.Model):
32 _inherit = "ir.cron"
33
34 _columns = {
35 'email_template': fields.many2one('email.template',
36 'Error E-mail Template',
37 help="Select the email template that will be sent when this scheduler fails."),
38 }
39
40
41 def _handle_callback_exception(self, cr, uid, model_name, method_name, args, job_id, job_exception):
42
43 res = super(ir_cron, self)._handle_callback_exception(cr, uid,
44 model_name, method_name, args, job_id, job_exception)
45
46 my_cron = self.browse(cr, uid, job_id)
47
48 if my_cron.email_template:
49 # we put the job_exception in context to be able to print it inside
50 # the email template
51 context = {
52 'job_exception': job_exception,
53 'dbname': cr.dbname,
54 }
55
56 logger.debug("Sending scheduler error email with context=%s" % context)
57 self.pool['email.template'].send_mail(cr, uid,
58 my_cron.email_template.id, my_cron.id, force_send=True,
59 context=context)
60
61 return res
62
63
64class res_users(orm.Model):
65 _inherit = 'res.users'
66
67 def test_scheduler_failure(self, cr, uid, context=None):
68 """This function is used to test and debug this module"""
69 raise orm.except_orm(_('Error :'), _("Task failure with UID = %d." % uid))
70
071
=== added file 'scheduler_error_mailer/ir_cron.xml'
--- scheduler_error_mailer/ir_cron.xml 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/ir_cron.xml 2013-09-30 22:06:15 +0000
@@ -0,0 +1,24 @@
1<?xml version="1.0" encoding="utf-8"?>
2<!--
3 Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
4 The licence is in the file __openerp__.py
5-->
6
7<openerp>
8<data>
9
10
11<record id="ir_cron_error_mailer_view" model="ir.ui.view">
12 <field name="name">ir.cron.error.mailer.form</field>
13 <field name="model">ir.cron</field>
14 <field name="inherit_id" ref="base.ir_cron_view"/>
15 <field name="arch" type="xml">
16 <field name="doall" position="after">
17 <field name="email_template" />
18 </field>
19 </field>
20</record>
21
22
23</data>
24</openerp>
025
=== added file 'scheduler_error_mailer/ir_cron_demo.xml'
--- scheduler_error_mailer/ir_cron_demo.xml 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/ir_cron_demo.xml 2013-09-30 22:06:15 +0000
@@ -0,0 +1,27 @@
1<?xml version="1.0" encoding="utf-8"?>
2
3<!--
4 Copyright (C) 2013 Akretion (http://www.akretion.com/)
5 @author Alexis de Lattre <alexis.delattre@akretion.com>
6 The licence is in the file __openerp__.py
7-->
8
9<openerp>
10<data noupdate="1">
11
12<record id="test_scheduler_error_mailer" model="ir.cron">
13 <field name="name">Test Scheduler Error Mailer</field>
14 <field name="active" eval="False"/>
15 <field name="user_id" ref="base.user_root"/>
16 <field name="interval_number">1</field>
17 <field name="interval_type">hours</field>
18 <field name="numbercall">-1</field> <!-- don't limit the number of calls -->
19 <field name="doall" eval="True"/>
20 <field name="email_template" ref="scheduler_error_mailer"/>
21 <field name="model" eval="'res.users'"/>
22 <field name="function" eval="'test_scheduler_failure'" />
23 <field name="args" eval="'()'"/>
24</record>
25
26</data>
27</openerp>
028
=== added file 'scheduler_error_mailer/ir_cron_email_tpl.xml'
--- scheduler_error_mailer/ir_cron_email_tpl.xml 1970-01-01 00:00:00 +0000
+++ scheduler_error_mailer/ir_cron_email_tpl.xml 2013-09-30 22:06:15 +0000
@@ -0,0 +1,50 @@
1<?xml version="1.0" encoding="utf-8"?>
2<!--
3 Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
4 The licence is in the file __openerp__.py
5-->
6
7<openerp>
8<data noupdate="1">
9
10<!-- Error Email template -->
11<record id="scheduler_error_mailer" model="email.template">
12 <field name="name">Scheduler Error</field>
13 <field name="email_from">${object.user_id.user_email or ''}</field>
14 <field name="email_to">${object.user_id.user_email or ''}</field>
15 <field name="subject">[DB ${ctx.get('dbname')}] Scheduler '${object.name or ''}' FAILED</field>
16 <field name="model_id" ref="base.model_ir_cron"/>
17 <field name="auto_delete" eval="True"/>
18 <field name="body_html"><![CDATA[
19<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
20
21<p>OpenERP tried to run the scheduler <em>${object.name or ''}</em> in the database <em>${ctx.get('dbname')}</em> but it failed. Here is the error message :</p>
22
23<strong>
24${ctx.get('job_exception') and ctx.get('job_exception').value or 'Failed to get the error message from the context.'}
25</strong>
26
27<p>You may check the logs of the OpenERP server to get more information about this failure.</p>
28
29<p>Properties of the scheduler <em>${object.name or ''}</em> :</p>
30<ul>
31<li>Model : ${object.model or ''}</li>
32<li>Method : ${object.function or ''}</li>
33<li>Arguments : ${object.args or ''}</li>
34<li>Interval : ${object.interval_number or '0'} ${object.interval_type or ''}</li>
35<li>Number of calls : ${object.numbercall or '0'}</li>
36<li>Repeat missed : ${object.doall}</li>
37<li>User : ${object.user_id.name or ''}</li>
38</ul>
39
40<p>
41-- <br/>
42Automatic e-mail sent by OpenERP. Do not reply.<br/>
43Database : ${ctx.get('dbname')}
44</p>
45</div>
46 ]]></field>
47</record>
48
49</data>
50</openerp>
051
=== added directory 'scheduler_error_mailer/static'
=== added directory 'scheduler_error_mailer/static/src'
=== added directory 'scheduler_error_mailer/static/src/img'
=== added file 'scheduler_error_mailer/static/src/img/icon.png'
1Binary files scheduler_error_mailer/static/src/img/icon.png 1970-01-01 00:00:00 +0000 and scheduler_error_mailer/static/src/img/icon.png 2013-09-30 22:06:15 +0000 differ52Binary files scheduler_error_mailer/static/src/img/icon.png 1970-01-01 00:00:00 +0000 and scheduler_error_mailer/static/src/img/icon.png 2013-09-30 22:06:15 +0000 differ