Skip to content

Commit 5991cd6

Browse files
[FIX] queue_job_cron_jobrunner: use priority to select job
1 parent 2e0ed2c commit 5991cd6

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

queue_job_cron_jobrunner/models/queue_job.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _acquire_one_job(self):
3636
FROM queue_job
3737
WHERE state = 'pending'
3838
AND (eta IS NULL OR eta <= (now() AT TIME ZONE 'UTC'))
39-
ORDER BY date_created DESC
39+
ORDER BY priority, date_created
4040
LIMIT 1 FOR NO KEY UPDATE SKIP LOCKED
4141
"""
4242
)
@@ -55,7 +55,7 @@ def _process(self, commit=False):
5555
# while the job is processing. However, doing this will release the
5656
# lock on the db, so we need to find another way.
5757
# if commit:
58-
# self.flush()
58+
# self.env.flush_all()
5959
# self.env.cr.commit()
6060

6161
# Actual processing

queue_job_cron_jobrunner/tests/test_queue_job.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,33 @@ def test_queue_job_cron_trigger_enqueue_dependencies(self):
6767

6868
self.assertEqual(job_record.state, "done", "Processed OK")
6969
# if the state is "waiting_dependencies", it means the "enqueue_waiting()"
70-
# step has not been doen when the parent job has been done
70+
# step has not been done when the parent job has been done
7171
self.assertEqual(job_record_depends.state, "done", "Processed OK")
72+
73+
def test_acquire_one_job_use_priority(self):
74+
with freeze_time("2024-01-01 10:01:01"):
75+
self.env["res.partner"].with_delay(priority=3).create({"name": "test"})
76+
77+
with freeze_time("2024-01-01 10:02:01"):
78+
job = (
79+
self.env["res.partner"].with_delay(priority=1).create({"name": "test"})
80+
)
81+
82+
with freeze_time("2024-01-01 10:03:01"):
83+
self.env["res.partner"].with_delay(priority=2).create({"name": "test"})
84+
85+
self.assertEqual(self.env["queue.job"]._acquire_one_job(), job.db_record())
86+
87+
def test_acquire_one_job_consume_the_oldest_first(self):
88+
with freeze_time("2024-01-01 10:01:01"):
89+
job = (
90+
self.env["res.partner"].with_delay(priority=30).create({"name": "test"})
91+
)
92+
93+
with freeze_time("2024-01-01 10:02:01"):
94+
self.env["res.partner"].with_delay(priority=30).create({"name": "test"})
95+
96+
with freeze_time("2024-01-01 10:03:01"):
97+
self.env["res.partner"].with_delay(priority=30).create({"name": "test"})
98+
99+
self.assertEqual(self.env["queue.job"]._acquire_one_job(), job.db_record())

0 commit comments

Comments
 (0)