Skip to content

Commit 6651362

Browse files
committed
[REF] queue_job: Move on fail definition to job function
1 parent 886d47b commit 6651362

7 files changed

Lines changed: 18 additions & 30 deletions

File tree

queue_job/delay.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ class Delayable:
438438
"description",
439439
"channel",
440440
"identity_key",
441-
"on_fail_method",
442441
)
443442
__slots__ = _properties + (
444443
"recordset",
@@ -458,7 +457,6 @@ def __init__(
458457
description=None,
459458
channel=None,
460459
identity_key=None,
461-
on_fail_method=None,
462460
):
463461
self._graph = DelayableGraph()
464462
self._graph.add_vertex(self)
@@ -471,7 +469,6 @@ def __init__(
471469
self.description = description
472470
self.channel = channel
473471
self.identity_key = identity_key
474-
self.on_fail_method = on_fail_method
475472

476473
self._job_method = None
477474
self._job_args = ()
@@ -550,7 +547,6 @@ def split(self, size, chain=False):
550547
description=self.description,
551548
channel=self.channel,
552549
identity_key=self.identity_key,
553-
on_fail_method=self.on_fail_method,
554550
)
555551
# Update the __self__
556552
delayable._job_method = getattr(recordset, self._job_method.__name__)
@@ -587,7 +583,6 @@ def _build_job(self):
587583
description=self.description,
588584
channel=self.channel,
589585
identity_key=self.identity_key,
590-
on_fail_method=self.on_fail_method,
591586
)
592587
return self._generated_job
593588

@@ -638,7 +633,6 @@ def __init__(
638633
description=None,
639634
channel=None,
640635
identity_key=None,
641-
on_fail_method=None,
642636
):
643637
self.delayable = Delayable(
644638
recordset,
@@ -648,7 +642,6 @@ def __init__(
648642
description=description,
649643
channel=channel,
650644
identity_key=identity_key,
651-
on_fail_method=on_fail_method,
652645
)
653646

654647
@property

queue_job/job.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ class Job:
200200
be added to a channel if the existing job with the same key is not yet
201201
started or executed.
202202
203-
.. attribute::on_fail_method
204-
205-
A function to be called if the job is failed and will not be retried.
206203
"""
207204

208205
@classmethod
@@ -301,9 +298,6 @@ def _load_from_db_record(cls, job_db_record):
301298
description=stored.name,
302299
channel=stored.channel,
303300
identity_key=stored.identity_key,
304-
on_fail_method=getattr(recordset, stored.on_fail_method_name)
305-
if stored.on_fail_method_name
306-
else None,
307301
)
308302

309303
if stored.date_created:
@@ -371,7 +365,6 @@ def __init__(
371365
description=None,
372366
channel=None,
373367
identity_key=None,
374-
on_fail_method=None,
375368
):
376369
"""Create a Job
377370
@@ -414,14 +407,14 @@ def __init__(
414407
self.method_name = func.__name__
415408
self.recordset = recordset
416409

417-
if on_fail_method:
418-
if not _is_model_method(on_fail_method):
419-
raise TypeError("Job accepts only methods of Models")
420-
self.on_fail_method_name = on_fail_method.__name__
421-
422410
self.job_config = (
423411
self.env["queue.job.function"].sudo().job_config(self.job_function_name)
424412
)
413+
on_fail_method_name = self.job_config.on_fail_method_name
414+
if on_fail_method_name:
415+
if not _is_model_method(getattr(self.recordset, on_fail_method_name, None)):
416+
raise TypeError("Job accepts only methods of Models")
417+
self.on_fail_method_name = on_fail_method_name
425418

426419
self.state = PENDING
427420

queue_job/models/base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def with_delay(
2626
description=None,
2727
channel=None,
2828
identity_key=None,
29-
on_fail_method=None,
3029
):
3130
"""Return a ``DelayableRecordset``
3231
@@ -60,7 +59,6 @@ def with_delay(
6059
description=description,
6160
channel=channel,
6261
identity_key=identity_key,
63-
on_fail_method=on_fail_method,
6462
)
6563

6664
def delayable(
@@ -71,7 +69,6 @@ def delayable(
7169
description=None,
7270
channel=None,
7371
identity_key=None,
74-
on_fail_method=None,
7572
):
7673
"""Return a ``Delayable``
7774
@@ -143,7 +140,6 @@ def delayable(
143140
description=description,
144141
channel=channel,
145142
identity_key=identity_key,
146-
on_fail_method=on_fail_method,
147143
)
148144

149145
def _patch_job_auto_delay(self, method_name, context_key=None):

queue_job/models/queue_job.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class QueueJob(models.Model):
7777

7878
model_name = fields.Char(string="Model", readonly=True)
7979
method_name = fields.Char(readonly=True)
80-
on_fail_method_name = fields.Char(readonly=True)
8180
records = JobSerialized(
8281
string="Record(s)",
8382
readonly=True,

queue_job/models/queue_job_function.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class QueueJobFunction(models.Model):
2929
"related_action_func_name "
3030
"related_action_kwargs "
3131
"job_function_id "
32-
"allow_commit",
32+
"allow_commit "
33+
"on_fail_method_name",
3334
)
3435

3536
def _default_channel(self):
@@ -48,6 +49,10 @@ def _default_channel(self):
4849
comodel_name="ir.model", string="Model", ondelete="cascade"
4950
)
5051
method = fields.Char()
52+
on_fail_method = fields.Char(
53+
help="Model function to be called if the job is failed and will not be "
54+
"retried.",
55+
)
5156

5257
channel_id = fields.Many2one(
5358
comodel_name="queue.job.channel",
@@ -159,6 +164,7 @@ def job_default_config(self):
159164
related_action_kwargs={},
160165
job_function_id=None,
161166
allow_commit=False,
167+
on_fail_method_name=None,
162168
)
163169

164170
def _parse_retry_pattern(self):
@@ -195,6 +201,7 @@ def job_config(self, name):
195201
related_action_kwargs=config.related_action.get("kwargs", {}),
196202
job_function_id=config.id,
197203
allow_commit=config.allow_commit,
204+
on_fail_method_name=config.on_fail_method,
198205
)
199206

200207
def _retry_pattern_format_error_message(self):

queue_job/tests/test_model_job_function.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_function_job_config(self):
3535
{
3636
"model_id": self.env.ref("base.model_res_users").id,
3737
"method": "read",
38+
"on_fail_method": "search_read",
3839
"channel_id": channel.id,
3940
"edit_retry_pattern": "{1: 2, 3: 4}",
4041
"edit_related_action": (
@@ -55,5 +56,6 @@ def test_function_job_config(self):
5556
related_action_kwargs={"b": 1},
5657
job_function_id=job_function.id,
5758
allow_commit=True,
59+
on_fail_method_name="search_read",
5860
),
5961
)

queue_job/tests/test_run_rob_controller.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ def test_runjob_success(self):
3434
self.assertEqual(job.db_record().state, "done")
3535

3636
def test_runjob_on_fail_hook(self):
37-
job = (
38-
self.env["queue.job"]
39-
.with_delay(on_fail_method=self.env["queue.job"]._test_on_fail_hook)
40-
._test_job(failure_rate=1)
41-
)
37+
function = self.env.ref("queue_job.job_function_queue_job__test_job")
38+
function.on_fail_method = "_test_on_fail_hook"
39+
job = self.env["queue.job"].with_delay()._test_job(failure_rate=1)
4240
with (
4341
self.assertRaises(JobError),
4442
patch(

0 commit comments

Comments
 (0)