-
-
Notifications
You must be signed in to change notification settings - Fork 545
[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
Draft
simahawk
wants to merge
4
commits into
OCA:18.0
Choose a base branch
from
camptocamp:18-qjob-profiler
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+900
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import controllers | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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})", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import queue_job_function | ||
| from . import queue_job |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - [Camptocamp](https://camptocamp.com): | ||
| - Simone Orsi \<<simone.orsi@camptocamp.com>\> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
?