[IMP] base_cron_exclusion: black, isort

pull/2225/head
Lois Rilo 2019-12-18 10:32:18 +01:00 committed by Christopher Ormaza
parent 37e9a888f6
commit 542af12d27
2 changed files with 29 additions and 22 deletions

View File

@ -10,12 +10,8 @@
"author": "Eficent, Odoo Community Association (OCA)", "author": "Eficent, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-tools", "website": "https://github.com/OCA/server-tools",
"category": "Tools", "category": "Tools",
"depends": [ "depends": ["base"],
"base", "data": ["views/ir_cron_view.xml"],
],
"data": [
"views/ir_cron_view.xml",
],
"license": "LGPL-3", "license": "LGPL-3",
"installable": True, "installable": True,
} }

View File

@ -4,8 +4,7 @@
import logging import logging
from odoo import sql_db from odoo import _, api, fields, models, sql_db
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -14,49 +13,61 @@ _logger = logging.getLogger(__name__)
class IrCron(models.Model): class IrCron(models.Model):
_inherit = "ir.cron" _inherit = "ir.cron"
@api.constrains('mutually_exclusive_cron_ids') @api.constrains("mutually_exclusive_cron_ids")
def _check_auto_exclusion(self): def _check_auto_exclusion(self):
for item in self: for item in self:
if item in item.mutually_exclusive_cron_ids: if item in item.mutually_exclusive_cron_ids:
raise ValidationError(_( raise ValidationError(
_(
"You can not mutually exclude a scheduled actions with " "You can not mutually exclude a scheduled actions with "
"itself.")) "itself."
)
)
mutually_exclusive_cron_ids = fields.Many2many( mutually_exclusive_cron_ids = fields.Many2many(
comodel_name="ir.cron", relation="ir_cron_exclusion", comodel_name="ir.cron",
column1="ir_cron1_id", column2="ir_cron2_id", relation="ir_cron_exclusion",
string="Mutually Exclusive Scheduled Actions") column1="ir_cron1_id",
column2="ir_cron2_id",
string="Mutually Exclusive Scheduled Actions",
)
@staticmethod @staticmethod
def _lock_mutually_exclusive_cron(db, job_id): def _lock_mutually_exclusive_cron(db, job_id):
lock_cr = db.cursor() lock_cr = db.cursor()
lock_cr.execute(""" lock_cr.execute(
"""
WITH Q1 AS (SELECT ir_cron2_id as cron_id FROM ir_cron_exclusion WITH Q1 AS (SELECT ir_cron2_id as cron_id FROM ir_cron_exclusion
WHERE ir_cron1_id=%s WHERE ir_cron1_id=%s
UNION ALL UNION ALL
SELECT ir_cron1_id as cron_id FROM ir_cron_exclusion SELECT ir_cron1_id as cron_id FROM ir_cron_exclusion
WHERE ir_cron2_id=%s) WHERE ir_cron2_id=%s)
SELECT * FROM Q1 SELECT * FROM Q1
GROUP BY cron_id;""", (job_id, job_id)) GROUP BY cron_id;""",
(job_id, job_id),
)
locked_ids = tuple([row[0] for row in lock_cr.fetchall()]) locked_ids = tuple([row[0] for row in lock_cr.fetchall()])
if locked_ids: if locked_ids:
lock_cr.execute("""SELECT * lock_cr.execute(
"""SELECT *
FROM ir_cron FROM ir_cron
WHERE numbercall != 0 WHERE numbercall != 0
AND active AND active
AND id IN %s AND id IN %s
FOR UPDATE NOWAIT""", FOR UPDATE NOWAIT""",
(locked_ids,), log_exceptions=False) (locked_ids,),
log_exceptions=False,
)
lock_cr.fetchall() lock_cr.fetchall()
return lock_cr return lock_cr
@classmethod @classmethod
def _process_job(cls, job_cr, job, cron_cr): def _process_job(cls, job_cr, job, cron_cr):
db = sql_db.db_connect(cls.pool._db.dbname) db = sql_db.db_connect(cls.pool._db.dbname)
locked_crons = cls._lock_mutually_exclusive_cron(db, job['id']) locked_crons = cls._lock_mutually_exclusive_cron(db, job["id"])
try: try:
res = super(IrCron, cls)._process_job(job_cr, job, cron_cr) res = super(IrCron, cls)._process_job(job_cr, job, cron_cr)
finally: finally:
locked_crons.close() locked_crons.close()
_logger.debug("released blocks for cron job %s" % job['cron_name']) _logger.debug("released blocks for cron job %s" % job["cron_name"])
return res return res