Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][IMP] queue_job: automatically update dependent jobs when state changes manually #676

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions queue_job/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ def perform(self):

return self.result

def enqueue_waiting(self):
sql = """
def _get_common_dependent_jobs_query(self):
return """
UPDATE queue_job
SET state = %s
FROM (
Expand Down Expand Up @@ -571,9 +571,17 @@ def enqueue_waiting(self):
AND %s = ALL(jobs.parent_states)
AND state = %s;
"""

def enqueue_waiting(self):
sql = self._get_common_dependent_jobs_query()
self.env.cr.execute(sql, (PENDING, self.uuid, DONE, WAIT_DEPENDENCIES))
self.env["queue.job"].invalidate_cache(["state"])

def cancel_dependent_jobs(self):
sql = self._get_common_dependent_jobs_query()
self.env.cr.execute(sql, (CANCELLED, self.uuid, CANCELLED, WAIT_DEPENDENCIES))
self.env["queue.job"].invalidate_cache(["state"])

def store(self):
"""Store the Job"""
job_model = self.env["queue.job"]
Expand Down
3 changes: 3 additions & 0 deletions queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,16 @@ def _change_job_state(self, state, result=None):
if state == DONE:
job_.set_done(result=result)
job_.store()
record.env["queue.job"].flush()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this flush necessary? This isn't the code path that was changed.

Copy link
Author

@QuocDuong1306 QuocDuong1306 Aug 6, 2024

Choose a reason for hiding this comment

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

Hello @amh-mw , It demonstrates the need for the flush: if you remove it, newly added tests won't pass:

The parent jobs need to be flushed before updating the child. Like v16 as is (here)

job_.enqueue_waiting()
elif state == PENDING:
job_.set_pending(result=result)
job_.store()
elif state == CANCELLED:
job_.set_cancelled(result=result)
job_.store()
record.env["queue.job"].flush()
job_.cancel_dependent_jobs()
else:
raise ValueError("State not supported: %s" % state)

Expand Down
37 changes: 37 additions & 0 deletions test_queue_job/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
RetryableJobError,
)
from odoo.addons.queue_job.job import (
CANCELLED,
DONE,
ENQUEUED,
FAILED,
Expand Down Expand Up @@ -531,6 +532,42 @@ def test_button_done(self):
stored.result, "Manually set to done by %s" % self.env.user.name
)

def test_button_done_enqueue_waiting_dependencies(self):
job_root = Job(self.env["test.queue.job"].testing_method)
job_child = Job(self.env["test.queue.job"].testing_method)
job_child.add_depends({job_root})

DelayableGraph._ensure_same_graph_uuid([job_root, job_child])
job_root.store()
job_child.store()

self.assertEqual(job_child.state, WAIT_DEPENDENCIES)
record_root = job_root.db_record()
record_child = job_child.db_record()
# Trigger button done
record_root.button_done()
# Check the state
self.assertEqual(record_root.state, DONE)
self.assertEqual(record_child.state, PENDING)

def test_button_cancel_dependencies(self):
job_root = Job(self.env["test.queue.job"].testing_method)
job_child = Job(self.env["test.queue.job"].testing_method)
job_child.add_depends({job_root})

DelayableGraph._ensure_same_graph_uuid([job_root, job_child])
job_root.store()
job_child.store()

self.assertEqual(job_child.state, WAIT_DEPENDENCIES)
record_root = job_root.db_record()
record_child = job_child.db_record()
# Trigger button cancelled
record_root.button_cancelled()
# Check the state
self.assertEqual(record_root.state, CANCELLED)
self.assertEqual(record_child.state, CANCELLED)

def test_requeue(self):
stored = self._create_job()
stored.write({"state": "failed"})
Expand Down
Loading