Skip to content

Commit

Permalink
Add the function to check if there is an existing lock for the key
Browse files Browse the repository at this point in the history
  • Loading branch information
jfan666 committed Jun 6, 2018
1 parent 0559736 commit 1ad45b5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pytest_lab/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def __init__(self, config, backend, ttl=30):
def aquire(self, key, wait_on_locks, user=None):
record = self.backend.read(key)

def check_record(record):
def check_lock(record):
if record and record.ttl:
logger.error(
'{} is locked by {}, waiting {} seconds for re-checking '
'lock to expire...'.format(key, record.value, record.ttl + 1))
'{} is locked by {}, waiting {} seconds for lock '
'to expire...'.format(key, record.value, record.ttl + 1))
start = time.time()
while time.time() - start < record.ttl + 1:
record = self.backend.read(key)
Expand All @@ -139,14 +139,14 @@ def check_record(record):
else:
return True

if not check_record(record):
if wait_on_locks:
while True:
if check_record(record):
break
else:
# Check if the key is locked or not
if not check_lock(record):
if not wait_on_locks:
raise ResourceLocked(
'{} is currently locked by {}'.format(key, record.value))
while True:
if check_lock(record):
break

# acquire
lockid = get_lock_id(user)
Expand Down

0 comments on commit 1ad45b5

Please sign in to comment.