[IMP] base_cron_exclusion: black, isort
parent
97f76e0fed
commit
64bb5e1373
|
@ -5,17 +5,13 @@
|
|||
{
|
||||
"name": "Base Cron Exclusion",
|
||||
"summary": "Allow you to select scheduled actions that should not run "
|
||||
"simultaneously.",
|
||||
"simultaneously.",
|
||||
"version": "12.0.1.0.0",
|
||||
"author": "Eficent, Odoo Community Association (OCA)",
|
||||
"website": "https://github.com/OCA/server-tools",
|
||||
"category": "Tools",
|
||||
"depends": [
|
||||
"base",
|
||||
],
|
||||
"data": [
|
||||
"views/ir_cron_view.xml",
|
||||
],
|
||||
"depends": ["base"],
|
||||
"data": ["views/ir_cron_view.xml"],
|
||||
"license": "LGPL-3",
|
||||
"installable": True,
|
||||
}
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
|
||||
import logging
|
||||
|
||||
from odoo import sql_db
|
||||
from odoo import api, fields, models, _
|
||||
from odoo import _, api, fields, models, sql_db
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
@ -14,49 +13,61 @@ _logger = logging.getLogger(__name__)
|
|||
class IrCron(models.Model):
|
||||
_inherit = "ir.cron"
|
||||
|
||||
@api.constrains('mutually_exclusive_cron_ids')
|
||||
@api.constrains("mutually_exclusive_cron_ids")
|
||||
def _check_auto_exclusion(self):
|
||||
for item in self:
|
||||
if item in item.mutually_exclusive_cron_ids:
|
||||
raise ValidationError(_(
|
||||
"You can not mutually exclude a scheduled actions with "
|
||||
"itself."))
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You can not mutually exclude a scheduled actions with "
|
||||
"itself."
|
||||
)
|
||||
)
|
||||
|
||||
mutually_exclusive_cron_ids = fields.Many2many(
|
||||
comodel_name="ir.cron", relation="ir_cron_exclusion",
|
||||
column1="ir_cron1_id", column2="ir_cron2_id",
|
||||
string="Mutually Exclusive Scheduled Actions")
|
||||
comodel_name="ir.cron",
|
||||
relation="ir_cron_exclusion",
|
||||
column1="ir_cron1_id",
|
||||
column2="ir_cron2_id",
|
||||
string="Mutually Exclusive Scheduled Actions",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _lock_mutually_exclusive_cron(db, job_id):
|
||||
lock_cr = db.cursor()
|
||||
lock_cr.execute("""
|
||||
lock_cr.execute(
|
||||
"""
|
||||
WITH Q1 AS (SELECT ir_cron2_id as cron_id FROM ir_cron_exclusion
|
||||
WHERE ir_cron1_id=%s
|
||||
UNION ALL
|
||||
SELECT ir_cron1_id as cron_id FROM ir_cron_exclusion
|
||||
WHERE ir_cron2_id=%s)
|
||||
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()])
|
||||
if locked_ids:
|
||||
lock_cr.execute("""SELECT *
|
||||
lock_cr.execute(
|
||||
"""SELECT *
|
||||
FROM ir_cron
|
||||
WHERE numbercall != 0
|
||||
AND active
|
||||
AND id IN %s
|
||||
FOR UPDATE NOWAIT""",
|
||||
(locked_ids,), log_exceptions=False)
|
||||
(locked_ids,),
|
||||
log_exceptions=False,
|
||||
)
|
||||
lock_cr.fetchall()
|
||||
return lock_cr
|
||||
|
||||
@classmethod
|
||||
def _process_job(cls, job_cr, job, cron_cr):
|
||||
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:
|
||||
res = super(IrCron, cls)._process_job(job_cr, job, cron_cr)
|
||||
finally:
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue