def acquire_lock(task_id, user_id, limit, timeout, pipeline=None, execute=True): redis_conn = sentinel.master pipeline = pipeline or redis_conn.pipeline(transaction=True) lock_manager = LockManager(redis_conn, timeout) task_users_key = get_task_users_key(task_id) user_tasks_key = get_user_tasks_key(user_id) if lock_manager.acquire_lock(task_users_key, user_id, limit, pipeline=pipeline): lock_manager.acquire_lock(user_tasks_key, task_id, float('inf'), pipeline=pipeline) if execute: return all(not isinstance(r, Exception) for r in pipeline.execute()) return True return False
def release_lock(task_id, user_id, timeout, pipeline=None, execute=True): redis_conn = sentinel.master pipeline = pipeline or redis_conn.pipeline(transaction=True) lock_manager = LockManager(redis_conn, timeout) task_users_key = get_task_users_key(task_id) user_tasks_key = get_user_tasks_key(user_id) lock_manager.release_lock(task_users_key, user_id, pipeline=pipeline) lock_manager.release_lock(user_tasks_key, task_id, pipeline=pipeline) project_ids = get_task_ids_project_id([task_id]) remaining_user_tasks_id = [t for t in get_user_tasks(user_id, timeout).keys() if t != str(task_id)] if project_ids: if project_ids[0] not in get_task_ids_project_id(remaining_user_tasks_id): unregister_active_user(project_ids[0], user_id, sentinel.master) if execute: pipeline.execute()
def get_locks(task_id, timeout): lock_manager = LockManager(sentinel.master, timeout) task_users_key = get_task_users_key(task_id) return lock_manager.get_locks(task_users_key)
def has_lock(task_id, user_id, timeout): lock_manager = LockManager(sentinel.master, timeout) task_users_key = get_task_users_key(task_id) return lock_manager.has_lock(task_users_key, user_id)