Skip to content
Draft
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
97 changes: 97 additions & 0 deletions queue_job_profiler/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
==================
Job Queue Profiler
==================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:b6e605728ea05be0bf98dbe6e94c197bab21882e965286ef214f719e252b444e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github
:target: https://github.com/OCA/queue/tree/18.0/queue_job_profiler
:alt: OCA/queue
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/queue-18-0/queue-18-0-queue_job_profiler
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/queue&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This addon adds profiling controls to queue job functions and wraps
queue job execution in an Odoo profiler session when enabled.

When profiling is enabled for a job function and the executing user
matches one of the configured profiling users (or no users are set), the
queue job runner records SQL and async stack traces via
``odoo.tools.profiler.Profiler`` and saves the results into
``ir_profile``.

**Table of contents**

.. contents::
:local:

Usage
=====

1. Open the Queue Job Functions menu.
2. Open the job function you want to profile.
3. In the Profiler group, enable Profiling and set the Profiling users
(optional) and Profiling until.
4. Run the job with the selected user (the queue job runner usually
executes as the superuser).
5. Inspect the generated entries in ``ir_profile`` to review the
captured profiling data.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/queue/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/queue/issues/new?body=module:%20queue_job_profiler%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Camptocamp

Contributors
------------

- `Camptocamp <https://camptocamp.com>`__:

- Simone Orsi <simone.orsi@camptocamp.com>

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

This module is part of the `OCA/queue <https://github.com/OCA/queue/tree/18.0/queue_job_profiler>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions queue_job_profiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
18 changes: 18 additions & 0 deletions queue_job_profiler/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2026 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl

{
"name": "Job Queue Profiler",
"version": "18.0.1.0.0",
"author": "Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/queue",
"license": "AGPL-3",
"category": "Generic Modules",
"depends": [
"queue_job",
],
"data": [
"views/queue_job_function_views.xml",
"views/queue_job_views.xml",
],
}
1 change: 1 addition & 0 deletions queue_job_profiler/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
31 changes: 31 additions & 0 deletions queue_job_profiler/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2026 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tools.profiler import Profiler

from odoo.addons.queue_job.controllers.main import RunJobController as BaseController


class RunJobController(BaseController):
def _try_perform_job(self, env, job):
if self._profiler_is_enabled(env, job):
return self._profiler_perform_job(env, job)
return super()._try_perform_job(env, job)

def _profiler_is_enabled(self, env, job):
func_id = job.job_config.job_function_id
job_function = env["queue.job.function"].browse(func_id)
return job_function.is_profiling_enabled()

def _profiler_perform_job(self, env, job):
with self._profiler_get(env, job):
result = super()._try_perform_job(env, job)
return result

def _profiler_get(self, env, job):
func_id = job.job_config.job_function_id
job_function = env["queue.job.function"].browse(func_id)
return Profiler(
description=job_function._profile_make_name(job),
profile_session=f"{env.user.name} (uid={env.user.id})",
)
2 changes: 2 additions & 0 deletions queue_job_profiler/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import queue_job_function
from . import queue_job
43 changes: 43 additions & 0 deletions queue_job_profiler/models/queue_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import fields, models


class QueueJob(models.Model):
_inherit = "queue.job"

job_is_profiled = fields.Boolean(
string="Profiled",
default=False,
help="Whether this job has been profiled or not.",
compute="_compute_job_is_profiled",
)

def _compute_job_is_profiled(self):
for job in self:
# don't care about perf as this is loaded only on the job form view
profile_name = job.job_function_id._profile_make_name(job)
job.job_is_profiled = bool(job._profiler_get_record(profile_name))

def _profiler_get_record(self, profile_name):
IrProfile = self.env["ir.profile"]
return IrProfile.search(
[("name", "=", profile_name)],
limit=1,
)

def action_view_profile(self):
self.ensure_one()
profile_name = self.job_function_id._profile_make_name(self)
profile = self._profiler_get_record(profile_name)
if not profile:
return {"type": "ir.actions.act_window_close"}
return {
"type": "ir.actions.act_window",
"name": "Profile",
"res_model": "ir.profile",
"view_mode": "form",
"res_id": profile.id,
"target": "current",
}
47 changes: 47 additions & 0 deletions queue_job_profiler/models/queue_job_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo import api, exceptions, fields, models


class QueueJobFunction(models.Model):
_inherit = "queue.job.function"

profiling_enabled = fields.Boolean(
string="Profiling enabled",
help="Indicates whether profiling is enabled for this job function.",
)
profiling_user_ids = fields.Many2many(
"res.users",
string="Profiling users",
help="The users allowed to perform profiling for this job function.",
)
profiling_until = fields.Datetime(
string="Profiling until",
help="The date and time until which profiling is enabled "
"for this job function.",
)

def is_profiling_enabled(self):
self.ensure_one()
return (
self.profiling_enabled
and (self.profiling_until and self.profiling_until >= fields.Datetime.now())
and (
not self.profiling_user_ids or self.env.user in self.profiling_user_ids
)
)

@api.constrains("profiling_enabled")
def _check_profiling_setup(self):
for record in self:
if record.profiling_enabled and not record.profiling_until:
raise exceptions.ValidationError(
self.env._(
"A profiling until date must be set when profiling is enabled."
)
)

def _profile_make_name(self, job):
self.ensure_one()
return f"queue.job {job.uuid} - {self.name}"
3 changes: 3 additions & 0 deletions queue_job_profiler/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions queue_job_profiler/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [Camptocamp](https://camptocamp.com):
- Simone Orsi \<<simone.orsi@camptocamp.com>\>
8 changes: 8 additions & 0 deletions queue_job_profiler/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This addon adds profiling controls to queue job functions and wraps
queue job execution in an Odoo profiler session when enabled.

When profiling is enabled for a job function and the executing user
matches one of the configured profiling users (or no users are set),
the queue job runner records
SQL and async stack traces via `odoo.tools.profiler.Profiler` and saves
the results into `ir_profile`.
8 changes: 8 additions & 0 deletions queue_job_profiler/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1. Open the Queue Job Functions menu.
2. Open the job function you want to profile.
3. In the Profiler group, enable Profiling and set the Profiling users
(optional) and Profiling until.
4. Run the job with the selected user (the queue job runner usually executes
as the superuser).
5. Inspect the generated entries in `ir_profile` to review the captured
profiling data.
Loading