From 95f411da96ac9fca83c0482086305320bf1fafb6 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 12 Aug 2013 20:58:33 +0200 Subject: [PATCH 1/2] [ADD] cron_run_manually --- cron_run_manually/__init__.py | 1 + cron_run_manually/__openerp__.py | 36 ++++++++++ cron_run_manually/i18n/cron_run_manually.pot | 39 +++++++++++ cron_run_manually/i18n/nl.po | 38 ++++++++++ cron_run_manually/model/__init__.py | 1 + cron_run_manually/model/ir_cron.py | 73 ++++++++++++++++++++ cron_run_manually/view/ir_cron.xml | 20 ++++++ 7 files changed, 208 insertions(+) create mode 100644 cron_run_manually/__init__.py create mode 100644 cron_run_manually/__openerp__.py create mode 100644 cron_run_manually/i18n/cron_run_manually.pot create mode 100644 cron_run_manually/i18n/nl.po create mode 100644 cron_run_manually/model/__init__.py create mode 100644 cron_run_manually/model/ir_cron.py create mode 100644 cron_run_manually/view/ir_cron.xml diff --git a/cron_run_manually/__init__.py b/cron_run_manually/__init__.py new file mode 100644 index 000000000..16e8b082f --- /dev/null +++ b/cron_run_manually/__init__.py @@ -0,0 +1 @@ +import model diff --git a/cron_run_manually/__openerp__.py b/cron_run_manually/__openerp__.py new file mode 100644 index 000000000..9fa6ed24a --- /dev/null +++ b/cron_run_manually/__openerp__.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# 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. +# +# 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 . +# +############################################################################## + +{ + 'name': 'Call cron jobs from their form view', + 'version': '0.1', + 'author': ["Therp BV", "OpenERP S.A."], + 'license': 'AGPL-3', + 'category': 'Tools', + 'description': """ +This module adds a button to the cron scheduled task form in OpenERP +that allows the administrator to run the job immediately, independently +of the scheduler. +""", + 'depends': ['base'], + 'data': ['view/ir_cron.xml'], + } diff --git a/cron_run_manually/i18n/cron_run_manually.pot b/cron_run_manually/i18n/cron_run_manually.pot new file mode 100644 index 000000000..fcf657e9b --- /dev/null +++ b/cron_run_manually/i18n/cron_run_manually.pot @@ -0,0 +1,39 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * cron_run_manually +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2013-08-12 18:26+0000\n" +"PO-Revision-Date: 2013-08-12 18:26+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:71 +#, python-format +msgid "Another process/thread is already busy executing this job" +msgstr "Another process/thread is already busy executing this job" + +#. module: cron_run_manually +#: view:ir.cron:0 +msgid "Run now" +msgstr "Run now" + +#. module: cron_run_manually +#: model:ir.model,name:cron_run_manually.model_ir_cron +msgid "ir.cron" +msgstr "ir.cron" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:70 +#, python-format +msgid "Error" +msgstr "Error" + diff --git a/cron_run_manually/i18n/nl.po b/cron_run_manually/i18n/nl.po new file mode 100644 index 000000000..257e1b058 --- /dev/null +++ b/cron_run_manually/i18n/nl.po @@ -0,0 +1,38 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * cron_run_manually +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2013-08-12 18:26+0000\n" +"PO-Revision-Date: 2013-08-12 18:26+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:71 +#, python-format +msgid "Another process/thread is already busy executing this job" +msgstr "Deze job wordt op dit moment al uitgevoerd door een ander proces" + +#. module: cron_run_manually +#: view:ir.cron:0 +msgid "Run now" +msgstr "Nu uitvoeren" + +#. module: cron_run_manually +#: model:ir.model,name:cron_run_manually.model_ir_cron +msgid "ir.cron" +msgstr "ir.cron" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:70 +#, python-format +msgid "Error" +msgstr "Fout" diff --git a/cron_run_manually/model/__init__.py b/cron_run_manually/model/__init__.py new file mode 100644 index 000000000..452b62ec4 --- /dev/null +++ b/cron_run_manually/model/__init__.py @@ -0,0 +1 @@ +import ir_cron diff --git a/cron_run_manually/model/ir_cron.py b/cron_run_manually/model/ir_cron.py new file mode 100644 index 000000000..9949be86c --- /dev/null +++ b/cron_run_manually/model/ir_cron.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# 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. +# +# 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 . +# +############################################################################## + +import psycopg2 +import logging +from openerp.osv import orm +from openerp.tools.translate import _ +from openerp.tools.safe_eval import safe_eval + +class irCron(orm.Model): + _inherit = 'ir.cron' + + 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() + + for job in jobs: + 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) + + # 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) + + except psycopg2.OperationalError, 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 + + return True diff --git a/cron_run_manually/view/ir_cron.xml b/cron_run_manually/view/ir_cron.xml new file mode 100644 index 000000000..e25d24afd --- /dev/null +++ b/cron_run_manually/view/ir_cron.xml @@ -0,0 +1,20 @@ + + + + + + ir.cron + + + +