Skip to content

Commit

Permalink
Add ddmrp_cron_actions_as_job
Browse files Browse the repository at this point in the history
It makes 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 OCA/queue#274 and
OCA/queue#275
  • Loading branch information
guewen committed Nov 2, 2020
1 parent 5450b75 commit 5515cf3
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ddmrp/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions ddmrp_cron_actions_as_job/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions ddmrp_cron_actions_as_job/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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": ["data/queue_job_channel_data.xml", "data/queue_job_function_data.xml"],
"license": "LGPL-3",
"installable": True,
}
6 changes: 6 additions & 0 deletions ddmrp_cron_actions_as_job/data/queue_job_channel_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<odoo noupdate="1">
<record model="queue.job.channel" id="channel_ddmrp">
<field name="name">ddmrp</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>
</odoo>
6 changes: 6 additions & 0 deletions ddmrp_cron_actions_as_job/data/queue_job_function_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<odoo noupdate="1">
<record id="job_function_stock_buffer_cron_actions" model="queue.job.function">
<field name="name"><![CDATA[<stock.buffer>.cron_actions]]></field>
<field name="channel_id" ref="channel_ddmrp" />
</record>
</odoo>
2 changes: 2 additions & 0 deletions ddmrp_cron_actions_as_job/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import stock_buffer
from . import stock_move
31 changes: 31 additions & 0 deletions ddmrp_cron_actions_as_job/models/stock_buffer.py
Original file line number Diff line number Diff line change
@@ -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 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", context_key="auto_delay_ddmrp_cron_actions"
),
)
return super()._register_hook()

def cron_ddmrp(self, automatic=False):
return super(
Buffer, self.with_context(auto_delay_ddmrp_cron_actions=True)
).cron_ddmrp(automatic=automatic)
13 changes: 13 additions & 0 deletions ddmrp_cron_actions_as_job/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2020 Camptocamp
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import models


class StockMove(models.Model):
_inherit = "stock.move"

def _update_ddmrp_nfp(self):
return super(
StockMove, self.with_context(auto_delay_ddmrp_cron_actions=True)
)._update_ddmrp_nfp()
1 change: 1 addition & 0 deletions ddmrp_cron_actions_as_job/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Guewen Baconnier <[email protected]>
5 changes: 5 additions & 0 deletions ddmrp_cron_actions_as_job/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions ddmrp_cron_actions_as_job/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_cron_actions_as_job
31 changes: 31 additions & 0 deletions ddmrp_cron_actions_as_job/tests/test_cron_actions_as_job.py
Original file line number Diff line number Diff line change
@@ -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, auto_delay_ddmrp_cron_actions=True)
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})
6 changes: 6 additions & 0 deletions setup/ddmrp_cron_actions_as_job/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 5515cf3

Please sign in to comment.