Coverage for ingadhoc-odoo-saas / saas_client / patches.py: 40%
18 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:22 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:22 +0000
1import logging
3from odoo.addons.base.models.assetsbundle import AssetsBundle
4from odoo.exceptions import ValidationError
6_logger = logging.getLogger(__name__)
9# Keep original method
10_old_save_attachment = AssetsBundle.save_attachment
13def _locked_save_attachment(self, extension, content):
14 """Protects concurrent asset generation with a PG lock.
16 :param extension: extension of the bundle to be recorded
17 :param content: bundle content to be recorded
19 :return the ir.attachment records for a given bundle.
20 """
21 dbname = self.env.cr.dbname
22 bundle_name = self.name or "unknown"
23 key = f"{dbname}:{bundle_name}"
24 _logger.info("Locking bundle generation: %s", key)
26 # Block concurrent asset generation at transaction level using current Odoo cursor
27 self.env.cr.execute("SELECT pg_try_advisory_xact_lock(hashtext(%s))", [key])
28 row = self.env.cr.fetchone()
29 if row is None or not row[0]:
30 _logger.warning("Another process is already generating assets for: %s", key)
31 raise ValidationError(f"Could not acquire advisory lock for asset bundle generation: {key}")
33 return _old_save_attachment(self, extension, content)
36# Method replacement
37AssetsBundle.save_attachment = _locked_save_attachment
38_logger.info("Patched AssetsBundle.save_attachment() with advisory lock")