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.9 KiB

7 years ago
  1. # Copyright 2012-2013 Akretion Sébastien BEAU,David Beal,Alexis de Lattre
  2. # Copyright 2016 Sodexis
  3. # Copyright 2018 bloopark systems (<http://bloopark.de>)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import _, api, fields, models
  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(self, cron_name, server_action_id, job_id,
  19. job_exception):
  20. res = super(IrCron, self)._handle_callback_exception(cron_name,
  21. server_action_id,
  22. job_id,
  23. job_exception)
  24. my_cron = self.browse(job_id)
  25. if my_cron.email_template_id:
  26. # we put the job_exception in context to be able to print it inside
  27. # the email template
  28. context = {
  29. 'job_exception': job_exception,
  30. 'dbname': self._cr.dbname,
  31. }
  32. _logger.debug(
  33. "Sending scheduler error email with context=%s", context)
  34. self.env['mail.template'].browse(
  35. my_cron.email_template_id.id
  36. ).with_context(context).sudo().send_mail(
  37. my_cron.id, force_send=True)
  38. return res
  39. @api.model
  40. def _test_scheduler_failure(self):
  41. """This function is used to test and debug this module."""
  42. raise UserError(
  43. _("Task failure with UID = %d.") % self._uid)