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.

73 lines
2.5 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. try:
  33. # Try to grab an exclusive lock on the job row
  34. # until the end of the transaction
  35. self.env.cr.execute(
  36. """SELECT *
  37. FROM ir_cron
  38. WHERE id=%s
  39. FOR UPDATE NOWAIT""",
  40. (self.id,),
  41. log_exceptions=False)
  42. except Exception as e:
  43. # User friendly error if the lock could not be claimed
  44. if getattr(e, "pgcode", None) == '55P03':
  45. raise exceptions.Warning(
  46. _('Another process/thread is already busy '
  47. 'executing this job'))
  48. raise
  49. _logger.info('Job `%s` triggered from form', self.name)
  50. # Execute the cron job
  51. method = getattr(self.sudo(self.user_id).env[self.model],
  52. self.function)
  53. args = safe_eval('tuple(%s)' % (self.args or ''))
  54. return method(*args)
  55. @api.model
  56. def _current_uid(self):
  57. """This function returns the current UID, for testing purposes."""
  58. return self.env.uid