def cluster_lock_steal(cluster_id, action_id): with session_for_write() as session: lock = session.query(models.ClusterLock).get(cluster_id) if lock is not None: lock.action_ids = [action_id] lock.semaphore = -1 lock.save(session) else: lock = models.ClusterLock(cluster_id=cluster_id, action_ids=[action_id], semaphore=-1) session.add(lock) return lock.action_ids
def cluster_lock_acquire(cluster_id, action_id, scope): '''Acquire lock on a cluster. :param cluster_id: ID of the cluster. :param action_id: ID of the action that attempts to lock the cluster. :param scope: +1 means a node-level operation lock; -1 indicates a cluster-level lock. :return: A list of action IDs that currently works on the cluster. ''' with session_for_write() as session: lock = session.query(models.ClusterLock).get(cluster_id) if lock is not None: if scope == 1 and lock.semaphore > 0: if action_id not in lock.action_ids: lock.action_ids.append(six.text_type(action_id)) lock.semaphore += 1 lock.save(session) else: lock = models.ClusterLock(cluster_id=cluster_id, action_ids=[six.text_type(action_id)], semaphore=scope) session.add(lock) return lock.action_ids