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.6 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. from psycopg2 import OperationalError
  22. _logger = logging.getLogger(__name__)
  23. class Cron(models.Model):
  24. _name = _inherit = "ir.cron"
  25. @api.one
  26. def run_manually(self):
  27. """Run a job from the cron form view."""
  28. if self.env.uid != SUPERUSER_ID and (not self.active or
  29. not self.numbercall):
  30. raise exceptions.AccessError(
  31. _('Only the admin user is allowed to '
  32. 'execute inactive cron jobs manually'))
  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 OperationalError 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. return method(*args)
  56. @api.model
  57. def _current_uid(self):
  58. """This function returns the current UID, for testing purposes."""
  59. return self.env.uid