Skip to content

Commit

Permalink
[IMP] edi_oca: don't consider exchange records if jobs have been created
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Jul 19, 2024
1 parent 4c87527 commit 7c00765
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions edi_oca/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import edi_exchange_type
from . import edi_exchange_type_rule
from . import edi_id_mixin
from . import queue_job
6 changes: 5 additions & 1 deletion edi_oca/models/edi_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def _check_output_exchange_sync(
)
for rec in new_records:
job1 = rec.delayable().action_exchange_generate()
rec.initial_job_id = job1
if not skip_send:
# Chain send job.
# Raise prio to max to send the record out as fast as possible.
Expand Down Expand Up @@ -437,6 +438,7 @@ def _output_new_records_domain(self, record_ids=None):
("type_id.direction", "=", "output"),
("edi_exchange_state", "=", "new"),
("exchange_file", "=", False),
("initial_job_id", "=", False),
]
if record_ids:
domain.append(("id", "in", record_ids))
Expand Down Expand Up @@ -613,7 +615,8 @@ def _check_input_exchange_sync(self, record_ids=None, **kw):
len(pending_records),
)
for rec in pending_records:
rec.with_delay().action_exchange_receive()
job = rec.with_delay().action_exchange_receive()
rec.initial_job_id = job

pending_process_records = self.exchange_record_model.search(
self._input_pending_process_records_domain(record_ids=record_ids)
Expand All @@ -631,6 +634,7 @@ def _input_pending_records_domain(self, record_ids=None):
("type_id.direction", "=", "input"),
("edi_exchange_state", "=", "input_pending"),
("exchange_file", "=", False),
("initial_job_id", "=", False),
]
if record_ids:
domain.append(("id", "in", record_ids))
Expand Down
2 changes: 2 additions & 0 deletions edi_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class EDIExchangeRecord(models.Model):
compute="_compute_retryable",
help="The record state can be rolled back manually in case of failure.",
)
initial_job_id = fields.Many2one(
help="Technical field to know if initial job has been created already.")

_sql_constraints = [
("identifier_uniq", "unique(identifier)", "The identifier must be unique."),
Expand Down
34 changes: 34 additions & 0 deletions edi_oca/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 ForgeFlow S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

import logging

from odoo import api, models
from odoo.tools import pycompat

_logger = logging.getLogger(__name__)


class QueueJob(models.Model):
_inherit = 'queue.job'

@api.model_create_multi
def create(self, vals_list):
jobs = super().create(vals_list)
for job, vals in pycompat.izip(jobs, vals_list):
records = job.records.exists()
if len(records) != 1 or records._name != "edi.exchange.record":
continue
if vals.get("state") == "failed" and records.initial_job_id.id == job.id:
records.initial_job_id = False
return jobs

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.initial_job_id.id == job.id
and job.state != "failed"):
records.initial_job_id = False
return super().write(vals)

0 comments on commit 7c00765

Please sign in to comment.