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

1import logging 

2 

3from odoo.addons.base.models.assetsbundle import AssetsBundle 

4from odoo.exceptions import ValidationError 

5 

6_logger = logging.getLogger(__name__) 

7 

8 

9# Keep original method 

10_old_save_attachment = AssetsBundle.save_attachment 

11 

12 

13def _locked_save_attachment(self, extension, content): 

14 """Protects concurrent asset generation with a PG lock. 

15 

16 :param extension: extension of the bundle to be recorded 

17 :param content: bundle content to be recorded 

18 

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) 

25 

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}") 

32 

33 return _old_save_attachment(self, extension, content) 

34 

35 

36# Method replacement 

37AssetsBundle.save_attachment = _locked_save_attachment 

38_logger.info("Patched AssetsBundle.save_attachment() with advisory lock")