|
| 1 | +# Copyright 2025 ACSONE SA/NV |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +from contextlib import closing |
| 4 | +from datetime import datetime, timedelta |
| 5 | + |
| 6 | +from odoo.tests import tagged |
| 7 | + |
| 8 | +from odoo.addons.queue_job.job import Job |
| 9 | +from odoo.addons.queue_job.jobrunner.runner import Database |
| 10 | + |
| 11 | +from .common import JobCommonCase |
| 12 | + |
| 13 | + |
| 14 | +@tagged("post_install", "-at_install") |
| 15 | +class TestRequeueDeadJob(JobCommonCase): |
| 16 | + def _get_demo_job(self, uuid): |
| 17 | + # job created during load of demo data |
| 18 | + job = self.env["queue.job"].search( |
| 19 | + [ |
| 20 | + ("uuid", "=", uuid), |
| 21 | + ], |
| 22 | + limit=1, |
| 23 | + ) |
| 24 | + |
| 25 | + self.assertTrue( |
| 26 | + job, |
| 27 | + f"Demo data queue job {uuid} should be loaded in order" |
| 28 | + " to make this tests work", |
| 29 | + ) |
| 30 | + |
| 31 | + return job |
| 32 | + |
| 33 | + def get_locks(self, uuid, cr=None): |
| 34 | + """ |
| 35 | + Retrieve lock rows |
| 36 | + """ |
| 37 | + if cr is None: |
| 38 | + cr = self.env.cr |
| 39 | + |
| 40 | + cr.execute( |
| 41 | + """ |
| 42 | + SELECT |
| 43 | + queue_job_id |
| 44 | + FROM |
| 45 | + queue_job_lock |
| 46 | + WHERE |
| 47 | + queue_job_id IN ( |
| 48 | + SELECT |
| 49 | + id |
| 50 | + FROM |
| 51 | + queue_job |
| 52 | + WHERE |
| 53 | + uuid = %s |
| 54 | + ) |
| 55 | + FOR UPDATE SKIP LOCKED |
| 56 | + """, |
| 57 | + [uuid], |
| 58 | + ) |
| 59 | + |
| 60 | + return cr.fetchall() |
| 61 | + |
| 62 | + def test_add_lock_record(self): |
| 63 | + queue_job = self._get_demo_job("test_started_job") |
| 64 | + self.assertEqual(len(queue_job), 1) |
| 65 | + job_obj = Job.load(self.env, queue_job.uuid) |
| 66 | + |
| 67 | + job_obj.set_started() |
| 68 | + self.assertEqual(job_obj.state, "started") |
| 69 | + |
| 70 | + locks = self.get_locks(job_obj.uuid) |
| 71 | + |
| 72 | + self.assertEqual(1, len(locks)) |
| 73 | + |
| 74 | + def test_lock(self): |
| 75 | + queue_job = self._get_demo_job("test_started_job") |
| 76 | + job_obj = Job.load(self.env, queue_job.uuid) |
| 77 | + |
| 78 | + job_obj.set_started() |
| 79 | + job_obj.lock() |
| 80 | + |
| 81 | + with closing(self.env.registry.cursor()) as new_cr: |
| 82 | + locks = self.get_locks(job_obj.uuid, new_cr) |
| 83 | + |
| 84 | + # Row should be locked |
| 85 | + self.assertEqual(0, len(locks)) |
| 86 | + |
| 87 | + def test_requeue_dead_jobs(self): |
| 88 | + queue_job = self._get_demo_job("test_enqueued_job") |
| 89 | + job_obj = Job.load(self.env, queue_job.uuid) |
| 90 | + |
| 91 | + job_obj.set_enqueued() |
| 92 | + job_obj.set_started() |
| 93 | + job_obj.date_enqueued = datetime.now() - timedelta(minutes=1) |
| 94 | + job_obj.store() |
| 95 | + |
| 96 | + # requeue dead jobs using current cursor |
| 97 | + query = Database(self.env.cr.dbname)._query_requeue_dead_jobs() |
| 98 | + self.env.cr.execute(query) |
| 99 | + |
| 100 | + uuids_requeued = self.env.cr.fetchall() |
| 101 | + self.assertTrue(queue_job.uuid in j[0] for j in uuids_requeued) |
0 commit comments