Maintainers
This module is maintained by the OCA.
-

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.
diff --git a/queue_job/README.rst b/queue_job/README.rst index 439fc6ffc1..23c2510a92 100644 --- a/queue_job/README.rst +++ b/queue_job/README.rst @@ -397,6 +397,33 @@ Based on this configuration, we can tell that: * retries 10 to 15 postponed 30 seconds later * all subsequent retries postponed 5 minutes later +**Job function: reach max retryable times** + +When a job has reached the maximum number of retries and still fails, +the job's status will be set to ``Failed``. +You can define a specific method to handle this event. +The method should be named ``{method_name}_on_max_retries_reached``. + +Here's an example: + +.. code-block:: python + + from odoo import models, fields, api + + class MyModel(models.Model): + _name = 'my.model' + + def button_done(self): + self.env['my.model'].with_delay().my_method('a', k=2) + + def my_method_on_max_retries_reached(self): + # This method is called when the job reaches the maximum retries and fails + # Add your custom logic here + +In this example, ``my_method_on_max_retries_reached`` is the method +that will be called when the job my_method fails after reaching the maximum retries. +You can add your custom logic inside this method to handle the event. + **Job Context** The context of the recordset of the job, or any recordset passed in arguments of diff --git a/queue_job/job.py b/queue_job/job.py index 920a8a0781..e5c438661c 100644 --- a/queue_job/job.py +++ b/queue_job/job.py @@ -527,6 +527,21 @@ def perform(self): elif not self.max_retries: # infinite retries raise elif self.retry >= self.max_retries: + hook = f"{self.method_name}_on_max_retries_reached" + if hasattr(self.recordset, hook): + recordset = self.recordset.with_context( + job_uuid=self.uuid, exc_info=self.exc_info + ) + try: + getattr(recordset, hook)() + except Exception as ex: + _logger.debug( + "Exception on %s:%s() for Job UUID: %s", + self.recordset, + hook, + self.uuid, + ex, + ) type_, value, traceback = sys.exc_info() # change the exception type but keep the original # traceback and message: diff --git a/queue_job/readme/USAGE.rst b/queue_job/readme/USAGE.rst index 0ac17f7b10..0a2cf35763 100644 --- a/queue_job/readme/USAGE.rst +++ b/queue_job/readme/USAGE.rst @@ -242,6 +242,33 @@ Based on this configuration, we can tell that: * retries 10 to 15 postponed 30 seconds later * all subsequent retries postponed 5 minutes later +**Job function: reach max retryable times** + +When a job has reached the maximum number of retries and still fails, +the job's status will be set to ``Failed``. +You can define a specific method to handle this event. +The method should be named ``{method_name}_on_max_retries_reached``. + +Here's an example: + +.. code-block:: python + + from odoo import models, fields, api + + class MyModel(models.Model): + _name = 'my.model' + + def button_done(self): + self.env['my.model'].with_delay().my_method('a', k=2) + + def my_method_on_max_retries_reached(self): + # This method is called when the job reaches the maximum retries and fails + # Add your custom logic here + +In this example, ``my_method_on_max_retries_reached`` is the method +that will be called when the job my_method fails after reaching the maximum retries. +You can add your custom logic inside this method to handle the event. + **Job Context** The context of the recordset of the job, or any recordset passed in arguments of diff --git a/queue_job/static/description/index.html b/queue_job/static/description/index.html index 20ec9c0e0a..e766bc03de 100644 --- a/queue_job/static/description/index.html +++ b/queue_job/static/description/index.html @@ -8,10 +8,11 @@ /* :Author: David Goodger (goodger@python.org) -:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $ +:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $ :Copyright: This stylesheet has been placed in the public domain. Default cascading style sheet for the HTML output of Docutils. +Despite the name, some widely supported CSS2 features are used. See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to customize this style sheet. @@ -274,7 +275,7 @@ margin-left: 2em ; margin-right: 2em } -pre.code .ln { color: grey; } /* line numbers */ +pre.code .ln { color: gray; } /* line numbers */ pre.code, code { background-color: #eeeeee } pre.code .comment, code .comment { color: #5C6576 } pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold } @@ -300,7 +301,7 @@ span.pre { white-space: pre } -span.problematic { +span.problematic, pre.problematic { color: red } span.section-subtitle { @@ -715,6 +716,28 @@
Job function: reach max retryable times
+When a job has reached the maximum number of retries and still fails, +the job’s status will be set to Failed. +You can define a specific method to handle this event. +The method should be named {method_name}_on_max_retries_reached.
+Here’s an example:
++from odoo import models, fields, api + +class MyModel(models.Model): + _name = 'my.model' + + def button_done(self): + self.env['my.model'].with_delay().my_method('a', k=2) + + def my_method_on_max_retries_reached(self): + # This method is called when the job reaches the maximum retries and fails + # Add your custom logic here ++
In this example, my_method_on_max_retries_reached is the method +that will be called when the job my_method fails after reaching the maximum retries. +You can add your custom logic inside this method to handle the event.
Job Context
The context of the recordset of the job, or any recordset passed in arguments of a job, is transferred to the job according to an allow-list.
@@ -958,7 +981,9 @@This module is maintained by the OCA.
-OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.