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.

81 lines
3.1 KiB

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