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

@@ -74,7 +74,7 @@ def task_index_document(self, document_id):
@app.task(bind=True, default_retry_delay=RETRY_DELAY, ignore_result=True)
def task_do_rebuild_all_indexes(self):
if Lock.filter(name__startswith='document_indexing_task_update_index_document'):
if Lock.check_existing(name__startswith='document_indexing_task_update_index_document'):
# A document index update is happening, wait
raise self.retry()

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