diff --git a/networkapi/distributedlock/__init__.py b/networkapi/distributedlock/__init__.py index 388237095..692988f40 100644 --- a/networkapi/distributedlock/__init__.py +++ b/networkapi/distributedlock/__init__.py @@ -117,3 +117,6 @@ def __enter__(self): def __exit__(self, type, value, traceback): _debug('releasing lock %s' % self.key) self.lock.release() + + def get_cached_data(self): + return self.lock.get_cached_data() diff --git a/networkapi/distributedlock/memcachedlock.py b/networkapi/distributedlock/memcachedlock.py index 8f29f45a0..ad3e426d3 100644 --- a/networkapi/distributedlock/memcachedlock.py +++ b/networkapi/distributedlock/memcachedlock.py @@ -69,3 +69,8 @@ def release(self): else: log.warning( "I've no lock to release. Increase TIMEOUT of lock operations") + + def get_cached_data(self): + value = self.client.get(self.key) + + return value if value else None diff --git a/networkapi/util/geral.py b/networkapi/util/geral.py index d364f104f..e8d68342f 100644 --- a/networkapi/util/geral.py +++ b/networkapi/util/geral.py @@ -72,6 +72,9 @@ def create_lock_with_blocking(locks_name): for lock_name in locks_name: try: lock = distributedlock(lock_name, blocking=False) + if lock.get_cached_data(): + log.info('Get cached lock data for {}. Disabling it and creating a new lock'.format(lock_name)) + lock.__exit__('', '', '') lock.__enter__() locks_list.append(lock) except LockNotAcquiredError: