Skip to content

Commit

Permalink
[IMP] *_auditfile_export: prevent parallel execution for record
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Mar 22, 2023
1 parent 8a9daa0 commit 7408750
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
44 changes: 43 additions & 1 deletion l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from dateutil.rrule import MONTHLY, rrule
from lxml import etree

from odoo import _, api, exceptions, fields, models, modules, release
from odoo import _, api, exceptions, fields, models, modules, registry, release

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -170,6 +170,9 @@ def _compute_fiscalyear_name(self):
"(missing the Unit4 compatibility) just set this flag to False."
)
auditfile_success = fields.Boolean()
in_use_semaphore = fields.Boolean(
help="Make sure no concurrent generations for this record.",
)

@api.model
def default_get(self, fields_list):
Expand Down Expand Up @@ -203,6 +206,7 @@ def _get_auditfile_template(self):
def button_generate(self):
"""Generate, store and validate auditfile."""
tmpdir = mkdtemp()
self._acquire_in_use()
try:
self._generate_audit_file(tmpdir)
except Exception as exception:
Expand All @@ -211,6 +215,44 @@ def button_generate(self):
finally:
# tmpdir also contains uncompressed auditfile.
shutil.rmtree(tmpdir)
self._release_in_use()

def _acquire_in_use(self):
"""Lock record for this by current thread of process.
Use separate cursor, to force immediate visibility of update.
"""
self.ensure_one()
if self.in_use_semaphore:
raise exceptions.UserError(
_("Generation of auditfile for this record is already in progress.")
)
with api.Environment.manage():
with registry(self.env.cr.dbname).cursor() as new_cr:
# Create a new environment with new cursor database
new_env = api.Environment(new_cr, self.env.uid, self.env.context)
# with_env replace original env for this method
self.with_env(new_env).write(
{"in_use_semaphore": True}
) # isolated transaction to commit
new_env.cr.commit() # Don't show a invalid-commit in this case

def _release_in_use(self):
"""Release record for this by current thread of process.
Use separate cursor, to force immediate visibility of update.
Ignore situation that record already is released.
"""
self.ensure_one()
with api.Environment.manage():
with registry(self.env.cr.dbname).cursor() as new_cr:
# Create a new environment with new cursor database
new_env = api.Environment(new_cr, self.env.uid, self.env.context)
# with_env replace original env for this method
self.with_env(new_env).write(
{"in_use_semaphore": False}
) # isolated transaction to commit
new_env.cr.commit() # Don't show a invalid-commit in this case

def _generate_audit_file(self, tmpdir):
"""Generate audit file in directory passed, and then attach it to record."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from lxml import etree

from odoo import fields
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tests.common import Form, TransactionCase
from odoo.tools import mute_logger
Expand Down Expand Up @@ -197,3 +198,15 @@ def test_07_invalid_characters(self):
self.assertTrue(record.date_end)
self.assertTrue(record.date_generated)
self.assertTrue(record.fiscalyear_name)

@mute_logger("odoo.addons.l10n_nl_xaf_auditfile_export.models.xaf_auditfile_export")
def test_08_prevent_parallel_execution(self):
"""Attempt at parallel execution should raise error."""
record = self.env["xaf.auditfile.export"].create({})
self.assertFalse(record.in_use_semaphore)
self._acquire_in_use()
with self.assertRaises(UserError):
self._acquire_in_use()
self.assertTrue(record.in_use_semaphore)
self._release_in_use()
self.assertFalse(record.in_use_semaphore)

0 comments on commit 7408750

Please sign in to comment.