Browse Source

Merge pull request #81 from grupoesoc/cron_run_manually

Cron run manually
pull/96/head
Pedro M. Baeza 10 years ago
parent
commit
c9f9e1ec27
  1. 1
      __unported__/cron_run_manually/__init__.py
  2. 37
      __unported__/cron_run_manually/__openerp__.py
  3. 1
      __unported__/cron_run_manually/model/__init__.py
  4. 82
      __unported__/cron_run_manually/model/ir_cron.py
  5. 20
      cron_run_manually/__init__.py
  6. 37
      cron_run_manually/__openerp__.py
  7. 30
      cron_run_manually/i18n/cron_run_manually.pot
  8. 36
      cron_run_manually/i18n/es.po
  9. 0
      cron_run_manually/i18n/nl.po
  10. 73
      cron_run_manually/ir_cron.py
  11. 23
      cron_run_manually/tests/correct_uid.yml
  12. 0
      cron_run_manually/view/ir_cron.xml

1
__unported__/cron_run_manually/__init__.py

@ -1 +0,0 @@
import model

37
__unported__/cron_run_manually/__openerp__.py

@ -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
__unported__/cron_run_manually/model/__init__.py

@ -1 +0,0 @@
import ir_cron

82
__unported__/cron_run_manually/model/ir_cron.py

@ -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

20
cron_run_manually/__init__.py

@ -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

37
cron_run_manually/__openerp__.py

@ -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,
}

30
__unported__/cron_run_manually/i18n/cron_run_manually.pot → cron_run_manually/i18n/cron_run_manually.pot

@ -1,13 +1,13 @@
# Translation of OpenERP Server.
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * cron_run_manually
#
msgid ""
msgstr ""
"Project-Id-Version: OpenERP Server 7.0\n"
"Project-Id-Version: Odoo Server 8.0rc1\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"
"POT-Creation-Date: 2014-09-16 09:47+0000\n"
"PO-Revision-Date: 2014-09-16 09:47+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
@ -16,32 +16,18 @@ msgstr ""
"Plural-Forms: \n"
#. module: cron_run_manually
#: code:addons/cron_run_manually/model/ir_cron.py:77
#: code:addons/cron_run_manually/ir_cron.py:57
#, 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
#: 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 ""
#. 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"
#: view:ir.cron:cron_run_manually.ir_cron_view
msgid "Run now"
msgstr ""

36
cron_run_manually/i18n/es.po

@ -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
__unported__/cron_run_manually/i18n/nl.po → cron_run_manually/i18n/nl.po

73
cron_run_manually/ir_cron.py

@ -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

23
cron_run_manually/tests/correct_uid.yml

@ -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"

0
__unported__/cron_run_manually/view/ir_cron.xml → cron_run_manually/view/ir_cron.xml

Loading…
Cancel
Save