def greenlet_queuestats(self): interval = min(self.config["orchestrate_interval"], 1 * 60) lock_timeout = 5 * 60 + (interval * 2) while True: lock = LuaLock(connections.redis, self.redis_queuestats_lock_key, timeout=lock_timeout, thread_local=False, blocking=False) with lock: lock_expires = time.time() + lock_timeout self.queue_etas = defaultdict(lambda: MovingETA(5)) while True: self.queuestats() # Because queue stats can be expensive, we try to keep the lock on the same agent lock_extend = (time.time() + lock_timeout) - lock_expires lock_expires += lock_extend lock.extend(lock_extend) time.sleep(interval) time.sleep(interval)
def greenlet_scheduler(self): redis_scheduler_lock_key = "%s:schedulerlock" % get_current_config()["redis_prefix"] while True: with LuaLock(connections.redis, redis_scheduler_lock_key, timeout=self.config["scheduler_interval"] + 10, blocking=False, thread_local=False): self.scheduler.check() time.sleep(self.config["scheduler_interval"])
def release_lock(lock_name, token): """ Release a lock Args: lock_name (str): The lock key in redis token (bytes): The unique id used Returns: bool: True if the lock was successfully released """ # this is a StrictRedis instance, we need this for the script installation that LuaLock uses redis = caches['redis'].client.get_client() lock = LuaLock(redis, lock_name) try: lock.do_release(token) except LockError: # If the lock is expired we don't want to raise an error pass
def _get_lock(lock_name, expiration): """ Creates a new redis LuaLock Args: lock_name (str): The name of the lock expiration (datetime.datetime): The expiration datetime Returns: redis.lock.Lock: a redis lua-based lock """ timeout = int((expiration - now_in_utc()).total_seconds()) # this is a StrictRedis instance, we need this for the script installation that LuaLock uses redis = caches['redis'].client.get_client() # don't block acquiring the lock, the task will need to try again later return LuaLock(redis, lock_name, timeout=timeout, blocking=False, thread_local=False)