Jairo Llopis
10 years ago
2 changed files with 79 additions and 87 deletions
@ -1,82 +1,77 @@ |
|||
# -*- coding: utf-8 -*- |
|||
############################################################################## |
|||
# |
|||
# OpenERP, Open Source Management Solution |
|||
# This module copyright (C) 2013 Therp BV (<http://therp.nl>) |
|||
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
|
|||
# OpenERP, Open Source Management Solution |
|||
# This module copyright (C) 2013 Therp BV (<http://therp.nl>) |
|||
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
############################################################################## |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
|
|||
import psycopg2 |
|||
import logging |
|||
from openerp.osv import orm |
|||
from openerp.tools import SUPERUSER_ID |
|||
from openerp.tools.translate import _ |
|||
from openerp import _, api, exceptions, models, SUPERUSER_ID |
|||
from openerp.tools.safe_eval import safe_eval |
|||
|
|||
|
|||
class irCron(orm.Model): |
|||
_inherit = 'ir.cron' |
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class Cron(models.Model): |
|||
_name = _inherit = "ir.cron" |
|||
|
|||
@api.one |
|||
def run_manually(self): |
|||
"""Run a job from the cron form view.""" |
|||
|
|||
if self.env.uid != SUPERUSER_ID and (not self.active or |
|||
not self.numbercall): |
|||
raise exceptions.AccessError( |
|||
_('Only the admin user is allowed to ' |
|||
'execute inactive cron jobs manually')) |
|||
|
|||
def run_manually(self, cr, uid, ids, context=None): |
|||
""" |
|||
Run a job from the cron form view. |
|||
_logger.warn(self.__dict__) |
|||
try: |
|||
# Try to grab an exclusive lock on the job row |
|||
# until the end of the transaction |
|||
self.env.cr.execute( |
|||
"""SELECT * |
|||
FROM ir_cron |
|||
WHERE id=%s |
|||
FOR UPDATE NOWAIT""", |
|||
(self.id,), |
|||
log_exceptions=False) |
|||
|
|||
Cut and paste of code snippets from addons/base/ir/ir_cron.py |
|||
""" |
|||
logger = logging.getLogger('cron_run_manually') |
|||
cr.execute( |
|||
""" |
|||
SELECT * from ir_cron |
|||
WHERE id in %s |
|||
""", (tuple(ids),)) |
|||
jobs = cr.dictfetchall() |
|||
except Exception as e: |
|||
# User friendly error if the lock could not be claimed |
|||
if getattr(e, "pgcode", None) == '55P03': |
|||
raise exceptions.Warning( |
|||
_('Another process/thread is already busy ' |
|||
'executing this job')) |
|||
|
|||
for job in jobs: |
|||
if uid != SUPERUSER_ID and ( |
|||
not job['active'] or not job['numbercall']): |
|||
raise orm.except_orm( |
|||
_('Error'), |
|||
_('Only the admin user is allowed to ' |
|||
'execute inactive cron jobs manually')) |
|||
raise |
|||
|
|||
try: |
|||
# Try to grab an exclusive lock on the job row |
|||
# until the end of the transaction |
|||
cr.execute( |
|||
"""SELECT * |
|||
FROM ir_cron |
|||
WHERE id=%s |
|||
FOR UPDATE NOWAIT""", |
|||
(job['id'],), log_exceptions=False) |
|||
_logger.info('Job `%s` triggered from form', self.name) |
|||
|
|||
# Got the lock on the job row, run its code |
|||
logger.debug('Job `%s` triggered from form', job['name']) |
|||
model = self.pool.get(job['model']) |
|||
method = getattr(model, job['function']) |
|||
args = safe_eval('tuple(%s)' % (job['args'] or '')) |
|||
method(cr, job['user_id'], *args) |
|||
# Prepare execution |
|||
method = getattr(self.env[self.model], self.function) |
|||
args = safe_eval('tuple(%s)' % (self.args or '')) |
|||
|
|||
except psycopg2.OperationalError as e: |
|||
# User friendly error if the lock could not be claimed |
|||
if e.pgcode == '55P03': |
|||
raise orm.except_orm( |
|||
_('Error'), |
|||
_('Another process/thread is already busy ' |
|||
'executing this job')) |
|||
raise |
|||
# Hack the UID |
|||
old_uid = self.env.uid |
|||
self.env.uid = self.user_id |
|||
|
|||
return True |
|||
# Execute the cron job |
|||
try: |
|||
method(*args) |
|||
finally: |
|||
# Revert UID to original |
|||
self.env.uid = old_uid |
Write
Preview
Loading…
Cancel
Save
Reference in new issue