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.

68 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # OpenERP, Open Source Management Solution
  3. # This module copyright (C) 2013 Therp BV (<http://therp.nl>)
  4. # Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A.
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. import logging
  19. from openerp import _, api, exceptions, models, SUPERUSER_ID
  20. from openerp.tools.safe_eval import safe_eval
  21. _logger = logging.getLogger(__name__)
  22. class Cron(models.Model):
  23. _name = _inherit = "ir.cron"
  24. @api.one
  25. def run_manually(self):
  26. """Run a job from the cron form view."""
  27. if self.env.uid != SUPERUSER_ID and (not self.active or
  28. not self.numbercall):
  29. raise exceptions.AccessError(
  30. _('Only the admin user is allowed to '
  31. 'execute inactive cron jobs manually'))
  32. _logger.warn(self.__dict__)
  33. try:
  34. # Try to grab an exclusive lock on the job row
  35. # until the end of the transaction
  36. self.env.cr.execute(
  37. """SELECT *
  38. FROM ir_cron
  39. WHERE id=%s
  40. FOR UPDATE NOWAIT""",
  41. (self.id,),
  42. log_exceptions=False)
  43. except Exception as e:
  44. # User friendly error if the lock could not be claimed
  45. if getattr(e, "pgcode", None) == '55P03':
  46. raise exceptions.Warning(
  47. _('Another process/thread is already busy '
  48. 'executing this job'))
  49. raise
  50. _logger.info('Job `%s` triggered from form', self.name)
  51. # Execute the cron job
  52. method = getattr(self.sudo(self.user_id).env[self.model],
  53. self.function)
  54. args = safe_eval('tuple(%s)' % (self.args or ''))
  55. method(*args)