From 2f94f006e33c09a1816f6a13b0ca5001e854008a Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Sun, 18 Jan 2015 18:32:14 +0100 Subject: [PATCH] [ADD] cron_run_manually: Tool for allowing run manually a programmed task --- cron_run_manually/__init__.py | 1 + cron_run_manually/__openerp__.py | 37 +++++++++ cron_run_manually/i18n/cron_run_manually.pot | 47 +++++++++++ cron_run_manually/i18n/es.po | 41 ++++++++++ cron_run_manually/i18n/nl.po | 47 +++++++++++ cron_run_manually/model/__init__.py | 1 + cron_run_manually/model/ir_cron.py | 82 ++++++++++++++++++++ cron_run_manually/view/ir_cron.xml | 20 +++++ 8 files changed, 276 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/es.po 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..9186ee3ad --- /dev/null +++ b/cron_run_manually/__init__.py @@ -0,0 +1 @@ +from . import model diff --git a/cron_run_manually/__openerp__.py b/cron_run_manually/__openerp__.py new file mode 100644 index 000000000..c4885f4d5 --- /dev/null +++ b/cron_run_manually/__openerp__.py @@ -0,0 +1,37 @@ +# -*- 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'], + 'installable': True, +} 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..5324df8da --- /dev/null +++ b/cron_run_manually/i18n/cron_run_manually.pot @@ -0,0 +1,47 @@ +# 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: 2014-03-14 17:41+0000\n" +"PO-Revision-Date: 2014-03-14 17:41+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:77 +#, python-format +msgid "Another process/thread is already busy executing this job" +msgstr "" + +#. module: cron_run_manually +#: view:ir.cron:0 +msgid "Run now" +msgstr "" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:52 +#, python-format +msgid "Only the admin user is allowed to execute inactive cron jobs manually" +msgstr "" + +#. module: cron_run_manually +#: model:_description:0 +#: model:ir.model,name:cron_run_manually.model_ir_cron +msgid "ir.cron" +msgstr "" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:51 +#: code:addons/cron_run_manually/model/ir_cron.py:76 +#, python-format +msgid "Error" +msgstr "" + diff --git a/cron_run_manually/i18n/es.po b/cron_run_manually/i18n/es.po new file mode 100644 index 000000000..0b848d864 --- /dev/null +++ b/cron_run_manually/i18n/es.po @@ -0,0 +1,41 @@ +# 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: 2014-03-14 17:41+0000\n" +"PO-Revision-Date: 2014-03-14 17:41+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:77 +#, python-format +msgid "Another process/thread is already busy executing this job" +msgstr "Otro proceso/hilo está ya ocupado ejecutando esta tarea" + +#. module: cron_run_manually +#: view:ir.cron:0 +msgid "Run now" +msgstr "Ejecutar ahora" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:52 +#, python-format +msgid "Only the admin user is allowed to execute inactive cron jobs manually" +msgstr "Sólo el usuario administrador puede ejecutar manualmente tareas inactivas" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:51 +#: code:addons/cron_run_manually/model/ir_cron.py:76 +#, 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..8c29f8920 --- /dev/null +++ b/cron_run_manually/i18n/nl.po @@ -0,0 +1,47 @@ +# 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: 2014-03-14 17:41+0000\n" +"PO-Revision-Date: 2014-01-20 14:09+0000\n" +"Last-Translator: michele \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Launchpad-Export-Date: 2014-05-24 06:46+0000\n" +"X-Generator: Launchpad (build 17017)\n" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/model/ir_cron.py:77 +#, 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 +#: code:addons/cron_run_manually/model/ir_cron.py:52 +#, python-format +msgid "Only the admin user is allowed to execute inactive cron jobs manually" +msgstr "" + +#. module: cron_run_manually +#: model:_description:0 +#: 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:51 +#: code:addons/cron_run_manually/model/ir_cron.py:76 +#, 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..911559262 --- /dev/null +++ b/cron_run_manually/model/__init__.py @@ -0,0 +1 @@ +from . 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..03ba21d78 --- /dev/null +++ b/cron_run_manually/model/ir_cron.py @@ -0,0 +1,82 @@ +# -*- 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 import SUPERUSER_ID +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: + 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')) + + 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 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 + + 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 + + + +