X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=multivimbroker%2Fmultivimbroker%2Fpub%2Futils%2Fshare_lock.py;h=e8eef8a7874957d6fdfc1f3123c08ab3369d8b87;hb=refs%2Fchanges%2F31%2F28131%2F1;hp=b594c68f1cc15b1a4a6275f978734cf22289245c;hpb=b1b0386b5ed988fbbf25e326c3d0ce951eff07cd;p=multicloud%2Fframework.git diff --git a/multivimbroker/multivimbroker/pub/utils/share_lock.py b/multivimbroker/multivimbroker/pub/utils/share_lock.py index b594c68..e8eef8a 100755 --- a/multivimbroker/multivimbroker/pub/utils/share_lock.py +++ b/multivimbroker/multivimbroker/pub/utils/share_lock.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2017 Wind River Systems, Inc. +# Copyright (c) 2017-2018 VMware, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,21 +16,26 @@ import time import redis -from multivimbroker.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD +from multivimbroker.pub.config.config import REDIS_HOST +from multivimbroker.pub.config.config import REDIS_PORT +from multivimbroker.pub.config.config import REDIS_PASSWD class SharedLock: - def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWD, db=9, lock_timeout=5 * 60): + def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT, + password=REDIS_PASSWD, db=9, lock_timeout=5 * 60): self.lock_key = lock_key self.lock_timeout = lock_timeout - self.redis = redis.Redis(host=host, port=port, db=db, password=password) + self.redis = redis.Redis(host=host, port=port, + db=db, password=password) self.acquire_time = -1 def acquire(self): begin = now = int(time.time()) while (now - begin) < self.lock_timeout: - result = self.redis.setnx(self.lock_key, now + self.lock_timeout + 1) + result = self.redis.setnx(self.lock_key, now + + self.lock_timeout + 1) if result == 1 or result is True: self.acquire_time = now return True @@ -42,7 +48,8 @@ class SharedLock: current_lock_timestamp = int(current_lock_timestamp) if now > current_lock_timestamp: - next_lock_timestamp = self.redis.getset(self.lock_key, now + self.lock_timeout + 1) + next_lock_timestamp = self.redis.getset(self.lock_key, now + + self.lock_timeout + 1) if not next_lock_timestamp: time.sleep(1) continue