Skip to content

Commit

Permalink
edi: use job identity_key
Browse files Browse the repository at this point in the history
Prevents having duplicated jobs for the same record as far as possible.
  • Loading branch information
simahawk committed Nov 27, 2023
1 parent 3ff3f41 commit e9c1ae8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from odoo import _, api, exceptions, fields, models

from ..utils import get_checksum
from ..utils import exchange_record_job_identity_exact, get_checksum

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -579,6 +579,8 @@ def _job_delay_params(self):
channel = self.type_id.sudo().job_channel_id
if channel:
params["channel"] = channel.complete_name
# Avoid generating the same job for the same record if existing
params["identity_key"] = exchange_record_job_identity_exact
return params

def with_delay(self, **kw):
Expand Down
12 changes: 12 additions & 0 deletions edi_oca/tests/test_backend_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ def test_job(self):
trap.assert_enqueued_job(
self.backend.exchange_record_model.action_exchange_generate,
)
trap.assert_enqueued_job(
self.backend.exchange_record_model.action_exchange_send,
)
self.backend._check_output_exchange_sync(record_ids=self.record.ids)
self.backend._check_output_exchange_sync(record_ids=self.record.ids)
self.backend._check_output_exchange_sync(record_ids=self.record.ids)
# identity key should prevent having new jobs
trap.assert_jobs_count(2)
self.record._set_file_content("something different")
self.backend._check_output_exchange_sync(record_ids=self.record.ids)
# identity key should prevent having new jobs
trap.assert_jobs_count(2)
10 changes: 10 additions & 0 deletions edi_oca/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hashlib

from odoo.addons.http_routing.models.ir_http import slugify
from odoo.addons.queue_job.job import identity_exact_hasher


def normalize_string(a_string, sep="_"):
Expand All @@ -14,3 +15,12 @@ def normalize_string(a_string, sep="_"):

def get_checksum(filecontent):
return hashlib.md5(filecontent).hexdigest()


def exchange_record_job_identity_exact(job_):
hasher = identity_exact_hasher(job_)
# Include files checksum
hasher.update(
str(sorted(job_.recordset.mapped("exchange_filechecksum"))).encode("utf-8")
)
return hasher.hexdigest()

0 comments on commit e9c1ae8

Please sign in to comment.