|
4 | 4 |
|
5 | 5 | from odoo import api, fields, models |
6 | 6 |
|
| 7 | +from odoo.addons.queue_job.job import identity_exact |
| 8 | + |
7 | 9 | _logger = logging.getLogger(__name__) |
8 | 10 |
|
9 | 11 |
|
10 | 12 | class IrCron(models.Model): |
11 | 13 | _inherit = "ir.cron" |
12 | 14 |
|
| 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 | + |
13 | 24 | run_as_queue_job = fields.Boolean( |
14 | 25 | help="Specify if this cron should be ran as a queue job" |
15 | 26 | ) |
@@ -42,23 +53,29 @@ def method_direct_trigger(self): |
42 | 53 | _cron = cron.with_user(cron.user_id).with_context( |
43 | 54 | lastcall=cron.lastcall |
44 | 55 | ) |
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 | + ) |
50 | 59 | return True |
51 | 60 |
|
52 | 61 | def _callback(self, cron_name, server_action_id, job_id): |
53 | 62 | cron = self.env["ir.cron"].sudo().browse(job_id) |
54 | 63 | if cron.run_as_queue_job: |
55 | 64 | 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) |
61 | 66 | else: |
62 | 67 | return super()._callback( |
63 | 68 | cron_name=cron_name, server_action_id=server_action_id, job_id=job_id |
64 | 69 | ) |
| 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) |
0 commit comments