|
@ -1,6 +1,5 @@ |
|
|
# -*- coding: utf-8 -*- |
|
|
# -*- coding: utf-8 -*- |
|
|
############################################################################## |
|
|
|
|
|
# |
|
|
|
|
|
|
|
|
|
|
|
# OpenERP, Open Source Management Solution |
|
|
# OpenERP, Open Source Management Solution |
|
|
# This module copyright (C) 2013 Therp BV (<http://therp.nl>) |
|
|
# This module copyright (C) 2013 Therp BV (<http://therp.nl>) |
|
|
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. |
|
|
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. |
|
@ -17,66 +16,62 @@ |
|
|
# |
|
|
# |
|
|
# You should have received a copy of the GNU Affero General Public License |
|
|
# 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/>. |
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
# |
|
|
|
|
|
############################################################################## |
|
|
|
|
|
|
|
|
|
|
|
import psycopg2 |
|
|
|
|
|
import logging |
|
|
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 |
|
|
from openerp.tools.safe_eval import safe_eval |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class irCron(orm.Model): |
|
|
|
|
|
_inherit = 'ir.cron' |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
def run_manually(self, cr, uid, ids, context=None): |
|
|
|
|
|
""" |
|
|
|
|
|
Run a job from the cron form view. |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
class Cron(models.Model): |
|
|
|
|
|
_name = _inherit = "ir.cron" |
|
|
|
|
|
|
|
|
for job in jobs: |
|
|
|
|
|
if uid != SUPERUSER_ID and ( |
|
|
|
|
|
not job['active'] or not job['numbercall']): |
|
|
|
|
|
raise orm.except_orm( |
|
|
|
|
|
_('Error'), |
|
|
|
|
|
|
|
|
@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 ' |
|
|
_('Only the admin user is allowed to ' |
|
|
'execute inactive cron jobs manually')) |
|
|
'execute inactive cron jobs manually')) |
|
|
|
|
|
|
|
|
|
|
|
_logger.warn(self.__dict__) |
|
|
try: |
|
|
try: |
|
|
# Try to grab an exclusive lock on the job row |
|
|
# Try to grab an exclusive lock on the job row |
|
|
# until the end of the transaction |
|
|
# until the end of the transaction |
|
|
cr.execute( |
|
|
|
|
|
|
|
|
self.env.cr.execute( |
|
|
"""SELECT * |
|
|
"""SELECT * |
|
|
FROM ir_cron |
|
|
FROM ir_cron |
|
|
WHERE id=%s |
|
|
WHERE id=%s |
|
|
FOR UPDATE NOWAIT""", |
|
|
FOR UPDATE NOWAIT""", |
|
|
(job['id'],), log_exceptions=False) |
|
|
|
|
|
|
|
|
|
|
|
# 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) |
|
|
|
|
|
|
|
|
(self.id,), |
|
|
|
|
|
log_exceptions=False) |
|
|
|
|
|
|
|
|
except psycopg2.OperationalError as e: |
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
# User friendly error if the lock could not be claimed |
|
|
# User friendly error if the lock could not be claimed |
|
|
if e.pgcode == '55P03': |
|
|
|
|
|
raise orm.except_orm( |
|
|
|
|
|
_('Error'), |
|
|
|
|
|
|
|
|
if getattr(e, "pgcode", None) == '55P03': |
|
|
|
|
|
raise exceptions.Warning( |
|
|
_('Another process/thread is already busy ' |
|
|
_('Another process/thread is already busy ' |
|
|
'executing this job')) |
|
|
'executing this job')) |
|
|
|
|
|
|
|
|
raise |
|
|
raise |
|
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
_logger.info('Job `%s` triggered from form', self.name) |
|
|
|
|
|
|
|
|
|
|
|
# Prepare execution |
|
|
|
|
|
method = getattr(self.env[self.model], self.function) |
|
|
|
|
|
args = safe_eval('tuple(%s)' % (self.args or '')) |
|
|
|
|
|
|
|
|
|
|
|
# Hack the UID |
|
|
|
|
|
old_uid = self.env.uid |
|
|
|
|
|
self.env.uid = self.user_id |
|
|
|
|
|
|
|
|
|
|
|
# Execute the cron job |
|
|
|
|
|
try: |
|
|
|
|
|
method(*args) |
|
|
|
|
|
finally: |
|
|
|
|
|
# Revert UID to original |
|
|
|
|
|
self.env.uid = old_uid |