diff --git a/setup/web_assets_warmup/odoo/addons/web_assets_warmup b/setup/web_assets_warmup/odoo/addons/web_assets_warmup new file mode 120000 index 000000000..a261cf76c --- /dev/null +++ b/setup/web_assets_warmup/odoo/addons/web_assets_warmup @@ -0,0 +1 @@ +../../../../web_assets_warmup \ No newline at end of file diff --git a/setup/web_assets_warmup/setup.py b/setup/web_assets_warmup/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_assets_warmup/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_assets_warmup/README.rst b/web_assets_warmup/README.rst new file mode 100644 index 000000000..e69de29bb diff --git a/web_assets_warmup/__init__.py b/web_assets_warmup/__init__.py new file mode 100644 index 000000000..5bf67492b --- /dev/null +++ b/web_assets_warmup/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import post_load_hook diff --git a/web_assets_warmup/__manifest__.py b/web_assets_warmup/__manifest__.py new file mode 100644 index 000000000..8e6aa5c7b --- /dev/null +++ b/web_assets_warmup/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +{ + "name": "Generate assets when Odoo starts", + "summary": "Ensure that assets are generated when Odoo starts.", + "version": "14.0.1.0.0", + "category": "Hidden", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "web", + ], + "website": "https://github.com/OCA/web", + "data": ["data/ir_cron.xml"], + "post_load": "post_load_hook", + "installable": True, +} diff --git a/web_assets_warmup/data/ir_cron.xml b/web_assets_warmup/data/ir_cron.xml new file mode 100644 index 000000000..bbebb47df --- /dev/null +++ b/web_assets_warmup/data/ir_cron.xml @@ -0,0 +1,22 @@ + + + + + + Generate report assets + 1 + months + -1 + + + + + code + model.cron_generate_assets() + + + diff --git a/web_assets_warmup/hooks.py b/web_assets_warmup/hooks.py new file mode 100644 index 000000000..a71a533e7 --- /dev/null +++ b/web_assets_warmup/hooks.py @@ -0,0 +1,70 @@ +# Copyright 2020 Camptocamp SA +# Copyright 2023 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +import logging +import os + +import psycopg2 + +from odoo import fields, registry + +logger = logging.getLogger(__name__) + + +def active_cron_assets(): + """Plan the next execution of the cron responsible to generate assets.""" + if os.environ.get("RUNNING_ENV") == "dev": + return + dbname = os.environ.get("DB_NAME") + reg = registry(dbname) + with reg.cursor() as cr: + cron_module, cron_ref = "web_assets_warmup", "cron_generate_assets" + query = """ + SELECT model, res_id + FROM ir_model_data + WHERE module=%s + AND name=%s; + """ + args = (cron_module, cron_ref) + cr.execute(query, args) + row = cr.fetchone() + # post_load hook is called before the update of the module so the + # ir_cron record doesn't exist on first install + if row: + model, res_id = row + if model != "ir.cron": + return + # if there is already someone doing the same or already being executed + # we can skip the update of ir_cron + try: + with cr.savepoint(): + cr.execute( + "SELECT * FROM ir_cron WHERE id = %s FOR UPDATE NOWAIT;", + (res_id,), + ) + query = """ + UPDATE ir_cron + SET active=true, nextcall=%s, priority=%s + WHERE id=%s + """ + nextcall = fields.Datetime.to_string(fields.Datetime.now()) + args = (nextcall, -99, res_id) + cr.execute(query, args) + logger.info( + "Cron '%s.%s' planned for execution at %s", + cron_module, + cron_ref, + nextcall, + ) + except psycopg2.OperationalError as e: + if e.pgcode == "55P03": + logger.info( + "Cron '%s.%s' is currently being executed or updated", + cron_module, + cron_ref, + ) + + +def post_load_hook(): + active_cron_assets() diff --git a/web_assets_warmup/models/__init__.py b/web_assets_warmup/models/__init__.py new file mode 100644 index 000000000..a248cf216 --- /dev/null +++ b/web_assets_warmup/models/__init__.py @@ -0,0 +1 @@ +from . import ir_actions_report diff --git a/web_assets_warmup/models/ir_actions_report.py b/web_assets_warmup/models/ir_actions_report.py new file mode 100644 index 000000000..954dc67ad --- /dev/null +++ b/web_assets_warmup/models/ir_actions_report.py @@ -0,0 +1,37 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +import logging + +from odoo import api, models + +logger = logging.getLogger(__name__) + + +class IrActionsReport(models.Model): + _inherit = "ir.actions.report" + + @api.model + def cron_generate_assets(self): + """Ensure that the assets are well-generated in the database.""" + logger.info("Ensure that assets are generated and stored in the database...") + # Call `_get_asset_nodes` as done when printing a report based on + # `web.report_layout` template (used by `web.html_container`) + options = { + "commit_assetsbundle": False, + "debug": False, + "inherit_branding": False, + "dev_mode": False, + "caller_template": "web.html_container", + } + assets_template_ids = [ + "web.report_assets_common", + "web.assets_common", + "web.report_assets_pdf", + ] + for xml_id in assets_template_ids: + self.env["ir.qweb"]._get_asset_nodes( + xmlid=xml_id, options=options, css=True, js=True + ) + logger.info("Ensure that assets are generated and stored in the database: done") + return True diff --git a/web_assets_warmup/readme/CONTRIBUTORS.rst b/web_assets_warmup/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..d5a5b126e --- /dev/null +++ b/web_assets_warmup/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Sébastien Alix +* Michael Tietz (MT Software) diff --git a/web_assets_warmup/readme/DESCRIPTION.rst b/web_assets_warmup/readme/DESCRIPTION.rst new file mode 100644 index 000000000..4c33c0782 --- /dev/null +++ b/web_assets_warmup/readme/DESCRIPTION.rst @@ -0,0 +1,9 @@ +Ensure that assets are generated and stored in the DB when Odoo starts + +If the assets from the database are not up-to-date, they are regenerated by +Odoo when we print a report, but to do so Odoo forces the commit, so if an +exception occurs after (or during) the report rendering, it let the database in +a broken state (picking have been validated in this case). + +To prevent this issue, we need to ensure that the assets are well-generated +when Odoo starts, not when the report is printed. diff --git a/web_assets_warmup/static/description/index.html b/web_assets_warmup/static/description/index.html new file mode 100644 index 000000000..409b89234 --- /dev/null +++ b/web_assets_warmup/static/description/index.html @@ -0,0 +1,428 @@ + + + + + + +Generate assets when Odoo starts + + + +
+

Generate assets when Odoo starts

+ + +

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runboat

+

Ensure that assets are generated and stored in the DB when Odoo starts

+

If the assets from the database are not up-to-date, they are regenerated by +Odoo when we print a report, but to do so Odoo forces the commit, so if an +exception occurs after (or during) the report rendering, it let the database in +a broken state (picking have been validated in this case).

+

To prevent this issue, we need to ensure that the assets are well-generated +when Odoo starts, not when the report is printed.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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/web project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ +