From a7f0b8c57a3319cf82868ec0606a39c80df4559b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Fri, 14 Jun 2024 12:29:31 +0200 Subject: [PATCH] [IMP] edi_oca: don't consider exchange records if jobs have been created --- edi_oca/models/__init__.py | 1 + edi_oca/models/edi_backend.py | 4 ++++ edi_oca/models/edi_exchange_record.py | 2 ++ edi_oca/models/queue_job.py | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 edi_oca/models/queue_job.py diff --git a/edi_oca/models/__init__.py b/edi_oca/models/__init__.py index f40b0abe1e..db0d1b9cf0 100644 --- a/edi_oca/models/__init__.py +++ b/edi_oca/models/__init__.py @@ -5,3 +5,4 @@ from . import edi_exchange_type from . import edi_exchange_type_rule from . import edi_id_mixin +from . import queue_job diff --git a/edi_oca/models/edi_backend.py b/edi_oca/models/edi_backend.py index 24f35f320f..94f0a0c441 100644 --- a/edi_oca/models/edi_backend.py +++ b/edi_oca/models/edi_backend.py @@ -414,6 +414,7 @@ def _check_output_exchange_sync( len(new_records), ) for rec in new_records: + rec.is_queued = True job1 = rec.delayable().action_exchange_generate() if not skip_send: # Chain send job. @@ -447,6 +448,7 @@ def _output_new_records_domain(self, record_ids=None): ("type_id.direction", "=", "output"), ("edi_exchange_state", "=", "new"), ("exchange_file", "=", False), + ("is_queued", "=", False), ] if record_ids: domain.append(("id", "in", record_ids)) @@ -627,6 +629,7 @@ def _check_input_exchange_sync(self, record_ids=None, **kw): len(pending_records), ) for rec in pending_records: + rec.is_queued = True rec.with_delay().action_exchange_receive() pending_process_records = self.exchange_record_model.search( @@ -645,6 +648,7 @@ def _input_pending_records_domain(self, record_ids=None): ("type_id.direction", "=", "input"), ("edi_exchange_state", "=", "input_pending"), ("exchange_file", "=", False), + ("is_queued", "=", False), ] if record_ids: domain.append(("id", "in", record_ids)) diff --git a/edi_oca/models/edi_exchange_record.py b/edi_oca/models/edi_exchange_record.py index 13b3c94322..4c11d3ec04 100644 --- a/edi_oca/models/edi_exchange_record.py +++ b/edi_oca/models/edi_exchange_record.py @@ -112,6 +112,8 @@ class EDIExchangeRecord(models.Model): compute="_compute_retryable", help="The record state can be rolled back manually in case of failure.", ) + is_queued = fields.Boolean( + help="Technical field to know if a related job has been created already.") _sql_constraints = [ ("identifier_uniq", "unique(identifier)", "The identifier must be unique."), diff --git a/edi_oca/models/queue_job.py b/edi_oca/models/queue_job.py new file mode 100644 index 0000000000..c7608e25ad --- /dev/null +++ b/edi_oca/models/queue_job.py @@ -0,0 +1,22 @@ +# Copyright 2024 ForgeFlow S.L. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +import logging + +from odoo import models + +_logger = logging.getLogger(__name__) + + +class QueueJob(models.Model): + _inherit = 'queue.job' + + def write(self, vals): + for job in self: + records = job.records.exists() + if len(records) != 1 or records._name != "edi.exchange.record": + continue + if (vals.get("state", "") == "failed" and records.is_queued + and job.state != "failed"): + records.is_queued = False + return super().write(vals)