From 50239d77e4c868ee438c78a6aeb2a41ad06d5e6d Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Thu, 29 Oct 2020 09:04:43 +0100 Subject: [PATCH] Add ddmrp_cron_actions_as_job It makes every calls to "cron_actions" run in queue jobs. The jobs have an identity key with "identity_exact", meaning that only one occurence of a job for the same buffer with the same arguments (only_nfp) will be created at a time (e.g. when the state of a stock.move is changed several times in the same transaction or in a different transaction in a short timeframe). It needs https://github.com/OCA/queue/pull/274 and https://github.com/OCA/queue/pull/275 --- ddmrp/tests/common.py | 10 ++++++ ddmrp_cron_actions_as_job/__init__.py | 1 + ddmrp_cron_actions_as_job/__manifest__.py | 15 +++++++++ ddmrp_cron_actions_as_job/models/__init__.py | 1 + .../models/stock_buffer.py | 21 +++++++++++++ .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 5 +++ ddmrp_cron_actions_as_job/tests/__init__.py | 1 + .../tests/test_cron_actions_as_job.py | 31 +++++++++++++++++++ .../odoo/addons/ddmrp_cron_actions_as_job | 1 + setup/ddmrp_cron_actions_as_job/setup.py | 6 ++++ 11 files changed, 93 insertions(+) create mode 100644 ddmrp_cron_actions_as_job/__init__.py create mode 100644 ddmrp_cron_actions_as_job/__manifest__.py create mode 100644 ddmrp_cron_actions_as_job/models/__init__.py create mode 100644 ddmrp_cron_actions_as_job/models/stock_buffer.py create mode 100644 ddmrp_cron_actions_as_job/readme/CONTRIBUTORS.rst create mode 100644 ddmrp_cron_actions_as_job/readme/DESCRIPTION.rst create mode 100644 ddmrp_cron_actions_as_job/tests/__init__.py create mode 100644 ddmrp_cron_actions_as_job/tests/test_cron_actions_as_job.py create mode 120000 setup/ddmrp_cron_actions_as_job/odoo/addons/ddmrp_cron_actions_as_job create mode 100644 setup/ddmrp_cron_actions_as_job/setup.py diff --git a/ddmrp/tests/common.py b/ddmrp/tests/common.py index 267905294..5157f2c0f 100644 --- a/ddmrp/tests/common.py +++ b/ddmrp/tests/common.py @@ -12,6 +12,16 @@ class TestDdmrpCommon(common.SavepointCase): def setUpClass(cls): super().setUpClass() + cls.env = cls.env( + context=dict( + cls.env.context, + tracking_disable=True, + # compatibility with ddmrp_cron_actions_as_job, + # that would delay calls to "cron_actions" in these tests + test_queue_job_no_delay=True, + ) + ) + # Models cls.productModel = cls.env["product.product"] cls.templateModel = cls.env["product.template"] diff --git a/ddmrp_cron_actions_as_job/__init__.py b/ddmrp_cron_actions_as_job/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/ddmrp_cron_actions_as_job/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/ddmrp_cron_actions_as_job/__manifest__.py b/ddmrp_cron_actions_as_job/__manifest__.py new file mode 100644 index 000000000..d458645a5 --- /dev/null +++ b/ddmrp_cron_actions_as_job/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Camptocamp (https://www.camptocamp.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +{ + "name": "DDMRP Buffer Calculation as job", + "version": "13.0.1.0.0", + "summary": "Run DDMRP Buffer Calculation as jobs", + "author": "Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/ddmrp", + "category": "Warehouse Management", + "depends": ["ddmrp", "queue_job"], + "data": [], + "license": "LGPL-3", + "installable": True, +} diff --git a/ddmrp_cron_actions_as_job/models/__init__.py b/ddmrp_cron_actions_as_job/models/__init__.py new file mode 100644 index 000000000..46f971b6d --- /dev/null +++ b/ddmrp_cron_actions_as_job/models/__init__.py @@ -0,0 +1 @@ +from . import stock_buffer diff --git a/ddmrp_cron_actions_as_job/models/stock_buffer.py b/ddmrp_cron_actions_as_job/models/stock_buffer.py new file mode 100644 index 000000000..bf18fb59a --- /dev/null +++ b/ddmrp_cron_actions_as_job/models/stock_buffer.py @@ -0,0 +1,21 @@ +# Copyright 2020 Camptocamp (https://www.camptocamp.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo import models + +from odoo.addons.queue_job.job import identity_exact + + +class Buffer(models.Model): + _inherit = "stock.buffer" + + def cron_actions_job_options(self, only_nfp=False): + return { + "identity_key": identity_exact, + "priority": 15, + "description": "DDMRP Buffer calculation ({})".format(self.display_name), + } + + def _register_hook(self): + self._patch_method("cron_actions", self._patch_job_auto_delay("cron_actions")) + return super()._register_hook() diff --git a/ddmrp_cron_actions_as_job/readme/CONTRIBUTORS.rst b/ddmrp_cron_actions_as_job/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..48286263c --- /dev/null +++ b/ddmrp_cron_actions_as_job/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Guewen Baconnier diff --git a/ddmrp_cron_actions_as_job/readme/DESCRIPTION.rst b/ddmrp_cron_actions_as_job/readme/DESCRIPTION.rst new file mode 100644 index 000000000..cb90ff2f3 --- /dev/null +++ b/ddmrp_cron_actions_as_job/readme/DESCRIPTION.rst @@ -0,0 +1,5 @@ +DDMRP Buffer calculations are now run with Queue Jobs. + +When auto-update of NFP is active, each time the state of a stock move changes, +a new computation is triggered, but thanks to identity keys on jobs, only one +job at a time is generated for the same buffer. diff --git a/ddmrp_cron_actions_as_job/tests/__init__.py b/ddmrp_cron_actions_as_job/tests/__init__.py new file mode 100644 index 000000000..48aece491 --- /dev/null +++ b/ddmrp_cron_actions_as_job/tests/__init__.py @@ -0,0 +1 @@ +from . import test_cron_actions_as_job diff --git a/ddmrp_cron_actions_as_job/tests/test_cron_actions_as_job.py b/ddmrp_cron_actions_as_job/tests/test_cron_actions_as_job.py new file mode 100644 index 000000000..bc9c2be28 --- /dev/null +++ b/ddmrp_cron_actions_as_job/tests/test_cron_actions_as_job.py @@ -0,0 +1,31 @@ +# Copyright 2020 Camptocamp (https://www.camptocamp.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + + +from odoo.addons.ddmrp.tests.common import TestDdmrpCommon +from odoo.addons.queue_job.job import identity_exact +from odoo.addons.queue_job.tests.common import mock_with_delay + + +class TestDdmrpCronActionsAsJob(TestDdmrpCommon): + def test_cron_actions_delay_job(self): + context = dict(self.env.context) + del context["test_queue_job_no_delay"] + buffer_a = self.buffer_a.with_context(context) + + with mock_with_delay() as (delayable_cls, delayable): + buffer_a.cron_actions(only_nfp=True) + + # check 'with_delay()' part: + self.assertEqual(delayable_cls.call_count, 1) + # arguments passed in 'with_delay()' + delay_args, delay_kwargs = delayable_cls.call_args + self.assertEqual(delay_args, (self.buffer_a,)) + self.assertEqual(delay_kwargs.get("priority"), 15) + self.assertEqual(delay_kwargs.get("identity_key"), identity_exact) + + # check what's passed to the job method 'cron_actions' + self.assertEqual(delayable.cron_actions.call_count, 1) + delay_args, delay_kwargs = delayable.cron_actions.call_args + self.assertEqual(delay_args, ()) + self.assertDictEqual(delay_kwargs, {"only_nfp": True}) diff --git a/setup/ddmrp_cron_actions_as_job/odoo/addons/ddmrp_cron_actions_as_job b/setup/ddmrp_cron_actions_as_job/odoo/addons/ddmrp_cron_actions_as_job new file mode 120000 index 000000000..41d508575 --- /dev/null +++ b/setup/ddmrp_cron_actions_as_job/odoo/addons/ddmrp_cron_actions_as_job @@ -0,0 +1 @@ +../../../../ddmrp_cron_actions_as_job \ No newline at end of file diff --git a/setup/ddmrp_cron_actions_as_job/setup.py b/setup/ddmrp_cron_actions_as_job/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/ddmrp_cron_actions_as_job/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)