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
1=== added directory 'scheduler_error_mailer'
2=== added file 'scheduler_error_mailer/__init__.py'
3--- scheduler_error_mailer/__init__.py 1970-01-01 00:00:00 +0000
4+++ scheduler_error_mailer/__init__.py 2013-09-30 22:06:15 +0000
5@@ -0,0 +1,24 @@
6+# -*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# Scheduler Error Mailer module for OpenERP
10+# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
11+# @author: Sébastien Beau <sebastien.beau@akretion.com>
12+# @author Alexis de Lattre <alexis.delattre@akretion.com>
13+#
14+# This program is free software: you can redistribute it and/or modify
15+# it under the terms of the GNU Affero General Public License as
16+# published by the Free Software Foundation, either version 3 of the
17+# License, or (at your option) any later version.
18+#
19+# This program is distributed in the hope that it will be useful,
20+# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+# GNU Affero General Public License for more details.
23+#
24+# You should have received a copy of the GNU Affero General Public License
25+# along with this program. If not, see <http://www.gnu.org/licenses/>.
26+#
27+##############################################################################
28+
29+from . import ir_cron
30
31=== added file 'scheduler_error_mailer/__openerp__.py'
32--- scheduler_error_mailer/__openerp__.py 1970-01-01 00:00:00 +0000
33+++ scheduler_error_mailer/__openerp__.py 2013-09-30 22:06:15 +0000
34@@ -0,0 +1,48 @@
35+# -*- encoding: utf-8 -*-
36+##############################################################################
37+#
38+# Scheduler Error Mailer module for OpenERP
39+# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
40+# @author: Sébastien Beau <sebastien.beau@akretion.com>
41+# @author David Beal <bealdavid@gmail.com>
42+# @author Alexis de Lattre <alexis.delattre@akretion.com>
43+#
44+# This program is free software: you can redistribute it and/or modify
45+# it under the terms of the GNU Affero General Public License as
46+# published by the Free Software Foundation, either version 3 of the
47+# License, or (at your option) any later version.
48+#
49+# This program is distributed in the hope that it will be useful,
50+# but WITHOUT ANY WARRANTY; without even the implied warranty of
51+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52+# GNU Affero General Public License for more details.
53+#
54+# You should have received a copy of the GNU Affero General Public License
55+# along with this program. If not, see <http://www.gnu.org/licenses/>.
56+#
57+##############################################################################
58+
59+
60+{
61+ 'name': 'Scheduler Error Mailer',
62+ 'summary': 'Send an e-mail when a scheduler fails',
63+ 'version': '1.0',
64+ 'category': 'Extra Tools',
65+ 'license': 'AGPL-3',
66+ 'description': """
67+Scheduler Error Mailer
68+======================
69+
70+This module adds the possibility to send an e-mail when a scheduler raises an error.""",
71+ 'author': 'Akretion',
72+ 'website': 'http://www.akretion.com/',
73+ 'depends': ['email_template'],
74+ 'data': [
75+ 'ir_cron.xml',
76+ 'ir_cron_email_tpl.xml',
77+ ],
78+ 'demo': ['ir_cron_demo.xml'],
79+ 'images': ['images/scheduler_error_mailer.jpg'],
80+ 'installable': True,
81+ 'active': False,
82+}
83
84=== added directory 'scheduler_error_mailer/images'
85=== added file 'scheduler_error_mailer/images/scheduler_error_mailer.jpg'
86Binary 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
87=== added file 'scheduler_error_mailer/ir_cron.py'
88--- scheduler_error_mailer/ir_cron.py 1970-01-01 00:00:00 +0000
89+++ scheduler_error_mailer/ir_cron.py 2013-09-30 22:06:15 +0000
90@@ -0,0 +1,70 @@
91+# -*- encoding: utf-8 -*-
92+#################################################################################
93+#
94+# Scheduler Error Mailer module for OpenERP
95+# Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
96+# @author: Sébastien Beau <sebastien.beau@akretion.com>
97+# @author David Beal <bealdavid@gmail.com>
98+# @author Alexis de Lattre <alexis.delattre@akretion.com>
99+#
100+# This program is free software: you can redistribute it and/or modify
101+# it under the terms of the GNU Affero General Public License as
102+# published by the Free Software Foundation, either version 3 of the
103+# License, or (at your option) any later version.
104+#
105+# This program is distributed in the hope that it will be useful,
106+# but WITHOUT ANY WARRANTY; without even the implied warranty of
107+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
108+# GNU Affero General Public License for more details.
109+#
110+# You should have received a copy of the GNU Affero General Public License
111+# along with this program. If not, see <http://www.gnu.org/licenses/>.
112+#
113+##############################################################################
114+
115+from openerp.osv import orm, fields
116+from openerp.tools.translate import _
117+import logging
118+
119+logger = logging.getLogger(__name__)
120+
121+class ir_cron(orm.Model):
122+ _inherit = "ir.cron"
123+
124+ _columns = {
125+ 'email_template': fields.many2one('email.template',
126+ 'Error E-mail Template',
127+ help="Select the email template that will be sent when this scheduler fails."),
128+ }
129+
130+
131+ def _handle_callback_exception(self, cr, uid, model_name, method_name, args, job_id, job_exception):
132+
133+ res = super(ir_cron, self)._handle_callback_exception(cr, uid,
134+ model_name, method_name, args, job_id, job_exception)
135+
136+ my_cron = self.browse(cr, uid, job_id)
137+
138+ if my_cron.email_template:
139+ # we put the job_exception in context to be able to print it inside
140+ # the email template
141+ context = {
142+ 'job_exception': job_exception,
143+ 'dbname': cr.dbname,
144+ }
145+
146+ logger.debug("Sending scheduler error email with context=%s" % context)
147+ self.pool['email.template'].send_mail(cr, uid,
148+ my_cron.email_template.id, my_cron.id, force_send=True,
149+ context=context)
150+
151+ return res
152+
153+
154+class res_users(orm.Model):
155+ _inherit = 'res.users'
156+
157+ def test_scheduler_failure(self, cr, uid, context=None):
158+ """This function is used to test and debug this module"""
159+ raise orm.except_orm(_('Error :'), _("Task failure with UID = %d." % uid))
160+
161
162=== added file 'scheduler_error_mailer/ir_cron.xml'
163--- scheduler_error_mailer/ir_cron.xml 1970-01-01 00:00:00 +0000
164+++ scheduler_error_mailer/ir_cron.xml 2013-09-30 22:06:15 +0000
165@@ -0,0 +1,24 @@
166+<?xml version="1.0" encoding="utf-8"?>
167+<!--
168+ Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
169+ The licence is in the file __openerp__.py
170+-->
171+
172+<openerp>
173+<data>
174+
175+
176+<record id="ir_cron_error_mailer_view" model="ir.ui.view">
177+ <field name="name">ir.cron.error.mailer.form</field>
178+ <field name="model">ir.cron</field>
179+ <field name="inherit_id" ref="base.ir_cron_view"/>
180+ <field name="arch" type="xml">
181+ <field name="doall" position="after">
182+ <field name="email_template" />
183+ </field>
184+ </field>
185+</record>
186+
187+
188+</data>
189+</openerp>
190
191=== added file 'scheduler_error_mailer/ir_cron_demo.xml'
192--- scheduler_error_mailer/ir_cron_demo.xml 1970-01-01 00:00:00 +0000
193+++ scheduler_error_mailer/ir_cron_demo.xml 2013-09-30 22:06:15 +0000
194@@ -0,0 +1,27 @@
195+<?xml version="1.0" encoding="utf-8"?>
196+
197+<!--
198+ Copyright (C) 2013 Akretion (http://www.akretion.com/)
199+ @author Alexis de Lattre <alexis.delattre@akretion.com>
200+ The licence is in the file __openerp__.py
201+-->
202+
203+<openerp>
204+<data noupdate="1">
205+
206+<record id="test_scheduler_error_mailer" model="ir.cron">
207+ <field name="name">Test Scheduler Error Mailer</field>
208+ <field name="active" eval="False"/>
209+ <field name="user_id" ref="base.user_root"/>
210+ <field name="interval_number">1</field>
211+ <field name="interval_type">hours</field>
212+ <field name="numbercall">-1</field> <!-- don't limit the number of calls -->
213+ <field name="doall" eval="True"/>
214+ <field name="email_template" ref="scheduler_error_mailer"/>
215+ <field name="model" eval="'res.users'"/>
216+ <field name="function" eval="'test_scheduler_failure'" />
217+ <field name="args" eval="'()'"/>
218+</record>
219+
220+</data>
221+</openerp>
222
223=== added file 'scheduler_error_mailer/ir_cron_email_tpl.xml'
224--- scheduler_error_mailer/ir_cron_email_tpl.xml 1970-01-01 00:00:00 +0000
225+++ scheduler_error_mailer/ir_cron_email_tpl.xml 2013-09-30 22:06:15 +0000
226@@ -0,0 +1,50 @@
227+<?xml version="1.0" encoding="utf-8"?>
228+<!--
229+ Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
230+ The licence is in the file __openerp__.py
231+-->
232+
233+<openerp>
234+<data noupdate="1">
235+
236+<!-- Error Email template -->
237+<record id="scheduler_error_mailer" model="email.template">
238+ <field name="name">Scheduler Error</field>
239+ <field name="email_from">${object.user_id.user_email or ''}</field>
240+ <field name="email_to">${object.user_id.user_email or ''}</field>
241+ <field name="subject">[DB ${ctx.get('dbname')}] Scheduler '${object.name or ''}' FAILED</field>
242+ <field name="model_id" ref="base.model_ir_cron"/>
243+ <field name="auto_delete" eval="True"/>
244+ <field name="body_html"><![CDATA[
245+<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
246+
247+<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>
248+
249+<strong>
250+${ctx.get('job_exception') and ctx.get('job_exception').value or 'Failed to get the error message from the context.'}
251+</strong>
252+
253+<p>You may check the logs of the OpenERP server to get more information about this failure.</p>
254+
255+<p>Properties of the scheduler <em>${object.name or ''}</em> :</p>
256+<ul>
257+<li>Model : ${object.model or ''}</li>
258+<li>Method : ${object.function or ''}</li>
259+<li>Arguments : ${object.args or ''}</li>
260+<li>Interval : ${object.interval_number or '0'} ${object.interval_type or ''}</li>
261+<li>Number of calls : ${object.numbercall or '0'}</li>
262+<li>Repeat missed : ${object.doall}</li>
263+<li>User : ${object.user_id.name or ''}</li>
264+</ul>
265+
266+<p>
267+-- <br/>
268+Automatic e-mail sent by OpenERP. Do not reply.<br/>
269+Database : ${ctx.get('dbname')}
270+</p>
271+</div>
272+ ]]></field>
273+</record>
274+
275+</data>
276+</openerp>
277
278=== added directory 'scheduler_error_mailer/static'
279=== added directory 'scheduler_error_mailer/static/src'
280=== added directory 'scheduler_error_mailer/static/src/img'
281=== added file 'scheduler_error_mailer/static/src/img/icon.png'
282Binary 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