diff --git a/mayan/apps/document_indexing/tasks.py b/mayan/apps/document_indexing/tasks.py index f405ddb1d7..4a3e0818de 100644 --- a/mayan/apps/document_indexing/tasks.py +++ b/mayan/apps/document_indexing/tasks.py @@ -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() diff --git a/mayan/apps/lock_manager/managers.py b/mayan/apps/lock_manager/managers.py index 01a7405501..e5ccf338e0 100644 --- a/mayan/apps/lock_manager/managers.py +++ b/mayan/apps/lock_manager/managers.py @@ -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