Skip to content

Commit 894d0f1

Browse files
committed
Merge PR #625 into 14.0
Signed-off-by lmignon
2 parents 806613c + fb16c00 commit 894d0f1

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

queue_job_cron/models/ir_cron.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44

55
from odoo import api, fields, models
66

7+
from odoo.addons.queue_job.job import identity_exact
8+
79
_logger = logging.getLogger(__name__)
810

911

1012
class IrCron(models.Model):
1113
_inherit = "ir.cron"
1214

15+
no_parallel_queue_job_run = fields.Boolean(
16+
help="Avoid parallel run. "
17+
"If the cron job is already running, the new one will be skipped. "
18+
"By default, odoo never runs the same cron job in parallel. This "
19+
"option is therefore set to True by default when job is run as a "
20+
"queue job.",
21+
default=True,
22+
)
23+
1324
run_as_queue_job = fields.Boolean(
1425
help="Specify if this cron should be ran as a queue job"
1526
)
@@ -42,23 +53,29 @@ def method_direct_trigger(self):
4253
_cron = cron.with_user(cron.user_id).with_context(
4354
lastcall=cron.lastcall
4455
)
45-
_cron.with_delay(
46-
priority=_cron.priority,
47-
description=_cron.name,
48-
channel=_cron.channel_id.complete_name,
49-
)._run_job_as_queue_job(server_action=_cron.ir_actions_server_id)
56+
_cron._delay_run_job_as_queue_job(
57+
server_action=_cron.ir_actions_server_id
58+
)
5059
return True
5160

5261
def _callback(self, cron_name, server_action_id, job_id):
5362
cron = self.env["ir.cron"].sudo().browse(job_id)
5463
if cron.run_as_queue_job:
5564
server_action = self.env["ir.actions.server"].browse(server_action_id)
56-
return self.with_delay(
57-
priority=cron.priority,
58-
description=cron.name,
59-
channel=cron.channel_id.complete_name,
60-
)._run_job_as_queue_job(server_action=server_action)
65+
return cron._delay_run_job_as_queue_job(server_action=server_action)
6166
else:
6267
return super()._callback(
6368
cron_name=cron_name, server_action_id=server_action_id, job_id=job_id
6469
)
70+
71+
def _delay_run_job_as_queue_job(self, server_action):
72+
self.ensure_one()
73+
identity_key = None
74+
if self.no_parallel_queue_job_run:
75+
identity_key = identity_exact
76+
return self.with_delay(
77+
priority=self.priority,
78+
description=self.name,
79+
channel=self.channel_id.complete_name,
80+
identity_key=identity_key,
81+
)._run_job_as_queue_job(server_action=server_action)

queue_job_cron/readme/newsfragments/.gitignore

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
By default prevent parallel run of the same cron job when run as queue job.
2+
3+
When a cron job is run by odoo, the odoo runner will prevent parallel run
4+
of the same cron job. Before this change, this was not the case when the
5+
cron job was run as a queue job. A new option is added to the cron job when
6+
run as a queue job to prevent parallel run. This option is set to True by
7+
default. In this way, the behavior is now the same as when the cron job is run
8+
by odoo but you keep the possibility to disable this restriction when run as
9+
a queue job.

queue_job_cron/tests/test_queue_job_cron.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,22 @@ def test_queue_job_cron_run(self):
3939
cron = self.env.ref("queue_job.ir_cron_autovacuum_queue_jobs")
4040
IrCron = self.env["ir.cron"]
4141
IrCron._run_job_as_queue_job(server_action=cron.ir_actions_server_id)
42+
43+
def test_queue_job_no_parallelism(self):
44+
cron = self.env.ref("queue_job.ir_cron_autovacuum_queue_jobs")
45+
default_channel = self.env.ref("queue_job_cron.channel_root_ir_cron")
46+
cron.write(
47+
{
48+
"no_parallel_queue_job_run": True,
49+
"run_as_queue_job": True,
50+
"channel_id": default_channel.id,
51+
}
52+
)
53+
cron.method_direct_trigger()
54+
cron.method_direct_trigger()
55+
nb_jobs = self.env["queue.job"].search_count([("name", "=", cron.name)])
56+
self.assertEqual(nb_jobs, 1)
57+
cron.no_parallel_queue_job_run = False
58+
cron.method_direct_trigger()
59+
nb_jobs = self.env["queue.job"].search_count([("name", "=", cron.name)])
60+
self.assertEqual(nb_jobs, 2)

queue_job_cron/views/ir_cron_view.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<field name="arch" type="xml">
88
<field name="doall" position="after">
99
<field name="run_as_queue_job" />
10+
<field
11+
name="no_parallel_queue_job_run"
12+
attrs="{'invisible': [('run_as_queue_job', '=', False)]}"
13+
/>
1014
<field
1115
name="channel_id"
1216
attrs="{'invisible': [('run_as_queue_job', '=', False)],

0 commit comments

Comments
 (0)