Skip to content

Commit 5515cf3

Browse files
committed
Add ddmrp_cron_actions_as_job
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
1 parent 5450b75 commit 5515cf3

14 files changed

Lines changed: 129 additions & 0 deletions

File tree

ddmrp/tests/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ class TestDdmrpCommon(common.SavepointCase):
1212
def setUpClass(cls):
1313
super().setUpClass()
1414

15+
cls.env = cls.env(
16+
context=dict(
17+
cls.env.context,
18+
tracking_disable=True,
19+
# compatibility with ddmrp_cron_actions_as_job,
20+
# that would delay calls to "cron_actions" in these tests
21+
test_queue_job_no_delay=True,
22+
)
23+
)
24+
1525
# Models
1626
cls.productModel = cls.env["product.product"]
1727
cls.templateModel = cls.env["product.template"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Camptocamp (https://www.camptocamp.com)
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
3+
4+
{
5+
"name": "DDMRP Buffer Calculation as job",
6+
"version": "13.0.1.0.0",
7+
"summary": "Run DDMRP Buffer Calculation as jobs",
8+
"author": "Camptocamp, Odoo Community Association (OCA)",
9+
"website": "https://github.com/OCA/ddmrp",
10+
"category": "Warehouse Management",
11+
"depends": ["ddmrp", "queue_job"],
12+
"data": ["data/queue_job_channel_data.xml", "data/queue_job_function_data.xml"],
13+
"license": "LGPL-3",
14+
"installable": True,
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<odoo noupdate="1">
2+
<record model="queue.job.channel" id="channel_ddmrp">
3+
<field name="name">ddmrp</field>
4+
<field name="parent_id" ref="queue_job.channel_root" />
5+
</record>
6+
</odoo>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<odoo noupdate="1">
2+
<record id="job_function_stock_buffer_cron_actions" model="queue.job.function">
3+
<field name="name"><![CDATA[<stock.buffer>.cron_actions]]></field>
4+
<field name="channel_id" ref="channel_ddmrp" />
5+
</record>
6+
</odoo>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import stock_buffer
2+
from . import stock_move
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2020 Camptocamp (https://www.camptocamp.com)
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
3+
4+
from odoo import models
5+
6+
from odoo.addons.queue_job.job import identity_exact
7+
8+
9+
class Buffer(models.Model):
10+
_inherit = "stock.buffer"
11+
12+
def cron_actions_job_options(self, only_nfp=False):
13+
return {
14+
"identity_key": identity_exact,
15+
"priority": 15,
16+
"description": "DDMRP Buffer calculation ({})".format(self.display_name),
17+
}
18+
19+
def _register_hook(self):
20+
self._patch_method(
21+
"cron_actions",
22+
self._patch_job_auto_delay(
23+
"cron_actions", context_key="auto_delay_ddmrp_cron_actions"
24+
),
25+
)
26+
return super()._register_hook()
27+
28+
def cron_ddmrp(self, automatic=False):
29+
return super(
30+
Buffer, self.with_context(auto_delay_ddmrp_cron_actions=True)
31+
).cron_ddmrp(automatic=automatic)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2020 Camptocamp
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
3+
4+
from odoo import models
5+
6+
7+
class StockMove(models.Model):
8+
_inherit = "stock.move"
9+
10+
def _update_ddmrp_nfp(self):
11+
return super(
12+
StockMove, self.with_context(auto_delay_ddmrp_cron_actions=True)
13+
)._update_ddmrp_nfp()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Guewen Baconnier <guewen.baconnier@camptocamp.com>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DDMRP Buffer calculations are now run with Queue Jobs.
2+
3+
When auto-update of NFP is active, each time the state of a stock move changes,
4+
a new computation is triggered, but thanks to identity keys on jobs, only one
5+
job at a time is generated for the same buffer.

0 commit comments

Comments
 (0)