diff --git a/edi_oca/models/__init__.py b/edi_oca/models/__init__.py index f40b0abe1e5..db0d1b9cf0a 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 24f35f320ff..73ab5b74300 100644 --- a/edi_oca/models/edi_backend.py +++ b/edi_oca/models/edi_backend.py @@ -415,6 +415,7 @@ def _check_output_exchange_sync( ) for rec in new_records: job1 = rec.delayable().action_exchange_generate() + rec.is_queued = True if not skip_send: # Chain send job. # Raise prio to max to send the record out as fast as possible. @@ -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)) @@ -628,6 +630,7 @@ def _check_input_exchange_sync(self, record_ids=None, **kw): ) for rec in pending_records: rec.with_delay().action_exchange_receive() + rec.is_queued = True pending_process_records = self.exchange_record_model.search( self._input_pending_process_records_domain(record_ids=record_ids) @@ -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 13b3c943224..4c11d3ec042 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 00000000000..3bccbf9bace --- /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)