You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

8 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2012-2013 Akretion Sébastien BEAU,David Beal,Alexis de Lattre
  3. # © 2016 Sodexis
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import models, fields, api, _
  6. from odoo.exceptions import UserError
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class IrCron(models.Model):
  10. _inherit = "ir.cron"
  11. email_template_id = fields.Many2one(
  12. comodel_name="mail.template",
  13. string="Error E-mail Template",
  14. help="Select the email template that will be sent when "
  15. "this scheduler fails."
  16. )
  17. @api.model
  18. def _handle_callback_exception(
  19. self, model_name, method_name, args, job_id, job_exception):
  20. res = super(IrCron, self)._handle_callback_exception(
  21. model_name, method_name, args, job_id, job_exception)
  22. my_cron = self.browse(job_id)
  23. if my_cron.email_template_id:
  24. # we put the job_exception in context to be able to print it inside
  25. # the email template
  26. context = {
  27. 'job_exception': job_exception,
  28. 'dbname': self._cr.dbname,
  29. }
  30. _logger.debug(
  31. "Sending scheduler error email with context=%s", context)
  32. self.env['mail.template'].browse(
  33. my_cron.email_template_id.id
  34. ).with_context(context).sudo().send_mail(
  35. my_cron.id, force_send=True)
  36. return res
  37. @api.model
  38. def _test_scheduler_failure(self):
  39. """This function is used to test and debug this module"""
  40. raise UserError(
  41. _("Task failure with UID = %d.") % self._uid)