def __init__(self, redisSettings=globalRedisSettings): self.redisHostname = redisSettings['redisHostname'] self.redisPort = redisSettings['redisPort'] self.redisDb = redisSettings['redisDb'] self.prefixInRedis = redisSettings['prefixInRedis'] self.checkInterval = redisSettings['checkInterval'] self.redisConnection = redis.Redis(host=self.redisHostname, port=self.redisPort, db=self.redisDb) self.lock = RedisLock(self.redisConnection, lock_key='LOCK:WebSiteStatusService')
def _do_execute_query(parameterized_query, wlm_queue=None): # This method should always be getting executed within a Lambda context # Distributed dog pile lock pattern # From: https://pypi.python.org/pypi/python-redis-lock log.info("About to attempt acquiring lock...") redis_client = redshift.get_redshift_cache_redis_client() with RedisLock(redis_client, parameterized_query.cache_key, expire=300): # Get a lock with a 5-minute lifetime since that's the maximum duration of a Lambda # to ensure the lock is held for as long as the Python process / Lambda is running. log.info("Lock acquired.") return _do_execute_query_work(parameterized_query, wlm_queue)
def set(self, key, expiration): self.lock = RedisLock(self.redis, lock_key='cyclops:lock:%s' % key, lock_timeout=5*60) if not self.lock.acquire(): return try: self.redis.setex( key, expiration, 0 ) finally: self.lock.release()