Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Available addons
----------------
addon | version | maintainers | summary
--- | --- | --- | ---
[queue_job](queue_job/) | 19.0.1.0.1 | <a href='https://github.com/guewen'><img src='https://github.com/guewen.png' width='32' height='32' style='border-radius:50%;' alt='guewen'/></a> <a href='https://github.com/sbidoul'><img src='https://github.com/sbidoul.png' width='32' height='32' style='border-radius:50%;' alt='sbidoul'/></a> | Job Queue
[queue_job](queue_job/) | 19.0.1.1.0 | <a href='https://github.com/guewen'><img src='https://github.com/guewen.png' width='32' height='32' style='border-radius:50%;' alt='guewen'/></a> <a href='https://github.com/sbidoul'><img src='https://github.com/sbidoul.png' width='32' height='32' style='border-radius:50%;' alt='sbidoul'/></a> | Job Queue
[test_queue_job](test_queue_job/) | 19.0.1.0.1 | <a href='https://github.com/sbidoul'><img src='https://github.com/sbidoul.png' width='32' height='32' style='border-radius:50%;' alt='sbidoul'/></a> | Queue Job Tests


Expand Down
2 changes: 1 addition & 1 deletion queue_job/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Job Queue
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:6f668a4a03d832fe3d406bd79a70cebf7faa72c6a22371a78aa2c5627103abd4
!! source digest: sha256:8f055109b96365bbd4bbcdd3273a3d2be459c003b4d49604bac2e2988bcf5c49
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Mature-brightgreen.png
Expand Down
2 changes: 1 addition & 1 deletion queue_job/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{
"name": "Job Queue",
"version": "19.0.1.0.1",
"version": "19.0.1.1.0",
"author": "Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/queue",
"license": "LGPL-3",
Expand Down
58 changes: 49 additions & 9 deletions queue_job/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import random
import time
import traceback
from contextlib import contextmanager
from io import StringIO

from psycopg2 import OperationalError, errorcodes
Expand All @@ -26,6 +27,29 @@
DEPENDS_MAX_TRIES_ON_CONCURRENCY_FAILURE = 5


@contextmanager
def _prevent_commit(cr):
"""Context manager to prevent commits on a cursor.

Commiting while the job is not finished would release the job lock, causing
it to be started again by the dead jobs requeuer.
"""

def forbidden_commit(*args, **kwargs):
raise RuntimeError(
"Commit is forbidden in queue jobs. "
"If the current job is a cron running as queue job, "
"modify it to run as a normal cron."
)

original_commit = cr.commit
cr.commit = forbidden_commit
try:
yield
finally:
cr.commit = original_commit


class RunJobController(http.Controller):
@classmethod
def _acquire_job(cls, env: api.Environment, job_uuid: str) -> Job | None:
Expand Down Expand Up @@ -69,13 +93,16 @@ def _acquire_job(cls, env: api.Environment, job_uuid: str) -> Job | None:
def _try_perform_job(cls, env, job):
"""Try to perform the job, mark it done and commit if successful."""
_logger.debug("%s started", job)
job.perform()
# Triggers any stored computed fields before calling 'set_done'
# so that will be part of the 'exec_time'
env.flush_all()
job.set_done()
job.store()
env.flush_all()
# TODO refactor, the relation between env and job.env is not clear
assert env.cr is job.env.cr
with _prevent_commit(env.cr):
job.perform()
# Triggers any stored computed fields before calling 'set_done'
# so that will be part of the 'exec_time'
env.flush_all()
job.set_done()
job.store()
env.flush_all()
env.cr.commit()
_logger.debug("%s done", job)

Expand Down Expand Up @@ -201,6 +228,7 @@ def create_test_job(
size=1,
failure_rate=0,
job_duration=0,
commit_within_job=False,
):
if not http.request.env.user.has_group("base.group_erp_manager"):
raise Forbidden(http.request.env._("Access Denied"))
Expand Down Expand Up @@ -246,6 +274,7 @@ def create_test_job(
description=description,
failure_rate=failure_rate,
job_duration=job_duration,
commit_within_job=commit_within_job,
)

if size > 1:
Expand All @@ -257,6 +286,7 @@ def create_test_job(
description=description,
failure_rate=failure_rate,
job_duration=job_duration,
commit_within_job=commit_within_job,
)
return ""

Expand All @@ -269,6 +299,7 @@ def _create_single_test_job(
size=1,
failure_rate=0,
job_duration=0,
commit_within_job=False,
):
delayed = (
http.request.env["queue.job"]
Expand All @@ -278,7 +309,11 @@ def _create_single_test_job(
channel=channel,
description=description,
)
._test_job(failure_rate=failure_rate, job_duration=job_duration)
._test_job(
failure_rate=failure_rate,
job_duration=job_duration,
commit_within_job=commit_within_job,
)
)
return f"job uuid: {delayed.db_record().uuid}"

Expand All @@ -293,6 +328,7 @@ def _create_graph_test_jobs(
description="Test job",
failure_rate=0,
job_duration=0,
commit_within_job=False,
):
model = http.request.env["queue.job"]
current_count = 0
Expand All @@ -315,7 +351,11 @@ def _create_graph_test_jobs(
max_retries=max_retries,
channel=channel,
description=f"{description} #{current_count}",
)._test_job(failure_rate=failure_rate, job_duration=job_duration)
)._test_job(
failure_rate=failure_rate,
job_duration=job_duration,
commit_within_job=commit_within_job,
)
)

grouping = random.choice(possible_grouping_methods)
Expand Down
4 changes: 3 additions & 1 deletion queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,11 @@ def related_action_open_record(self):
)
return action

def _test_job(self, failure_rate=0, job_duration=0):
def _test_job(self, failure_rate=0, job_duration=0, commit_within_job=False):
_logger.info("Running test job.")
if random.random() <= failure_rate:
raise JobError("Job failed")
if job_duration:
time.sleep(job_duration)
if commit_within_job:
self.env.cr.commit() # pylint: disable=invalid-commit
2 changes: 1 addition & 1 deletion queue_job/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ <h1>Job Queue</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:6f668a4a03d832fe3d406bd79a70cebf7faa72c6a22371a78aa2c5627103abd4
!! source digest: sha256:8f055109b96365bbd4bbcdd3273a3d2be459c003b4d49604bac2e2988bcf5c49
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Mature" src="https://img.shields.io/badge/maturity-Mature-brightgreen.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/queue/tree/19.0/queue_job"><img alt="OCA/queue" src="https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/queue-19-0/queue-19-0-queue_job"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/queue&amp;target_branch=19.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon adds an integrated Job Queue to Odoo.</p>
Expand Down