Skip to content

Commit

Permalink
[IMP] queue_job: Adapt code after backporting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis committed Sep 10, 2024
1 parent 7c1064f commit 77d6da1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions queue_job/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"views/queue_job_function_views.xml",
"wizards/queue_jobs_to_done_views.xml",
"wizards/queue_requeue_job_views.xml",
"wizards/queue_jobs_to_cancelled_views.xml",
"views/queue_job_menus.xml",
"data/queue_data.xml",
"data/queue_job_function_data.xml",
Expand Down
2 changes: 1 addition & 1 deletion queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2013-2020 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

import random
import logging
from datetime import datetime, timedelta

Expand Down
38 changes: 35 additions & 3 deletions queue_job/models/queue_job_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class QueueJobFunction(models.Model):
"retry_pattern "
"related_action_enable "
"related_action_func_name "
"related_action_kwargs ",
"related_action_kwargs "
"job_function_id ",
)

def _default_channel(self):
Expand Down Expand Up @@ -88,8 +89,8 @@ def _inverse_name(self):
groups = regex_job_function_name.match(self.name)
if not groups:
raise exceptions.UserError(_("Invalid job function: {}").format(self.name))
model_name = groups[1]
method = groups[2]
model_name = groups.group(1)
method = groups.group(2)
model = self.env["ir.model"].search([("model", "=", model_name)], limit=1)
if not model:
raise exceptions.UserError(_("Model {} not found").format(model_name))
Expand Down Expand Up @@ -123,13 +124,37 @@ def _inverse_edit_related_action(self):
def job_function_name(model_name, method_name):
return "<{}>.{}".format(model_name, method_name)

@api.model
def _find_or_create_channel(self, channel_path):
channel_model = self.env['queue.job.channel']
parts = channel_path.split('.')
parts.reverse()
channel_name = parts.pop()
assert channel_name == 'root', "A channel path starts with 'root'"
# get the root channel
channel = channel_model.search([('name', '=', channel_name)])
while parts:
channel_name = parts.pop()
parent_channel = channel
channel = channel_model.search([
('name', '=', channel_name),
('parent_id', '=', parent_channel.id),
], limit=1)
if not channel:
channel = channel_model.create({
'name': channel_name,
'parent_id': parent_channel.id,
})
return channel

def job_default_config(self):
return self.JobConfig(
channel="root",
retry_pattern={},
related_action_enable=True,
related_action_func_name=None,
related_action_kwargs={},
job_function_id=None,
)

def _parse_retry_pattern(self):
Expand Down Expand Up @@ -162,6 +187,7 @@ def job_config(self, name):
related_action_enable=config.related_action.get("enable", True),
related_action_func_name=config.related_action.get("func_name"),
related_action_kwargs=config.related_action.get("kwargs"),
job_function_id=config.id,
)

def _retry_pattern_format_error_message(self):
Expand Down Expand Up @@ -240,3 +266,9 @@ def unlink(self):
res = super().unlink()
self.clear_caches()
return res

def _register_job(self, model, job_method):
func_name = self.job_function_name(model._name, job_method.__name__)
if not self.search_count([('name', '=', func_name)]):
channel = self._find_or_create_channel(job_method.default_channel)
self.create({'name': func_name, 'channel_id': channel.id})
1 change: 1 addition & 0 deletions queue_job/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import queue_requeue_job
from . import queue_jobs_to_done
from . import queue_jobs_to_cancelled
17 changes: 17 additions & 0 deletions queue_job/wizards/queue_jobs_to_cancelled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2013-2020 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

from odoo import models


class SetJobsToCancelled(models.TransientModel):
_inherit = "queue.requeue.job"
_name = "queue.jobs.to.cancelled"
_description = "Cancel all selected jobs"

def set_cancelled(self):
jobs = self.job_ids.filtered(
lambda x: x.state in ("pending", "failed", "enqueued")
)
jobs.button_cancelled()
return {"type": "ir.actions.act_window_close"}
34 changes: 34 additions & 0 deletions queue_job/wizards/queue_jobs_to_cancelled_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_set_jobs_cancelled" model="ir.ui.view">
<field name="name">Cancel Jobs</field>
<field name="model">queue.jobs.to.cancelled</field>
<field name="arch" type="xml">
<form>
<group string="The selected jobs will be cancelled.">
<field name="job_ids" nolabel="1" colspan="2" />
</group>
<footer>
<button
name="set_cancelled"
string="Cancel jobs"
type="object"
class="oe_highlight"
/>
<button string="Cancel" class="oe_link" special="cancel" />
</footer>
</form>
</field>
</record>

<record id="action_set_jobs_cancelled" model="ir.actions.act_window">
<field name="name">Cancel jobs</field>
<field name="res_model">queue.jobs.to.cancelled</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_set_jobs_cancelled" />
<field name="target">new</field>
<field name="binding_model_id" ref="queue_job.model_queue_job" />
</record>

</odoo>
2 changes: 1 addition & 1 deletion test_queue_job/tests/test_job_auto_delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_auto_delay_force_sync(self):
"""method forced to run synchronously"""
result = (
self.env["test.queue.job"]
.with_context(_job_force_sync=True)
.with_context(queue_job__no_delay=True)
.delay_me(1, kwarg=2)
)
self.assertTrue(result, (1, 2))
Expand Down

0 comments on commit 77d6da1

Please sign in to comment.