Skip to content

Commit fa12184

Browse files
committed
[IMP] shopfloor_base: Allow changing the way locks are acquired
This commit adds two new system parameters: * shopfloor.lock.for_update.no_key: Can be defined with a True-ish value to use FOR NO KEY UPDATE in the select to acquire a lock on selected rows * shopfloor.lock.for_update.no_wait: Can be defined with a True-ish value to use NOWAIT and raise an error instead of waiting for the lock to be acquired. Note that NOWAIT keyword cannot be use in conjunction with SKIP LOCKED, hence, if skip_locked arg is used, the value of no_wait parameter will be ignored.
1 parent 5e3c4da commit fa12184

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

shopfloor_base/actions/lock.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import hashlib
55
import struct
66

7+
from odoo.tools import str2bool
8+
79
from odoo.addons.component.core import Component
810

911

@@ -38,9 +40,22 @@ def for_update(self, records, log_exceptions=False, skip_locked=False):
3840
be obtained.
3941
4042
"""
41-
query = "SELECT id FROM %s WHERE ID IN %%s FOR UPDATE"
43+
no_key = str2bool(
44+
self.env["ir.config_parameter"]
45+
.sudo()
46+
.get_param("shopfloor.lock.for_update.no_key")
47+
)
48+
no_wait = str2bool(
49+
self.env["ir.config_parameter"]
50+
.sudo()
51+
.get_param("shopfloor.lock.for_update.no_wait")
52+
)
53+
for_update_str = " FOR NO KEY UPDATE " if no_key else " FOR UPDATE "
54+
query = "SELECT id FROM %s WHERE ID IN %%s " + for_update_str
4255
if skip_locked:
4356
query += " SKIP LOCKED"
57+
elif no_wait:
58+
query += " NOWAIT"
4459
sql = query % records._table
4560
self.env.cr.execute(sql, (tuple(records.ids),), log_exceptions=log_exceptions)
4661
if skip_locked:

0 commit comments

Comments
 (0)