[FIX] stock: perf run scheduler multi company

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 <ryv@odoo.com>" for finding this issue.

opw-2328707

closes odoo/odoo#61183

X-original-commit: a21f9876b5b9d01380d03d638deb48cd091f26e7
Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
This commit is contained in:
Arnold Moyaux
2020-11-02 15:16:06 +00:00
parent d36c51d082
commit a021d627f1
2 changed files with 11 additions and 8 deletions
+2 -2
View File
@@ -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
+9 -6
View File
@@ -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):