Skip to content

Commit d6c907a

Browse files
committed
[IMP] queue_job: use state constant in lock function
1 parent 886ee8f commit d6c907a

File tree

5 files changed

+64
-19
lines changed

5 files changed

+64
-19
lines changed

queue_job/job.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ def lock(self) -> bool:
261261
queue_job
262262
WHERE
263263
uuid = %s
264-
AND state='started'
264+
AND state = %s
265265
)
266266
FOR UPDATE SKIP LOCKED;
267267
""",
268-
[self.uuid],
268+
[self.uuid, STARTED],
269269
)
270270

271271
# 1 job should be locked

test_queue_job/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from . import test_acquire_job
12
from . import test_autovacuum
23
from . import test_delayable
34
from . import test_dependencies

test_queue_job/tests/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ def _create_job(self):
2020
stored = Job.db_records_from_uuids(self.env, [test_job.uuid])
2121
self.assertEqual(len(stored), 1)
2222
return stored
23+
24+
def _get_demo_job(self, uuid):
25+
# job created during load of demo data
26+
job = self.env["queue.job"].search([("uuid", "=", uuid)], limit=1)
27+
self.assertTrue(
28+
job,
29+
f"Demo data queue job {uuid!r} should be loaded in order "
30+
"to make this test work",
31+
)
32+
return job
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2026 ACSONE SA/NV
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
import logging
4+
from unittest import mock
5+
6+
from odoo.tests import tagged
7+
8+
from odoo.addons.queue_job.controllers.main import RunJobController
9+
10+
from .common import JobCommonCase
11+
12+
13+
@tagged("post_install", "-at_install")
14+
class TestRequeueDeadJob(JobCommonCase):
15+
def test_acquire_enqueued_job(self):
16+
job_record = self._get_demo_job(uuid="test_enqueued_job")
17+
self.assertFalse(
18+
self.env["queue.job.lock"].search(
19+
[("queue_job_id", "=", job_record.id)],
20+
),
21+
"A job lock record should not exist at this point",
22+
)
23+
with mock.patch.object(
24+
self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all)
25+
) as mock_commit:
26+
job = RunJobController._acquire_job(self.env, job_uuid="test_enqueued_job")
27+
mock_commit.assert_called_once()
28+
self.assertIsNotNone(job)
29+
self.assertEqual(job.uuid, "test_enqueued_job")
30+
self.assertEqual(job.state, "started")
31+
self.assertTrue(
32+
self.env["queue.job.lock"].search(
33+
[("queue_job_id", "=", job_record.id)]
34+
),
35+
"A job lock record should exist at this point",
36+
)
37+
38+
def test_acquire_started_job(self):
39+
with (
40+
mock.patch.object(
41+
self.env.cr, "commit", mock.Mock(side_effect=self.env.flush_all)
42+
) as mock_commit,
43+
self.assertLogs(level=logging.WARNING) as logs,
44+
):
45+
job = RunJobController._acquire_job(self.env, "test_started_job")
46+
mock_commit.assert_not_called()
47+
self.assertIsNone(job)
48+
self.assertIn(
49+
"was requested to run job test_started_job, but it does not exist",
50+
logs.output[0],
51+
)

test_queue_job/tests/test_requeue_dead_job.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@
1313

1414
@tagged("post_install", "-at_install")
1515
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-
3316
def get_locks(self, uuid, cr=None):
3417
"""
3518
Retrieve lock rows

0 commit comments

Comments
 (0)