Use non-blocking mutex on registry CLI with retry on error
This commit is contained in:
parent
5f8e809b2d
commit
59e12f0ab1
@ -1,9 +1,12 @@
|
|||||||
import fcntl
|
import fcntl
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
import time
|
||||||
|
|
||||||
# Define default file path for the lock
|
# Define default file path for the lock
|
||||||
DEFAULT_LOCK_FILE_PATH = "/tmp/registry_mutex_lock_file"
|
DEFAULT_LOCK_FILE_PATH = "/tmp/registry_mutex_lock_file"
|
||||||
|
|
||||||
|
LOCK_RETRY_INTERVAL = 3
|
||||||
|
|
||||||
|
|
||||||
def registry_mutex():
|
def registry_mutex():
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@ -13,16 +16,25 @@ def registry_mutex():
|
|||||||
if self.mutex_lock_file:
|
if self.mutex_lock_file:
|
||||||
lock_file_path = self.mutex_lock_file
|
lock_file_path = self.mutex_lock_file
|
||||||
|
|
||||||
|
result = None
|
||||||
with open(lock_file_path, 'w') as lock_file:
|
with open(lock_file_path, 'w') as lock_file:
|
||||||
try:
|
while True:
|
||||||
# Try to acquire the lock
|
try:
|
||||||
fcntl.flock(lock_file, fcntl.LOCK_EX)
|
# Try to acquire the lock
|
||||||
|
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
print(f"Registry lock acquired, {lock_file_path}")
|
||||||
|
|
||||||
# Call the actual function
|
# Call the actual function
|
||||||
result = func(self, *args, **kwargs)
|
result = func(self, *args, **kwargs)
|
||||||
finally:
|
break
|
||||||
# Always release the lock
|
except BlockingIOError:
|
||||||
fcntl.flock(lock_file, fcntl.LOCK_UN)
|
# Retry on error
|
||||||
|
print(f"Not able to acquire lock on {lock_file_path}, retrying in {LOCK_RETRY_INTERVAL}s...")
|
||||||
|
time.sleep(LOCK_RETRY_INTERVAL)
|
||||||
|
|
||||||
|
# Always release the lock
|
||||||
|
fcntl.flock(lock_file, fcntl.LOCK_UN)
|
||||||
|
print(f"Registry lock released, {lock_file_path}")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user