-
-
Notifications
You must be signed in to change notification settings - Fork 575
[18.0][ADD] queue_job_alert_running #942
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
Open
Saran440
wants to merge
1
commit into
OCA:18.0
Choose a base branch
from
ecosoft-odoo:18.0-add-queue_job_alert_running
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.
+845
−0
Open
Changes from all commits
Commits
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,123 @@ | ||
| ======================= | ||
| Job Queue Alert Running | ||
| ======================= | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! 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_alert_running | ||
| :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_alert_running | ||
| :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 provides a mixin (``queue.job.status.mixin``) that adds an | ||
| ``is_job_running`` boolean field and automatically displays a warning | ||
| banner on form views when a record has a running queue job, helping | ||
| prevent concurrent editing of the same record. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| To use this module: | ||
|
|
||
| 1. Inherit ``queue.job.status.mixin`` in your model: | ||
|
|
||
| .. code:: python | ||
|
|
||
| class MyModel(models.Model): | ||
| _name = "my.model" | ||
| _inherit = ["queue.job.status.mixin"] | ||
|
|
||
| 2. Call ``action_mark_running()`` when a queue job starts (e.g., before | ||
| ``with_delay()``): | ||
|
|
||
| .. code:: python | ||
|
|
||
| def action_process(self): | ||
| self.action_mark_running() | ||
| self.with_delay()._process_in_background() | ||
|
|
||
| 3. Call ``action_mark_done()`` inside the background job when finished: | ||
|
|
||
| .. code:: python | ||
|
|
||
| def _process_in_background(self): | ||
| try: | ||
| # ... do work ... | ||
| pass | ||
| finally: | ||
| self.action_mark_done() | ||
|
|
||
| When ``is_job_running`` is ``True``, a yellow warning banner appears at | ||
| the top of the form view: *"This Record is running in queue job."* | ||
|
|
||
| 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_alert_running%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 | ||
| ------- | ||
|
|
||
| * Ecosoft | ||
|
|
||
| Contributors | ||
| ------------ | ||
|
|
||
| - Saran Lim. <saranl@ecosoft.co.th> | ||
|
|
||
| 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. | ||
|
|
||
| .. |maintainer-Saran440| image:: https://github.com/Saran440.png?size=40px | ||
| :target: https://github.com/Saran440 | ||
| :alt: Saran440 | ||
|
|
||
| Current `maintainer <https://odoo-community.org/page/maintainer-role>`__: | ||
|
|
||
| |maintainer-Saran440| | ||
|
|
||
| This module is part of the `OCA/queue <https://github.com/OCA/queue/tree/18.0/queue_job_alert_running>`_ 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,3 @@ | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | ||
|
|
||
| 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,17 @@ | ||
| # Copyright 2025 Ecosoft Co., Ltd. | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | ||
|
|
||
| { | ||
| "name": "Job Queue Alert Running", | ||
| "version": "18.0.1.0.0", | ||
| "author": "Ecosoft, Odoo Community Association (OCA)", | ||
| "website": "https://github.com/OCA/queue", | ||
| "summary": "Show a warning banner when a record has a running queue job", | ||
| "license": "AGPL-3", | ||
| "category": "Generic Modules", | ||
| "depends": ["queue_job"], | ||
| "data": [ | ||
| "templates/queue_running_templates.xml", | ||
| ], | ||
| "maintainers": ["Saran440"], | ||
| } |
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 @@ | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | ||
|
|
||
| from . import queue_job_status_mixin |
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,57 @@ | ||
| # Copyright 2025 Ecosoft Co., Ltd. | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | ||
|
|
||
| from lxml import etree | ||
|
|
||
| from odoo import api, fields, models | ||
| from odoo.tools.misc import frozendict | ||
|
|
||
|
|
||
| class QueueJobStatusMixin(models.AbstractModel): | ||
| _name = "queue.job.status.mixin" | ||
| _description = "Queue Job Status Mixin" | ||
|
|
||
| is_job_running = fields.Boolean(copy=False) | ||
|
|
||
| def action_mark_running(self): | ||
| self.write({"is_job_running": True}) | ||
|
|
||
| def action_mark_done(self): | ||
| self.write({"is_job_running": False}) | ||
|
|
||
| @api.model | ||
| def _merge_view_fields(self, all_models: dict, new_models: dict): | ||
| """Merge new_models into all_models. | ||
| Both are {modelname(str) > fields(tuple)}.""" | ||
| for model, view_fields in new_models.items(): | ||
| if model in all_models: | ||
| all_models[model] = tuple(set(all_models[model]) | set(view_fields)) | ||
| else: | ||
| all_models[model] = tuple(view_fields) | ||
|
|
||
| def _add_queue_running_label(self, node, params): | ||
| str_element = self.env["ir.qweb"]._render( | ||
| "queue_job_alert_running.queue_running_label", params | ||
| ) | ||
| new_node = etree.fromstring(str_element) | ||
| return new_node | ||
|
|
||
| @api.model | ||
| def get_view(self, view_id=None, view_type="form", **options): | ||
| res = super().get_view(view_id=view_id, view_type=view_type, **options) | ||
| View = self.env["ir.ui.view"] | ||
| if view_type == "form": | ||
| doc = etree.XML(res["arch"]) | ||
| params = {} | ||
| all_models = res["models"].copy() # {modelname(str) > fields(tuple)} | ||
| for node in doc.xpath("/form/sheet"): | ||
| # _add_queue_running_label process | ||
| new_node = self._add_queue_running_label(node, params) | ||
| new_arch, new_models = View.postprocess_and_fields(new_node, self._name) | ||
| new_node = etree.fromstring(new_arch) | ||
| for new_element in new_node: | ||
| node.addprevious(new_element) | ||
| self._merge_view_fields(all_models, new_models) | ||
| res["arch"] = etree.tostring(doc) | ||
| res["models"] = frozendict(all_models) | ||
| return res |
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 @@ | ||
| - Saran Lim. \<<saranl@ecosoft.co.th>\> |
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 @@ | ||
| This addon provides a mixin (`queue.job.status.mixin`) that adds an `is_job_running` boolean field and automatically displays a warning banner on form views when a record has a running queue job, helping prevent concurrent editing of the same record. |
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,30 @@ | ||
| To use this module: | ||
|
|
||
| 1. Inherit `queue.job.status.mixin` in your model: | ||
|
|
||
| ```python | ||
| class MyModel(models.Model): | ||
| _name = "my.model" | ||
| _inherit = ["queue.job.status.mixin"] | ||
| ``` | ||
|
|
||
| 2. Call `action_mark_running()` when a queue job starts (e.g., before `with_delay()`): | ||
|
|
||
| ```python | ||
| def action_process(self): | ||
| self.action_mark_running() | ||
| self.with_delay()._process_in_background() | ||
| ``` | ||
|
|
||
| 3. Call `action_mark_done()` inside the background job when finished: | ||
|
|
||
| ```python | ||
| def _process_in_background(self): | ||
| try: | ||
| # ... do work ... | ||
| pass | ||
| finally: | ||
| self.action_mark_done() | ||
| ``` | ||
|
|
||
| When `is_job_running` is `True`, a yellow warning banner appears at the top of the form view: *"This Record is running in queue job."* | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
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.
This approach leaves room for a downstream developer to mess up error handling. What about a method decorator instead?
Failed jobs seem likely to goof up the transaction, so the decorator would need to
savepointandroll back?Alternate approach:
is_job_runningas computed field?