-
-
Notifications
You must be signed in to change notification settings - Fork 551
[18.0] Add queue_job_profiler #895
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
base: 18.0
Are you sure you want to change the base?
Changes from all commits
a510189
7818e8f
e58b0e4
228f3e1
2bde5ce
6369a8c
919e0d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import controllers | ||
| from . import models |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # 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)", | ||
| "maintainers": ["simahawk"], | ||
| "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", | ||
| ], | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import main |
| 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})", | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import queue_job_function | ||
| from . import queue_job |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # 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", | ||
| compute_sudo=True, | ||
| ) | ||
|
|
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thank you. We may also have an issue with |
||
| job.job_is_profiled = bool(job._profiler_get_record(profile_name)) | ||
|
|
||
| def _profiler_get_record(self, profile_name): | ||
| IrProfile = self.env["ir.profile"].sudo() | ||
| 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", | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||||||
| # Copyright 2026 Camptocamp SA | ||||||||||||
| # 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") | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constraint should track |
||||||||||||
| 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): | ||||||||||||
| if not self: | ||||||||||||
| return f"queue.job {job.uuid} - <anonymous>" | ||||||||||||
| self.ensure_one() | ||||||||||||
| return f"queue.job {job.uuid} - {self.name}" | ||||||||||||
|
Comment on lines
+46
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - [Camptocamp](https://camptocamp.com): | ||
| - Simone Orsi \<<simone.orsi@camptocamp.com>\> |
| 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`. |
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please decorate this method with
@api.depends(...), so that the field's value can be handled properly (eg: invalidated and recomputed correctly when needed).