Add method to check for a lock's existance and force it to release if stale.

This commit is contained in:
Roberto Rosario
2015-08-01 01:00:26 -04:00
parent 8382df91a6
commit 7ddc4c1c04
2 changed files with 19 additions and 1 deletions

View File

@@ -49,3 +49,21 @@ class LockManager(models.Manager):
else:
logger.debug('acquired lock: %s', name)
return lock
def check_existing(self, **kwargs):
try:
existing_lock = self.get(**kwargs)
except self.model.DoesNotExist:
return False
else:
# Lock exists, try to re-acquire it in case it is a stale lock
try:
lock = self.acquire_lock(existing_lock.name)
except LockError:
# This is expected, try to acquire it to force it to
# timeout in case it is a stale lock.
return True
else:
# Able to re-acquire anothers lock, so we release it now
lock.release()
return False