Skip to content

Commit

Permalink
proc_auto_create_grp_by_product: fix raised error
Browse files Browse the repository at this point in the history
Raise correct error to allow correct catching and retry
  • Loading branch information
jbaudoux committed Apr 6, 2024
1 parent 6cdd79b commit 40e94f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 10 additions & 4 deletions procurement_auto_create_group_by_product/models/stock_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import struct

from odoo import fields, models
from odoo.exceptions import UserError


class StockRule(models.Model):
Expand All @@ -28,9 +27,16 @@ def _get_auto_procurement_group(self, product):
)
lock_acquired = self.env.cr.fetchone()[0]
if not lock_acquired:
raise UserError(
f"The auto procurement group for product {product.name} "
"is already being created by someone else."
# This error will be catched by odoo or the job queue
# runner and will cause a retry
self.env.cr.execute(
"""
do $$
begin
raise SQLSTATE '55P03';
end
$$ language plpgsql;
"""
)
return super()._get_auto_procurement_group(product)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2023 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import psycopg2

from odoo import api, registry
from odoo.exceptions import UserError
from odoo.service.model import PG_CONCURRENCY_ERRORS_TO_RETRY

from odoo.addons.procurement_auto_create_group.tests.test_auto_create import (
TestProcurementAutoCreateGroup,
Expand Down Expand Up @@ -135,9 +137,6 @@ def test_concurrent_procurement_group_creation(self):
rule2.auto_create_group_by_product = True
product2 = new_env["product.product"].browse(product.id)
self.assertFalse(product2.auto_create_procurement_group_ids)
exception_msg = (
f"The auto procurement group for product {product2.name} "
"is already being created by someone else."
)
with self.assertRaisesRegex(UserError, exception_msg):
with self.assertRaises(psycopg2.OperationalError) as cm:
rule2._get_auto_procurement_group(product2)
self.assertTrue(cm.pgcode in PG_CONCURRENCY_ERRORS_TO_RETRY)

0 comments on commit 40e94f2

Please sign in to comment.