From a021d627f1296fbc805d2ed7ebc3e394aea59711 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Fri, 25 Sep 2020 15:42:52 +0000 Subject: [PATCH] [FIX] stock: perf run scheduler multi company MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Usecase to reproduce: - 10 company having 10 000 moves in state 'confirmed', 'partially_available' - Run scheduler manually (without cron) It will takes a huge amount of time. It happens because the run scheduler wizard will call run_scheduler as sudo for each company (passing company as arg). run_scheduler will try to assign move that could be reserved. However the domain in order to search move do not include the company_id and search for every company. Following upper usecase: Instead of running action_assign 10 times on 10 000 moves, it will run action_assign 10 times on 100 000 moves. Databases with multi companies and having a lot of stock will have a scheduler speedup from this patch. Thanks "Rémy Voet " for finding this issue. opw-2328707 closes odoo/odoo#61183 X-original-commit: a21f9876b5b9d01380d03d638deb48cd091f26e7 Signed-off-by: Arnold Moyaux --- addons/mrp/models/stock_rule.py | 4 ++-- addons/stock/models/stock_rule.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/addons/mrp/models/stock_rule.py b/addons/mrp/models/stock_rule.py index 8da658867f3..24d1c4f16c2 100644 --- a/addons/mrp/models/stock_rule.py +++ b/addons/mrp/models/stock_rule.py @@ -172,7 +172,7 @@ class ProcurementGroup(models.Model): procurements_without_kit.append(procurement) return super(ProcurementGroup, self).run(procurements_without_kit, raise_user_error=raise_user_error) - def _get_moves_to_assign_domain(self): - domain = super(ProcurementGroup, self)._get_moves_to_assign_domain() + def _get_moves_to_assign_domain(self, company_id): + domain = super(ProcurementGroup, self)._get_moves_to_assign_domain(company_id) domain = expression.AND([domain, [('production_id', '=', False)]]) return domain diff --git a/addons/stock/models/stock_rule.py b/addons/stock/models/stock_rule.py index 00650716e7d..0ba946d9a5e 100644 --- a/addons/stock/models/stock_rule.py +++ b/addons/stock/models/stock_rule.py @@ -482,11 +482,14 @@ class ProcurementGroup(models.Model): ('product_id', '=', values['product_id'].id)] @api.model - def _get_moves_to_assign_domain(self): - return expression.AND([ - [('state', 'in', ['confirmed', 'partially_available'])], - [('product_uom_qty', '!=', 0.0)] - ]) + def _get_moves_to_assign_domain(self, company_id): + moves_domain = [ + ('state', 'in', ['confirmed', 'partially_available']), + ('product_uom_qty', '!=', 0.0) + ] + if company_id: + moves_domain = expression.AND([[('company_id', '=', company_id)], moves_domain]) + return moves_domain @api.model def _run_scheduler_tasks(self, use_new_cursor=False, company_id=False): @@ -496,7 +499,7 @@ class ProcurementGroup(models.Model): orderpoints.sudo()._procure_orderpoint_confirm(use_new_cursor=use_new_cursor, company_id=company_id, raise_user_error=False) # Search all confirmed stock_moves and try to assign them - domain = self._get_moves_to_assign_domain() + domain = self._get_moves_to_assign_domain(company_id) moves_to_assign = self.env['stock.move'].search(domain, limit=None, order='priority desc, date asc') for moves_chunk in split_every(100, moves_to_assign.ids):