Browse Source
Merge pull request #81 from grupoesoc/cron_run_manually
Merge pull request #81 from grupoesoc/cron_run_manually
Cron run manuallypull/96/head
Pedro M. Baeza
10 years ago
12 changed files with 197 additions and 143 deletions
-
1__unported__/cron_run_manually/__init__.py
-
37__unported__/cron_run_manually/__openerp__.py
-
1__unported__/cron_run_manually/model/__init__.py
-
82__unported__/cron_run_manually/model/ir_cron.py
-
20cron_run_manually/__init__.py
-
37cron_run_manually/__openerp__.py
-
30cron_run_manually/i18n/cron_run_manually.pot
-
36cron_run_manually/i18n/es.po
-
0cron_run_manually/i18n/nl.po
-
73cron_run_manually/ir_cron.py
-
23cron_run_manually/tests/correct_uid.yml
-
0cron_run_manually/view/ir_cron.xml
@ -1 +0,0 @@ |
|||||
import model |
|
@ -1,37 +0,0 @@ |
|||||
# -*- 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. |
|
||||
# |
|
||||
# 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/>. |
|
||||
# |
|
||||
############################################################################## |
|
||||
|
|
||||
{ |
|
||||
'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': False, |
|
||||
} |
|
@ -1 +0,0 @@ |
|||||
import ir_cron |
|
@ -1,82 +0,0 @@ |
|||||
# -*- 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. |
|
||||
# |
|
||||
# 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.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 |
|
@ -0,0 +1,20 @@ |
|||||
|
# -*- 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. |
||||
|
# |
||||
|
# 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/>. |
||||
|
|
||||
|
from . import ir_cron |
@ -0,0 +1,37 @@ |
|||||
|
# -*- 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. |
||||
|
# |
||||
|
# 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/>. |
||||
|
|
||||
|
{ |
||||
|
'name': 'Call cron jobs from their form view', |
||||
|
'version': '1.0', |
||||
|
'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', 'mail'], |
||||
|
'data': ['view/ir_cron.xml'], |
||||
|
"test": [ |
||||
|
"tests/correct_uid.yml", |
||||
|
], |
||||
|
'installable': True, |
||||
|
} |
@ -0,0 +1,36 @@ |
|||||
|
# Translation of Odoo Server. |
||||
|
# This file contains the translation of the following modules: |
||||
|
# * cron_run_manually |
||||
|
# |
||||
|
msgid "" |
||||
|
msgstr "" |
||||
|
"Project-Id-Version: Odoo Server 8.0rc1\n" |
||||
|
"Report-Msgid-Bugs-To: \n" |
||||
|
"POT-Creation-Date: 2014-09-16 09:47+0000\n" |
||||
|
"PO-Revision-Date: 2014-09-16 11:52+0100\n" |
||||
|
"Last-Translator: Jairo Llopis <yajo.sk8@gmail.com>\n" |
||||
|
"Language-Team: Grupo ESOC <informatica@grupoesoc.es>\n" |
||||
|
"MIME-Version: 1.0\n" |
||||
|
"Content-Type: text/plain; charset=UTF-8\n" |
||||
|
"Content-Transfer-Encoding: 8bit\n" |
||||
|
"X-Generator: Poedit 1.5.4\n" |
||||
|
"Language: es\n" |
||||
|
"X-Poedit-SourceCharset: UTF-8\n" |
||||
|
|
||||
|
#. module: cron_run_manually |
||||
|
#: code:addons/cron_run_manually/ir_cron.py:57 |
||||
|
#, python-format |
||||
|
msgid "Another process/thread is already busy executing this job" |
||||
|
msgstr "Hay otro proceso o hilo ejecutando este trabajo en este momento" |
||||
|
|
||||
|
#. module: cron_run_manually |
||||
|
#: code:addons/cron_run_manually/ir_cron.py:38 |
||||
|
#, python-format |
||||
|
msgid "Only the admin user is allowed to execute inactive cron jobs manually" |
||||
|
msgstr "" |
||||
|
"Solo el administrador puede ejecutar manualmente trabajos cron inactivos" |
||||
|
|
||||
|
#. module: cron_run_manually |
||||
|
#: view:ir.cron:cron_run_manually.ir_cron_view |
||||
|
msgid "Run now" |
||||
|
msgstr "Ejecutar ahora" |
@ -0,0 +1,73 @@ |
|||||
|
# -*- 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. |
||||
|
# |
||||
|
# 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 logging |
||||
|
from openerp import _, api, exceptions, models, SUPERUSER_ID |
||||
|
from openerp.tools.safe_eval import safe_eval |
||||
|
from psycopg2 import OperationalError |
||||
|
|
||||
|
_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')) |
||||
|
|
||||
|
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) |
||||
|
|
||||
|
except OperationalError 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')) |
||||
|
|
||||
|
raise |
||||
|
|
||||
|
_logger.info('Job `%s` triggered from form', self.name) |
||||
|
|
||||
|
# Execute the cron job |
||||
|
method = getattr(self.sudo(self.user_id).env[self.model], |
||||
|
self.function) |
||||
|
args = safe_eval('tuple(%s)' % (self.args or '')) |
||||
|
return method(*args) |
||||
|
|
||||
|
@api.model |
||||
|
def _current_uid(self): |
||||
|
"""This function returns the current UID, for testing purposes.""" |
||||
|
|
||||
|
return self.env.uid |
@ -0,0 +1,23 @@ |
|||||
|
- I create a user that will run the job. |
||||
|
- !record {model: res.users, id: worker_user, view: False}: |
||||
|
company_id: base.main_company |
||||
|
name: worker |
||||
|
login: worker |
||||
|
password: worker |
||||
|
email: worker@example.com |
||||
|
|
||||
|
- Create a cron job to check the UID |
||||
|
- !record {model: ir.cron, id: check_uid_job}: |
||||
|
name: Check UID |
||||
|
active: True |
||||
|
user_id: worker_user |
||||
|
interval_number: 1 |
||||
|
interval_type: days |
||||
|
numbercall: -1 |
||||
|
doall: False |
||||
|
model: ir.cron |
||||
|
function: _current_uid |
||||
|
|
||||
|
- I execute the cron job manually to check its running UID |
||||
|
- !python {model: ir.cron, id: check_uid_job}: | |
||||
|
assert self.run_manually()[0] == self.user_id.id, "Wrong UID in cron job" |
Write
Preview
Loading…
Cancel
Save
Reference in new issue