Skip to content

[19.0][IMP] queue_job: Implement on fail hook - #955

Open
grindtildeath wants to merge 7 commits into
OCA:19.0from
camptocamp:19.0-imp-queue_job_on_fail_hook
Open

[19.0][IMP] queue_job: Implement on fail hook#955
grindtildeath wants to merge 7 commits into
OCA:19.0from
camptocamp:19.0-imp-queue_job_on_fail_hook

Conversation

@grindtildeath

Copy link
Copy Markdown

Allows to execute a model function when the job fails and will not be retried.

Allows to execute a model function when the job fails and will
not be retried.
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Hi @sbidoul, @guewen,
some modules you are maintaining are being modified, check this out!

@simahawk

Copy link
Copy Markdown
Contributor

Just for the record, there were 2 other attempts to manage this

Your approach seem cleaner tho.

@simahawk simahawk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM

vals = cls._get_failure_values(job, traceback_txt, orig_exception)
job.set_failed(**vals)
job.store()
job.on_fail_hook(vals)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we pass the exception obj too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any added value to pass the raw exception instead of what's been extracted from it in _get_failure_values

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can inspect the exception for additional info. Not a must have tho.

if commit_within_job:
self.env.cr.commit() # pylint: disable=invalid-commit

def _test_on_fail_hook(self, **kw):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only for testing,this is not needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get AttributeError: <class 'odoo.addons.queue_job.models.queue_job.QueueJob'> does not have the attribute '_test_on_fail_hook' if I remove it

@florentx florentx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI fails on test_queue_job add-on.

@OCA-git-bot OCA-git-bot added the mod:test_queue_job Module test_queue_job label Jul 27, 2026
@simahawk

Copy link
Copy Markdown
Contributor

IMO the test failure is correct because it runs in a temporary env that is not rolled back.
You should mock in_temporary_env to use the same cursor.

@grindtildeath

Copy link
Copy Markdown
Author

@simahawk That is what I did, but it was still 🔴 because of the logger in the controller, explicitely muting it makes it 🟢

@guewen Can you please have a look? 🙏


model_name = fields.Char(string="Model", readonly=True)
method_name = fields.Char(readonly=True)
on_fail_method_name = fields.Char(readonly=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would appreciate consistency in naming: on_fail_hook + on_fail_method + on_fail_method_name is going to make some greps miss.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @amh-mw

What would you suggest?

As I take naming quite seriously, here was my rationale:

  • on_fail_method is a reference to the method (eg when call with_delay )
  • on_fail_method_name is the name of the method (which must be on the same model than the queue job)
  • on_fail_hook is the internal function of the job that will be calling the on_fail_method

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might rename all of them (plus on_fail_func that I just noticed) to on_fail. They're on different classes or are method arguments, so I am 85% confident they won't collide.

In honor of this pull request, I'm going to rename _on_done in #937 to check_done to reserve the on_ prefix for hooks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on_fail instead of on_fail_hook make sense. For the rest, is true that func is the term that is already present. But not a blocker from my POV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:queue_job Module queue_job mod:test_queue_job Module test_queue_job series:19.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants