diff --git a/queue_job_batch/models/queue_job.py b/queue_job_batch/models/queue_job.py index 524da23ef..58d2fcc6c 100644 --- a/queue_job_batch/models/queue_job.py +++ b/queue_job_batch/models/queue_job.py @@ -25,7 +25,7 @@ def write(self, vals): for record in self: if record.state != "done" and record.job_batch_id: batches |= record.job_batch_id - for batch in batches: + for batch in batches.with_context(job_batch=None): # We need to make it with delay in order to prevent two jobs # to work with the same batch batch.with_delay(identity_key=identity_exact).check_state() diff --git a/queue_job_batch/models/queue_job_batch.py b/queue_job_batch/models/queue_job_batch.py index a0d084040..0bee9198a 100644 --- a/queue_job_batch/models/queue_job_batch.py +++ b/queue_job_batch/models/queue_job_batch.py @@ -2,9 +2,11 @@ # Copyright 2023 ForgeFlow S.L. (http://www.forgeflow.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + from odoo import api, fields, models from odoo.addons.mail.tools.discuss import Store +from odoo.addons.queue_job.exception import RetryableJobError class QueueJobBatch(models.Model): @@ -75,6 +77,18 @@ def _get_state(self, job_states): return "enqueued" return "pending" + def check_done(self): + if self.job_count != self.finished_job_count + self.failed_job_count: + raise RetryableJobError( + "%s: %d total jobs != %d finished + %d failed" + % ( + self.name, + self.job_count, + self.finished_job_count, + self.failed_job_count, + ) + ) + def check_state(self): grouped = self.env["queue.job"].read_group( [("job_batch_id", "in", self.ids)], diff --git a/queue_job_batch/tests/__init__.py b/queue_job_batch/tests/__init__.py new file mode 100644 index 000000000..39cec4642 --- /dev/null +++ b/queue_job_batch/tests/__init__.py @@ -0,0 +1 @@ +from . import test_queue_job_batch diff --git a/queue_job_batch/tests/test_queue_job_batch.py b/queue_job_batch/tests/test_queue_job_batch.py new file mode 100644 index 000000000..216c7f1a7 --- /dev/null +++ b/queue_job_batch/tests/test_queue_job_batch.py @@ -0,0 +1,37 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests import TransactionCase + +from odoo.addons.queue_job.exception import RetryableJobError + + +class TestJobBatch(TransactionCase): + def setUp(self): + super().setUp() + self.job_batch = self.env["queue.job.batch"].create( + { + "name": "test", + "user_id": self.env.user.id, + } + ) + partners = self.env.ref("base.res_partner_1") + self.env.ref( + "base.res_partner_2" + ) + self.jobs = [ + p.with_context(job_batch=self.job_batch).with_delay()._get_complete_name() + for p in partners + ] + self.assertEqual(len(self.job_batch.job_ids), len(self.jobs)) + + def test_check_done(self): + self.jobs[0].set_started() + self.jobs[0].perform() + self.jobs[0].set_done() + self.jobs[0].store() + with self.assertRaises(RetryableJobError): + self.job_batch.check_done() + self.jobs[1].set_started() + self.jobs[1].perform() + self.jobs[1].set_done() + self.jobs[1].store() + self.job_batch.check_done()