diff --git a/web_assets_warmup/hooks.py b/web_assets_warmup/hooks.py index 0106883b3..a71a533e7 100644 --- a/web_assets_warmup/hooks.py +++ b/web_assets_warmup/hooks.py @@ -1,9 +1,12 @@ # Copyright 2020 Camptocamp SA +# Copyright 2023 Michael Tietz (MT Software) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) import logging import os +import psycopg2 + from odoo import fields, registry logger = logging.getLogger(__name__) @@ -32,20 +35,35 @@ def active_cron_assets(): model, res_id = row if model != "ir.cron": return - query = """ - UPDATE ir_cron - SET active=true, nextcall=%s, priority=%s - WHERE id=%s - """ - nextcall = fields.Datetime.to_string(fields.Datetime.now()) - args = (nextcall, -99, res_id) - cr.execute(query, args) - logger.info( - "Cron '%s.%s' planned for execution at %s", - cron_module, - cron_ref, - nextcall, - ) + # if there is already someone doing the same or already being executed + # we can skip the update of ir_cron + try: + with cr.savepoint(): + cr.execute( + "SELECT * FROM ir_cron WHERE id = %s FOR UPDATE NOWAIT;", + (res_id,), + ) + query = """ + UPDATE ir_cron + SET active=true, nextcall=%s, priority=%s + WHERE id=%s + """ + nextcall = fields.Datetime.to_string(fields.Datetime.now()) + args = (nextcall, -99, res_id) + cr.execute(query, args) + logger.info( + "Cron '%s.%s' planned for execution at %s", + cron_module, + cron_ref, + nextcall, + ) + except psycopg2.OperationalError as e: + if e.pgcode == "55P03": + logger.info( + "Cron '%s.%s' is currently being executed or updated", + cron_module, + cron_ref, + ) def post_load_hook(): diff --git a/web_assets_warmup/readme/CONTRIBUTORS.rst b/web_assets_warmup/readme/CONTRIBUTORS.rst index c452804a9..d5a5b126e 100644 --- a/web_assets_warmup/readme/CONTRIBUTORS.rst +++ b/web_assets_warmup/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ * Sébastien Alix +* Michael Tietz (MT Software)