def __init__(self, label): """Init the query :param label: Name of the model, label of the node :type label: str """ self.label = label # Params to be passed to neo self.params = {} self._count = None # List of relationship time conditions self.time_wheres = [] # List of filter conditions self.filter_count = 0 self.filter_wheres = [] self.matches = [] self.rels = [] self.state_matches = [] self.return_labels = [] self.start_path() self.time(utils.milliseconds_now()) # List of order by property and direction tuples self._orderby = [] self._skip = None self._limit = None
def time(self, timestamp): """Update the time parameter :param timestamp: New timestamp :type timestamp: int :returns: Modified self :rtype: Query """ if timestamp is None: timestamp = utils.milliseconds_now() self.params['time'] = timestamp return self
def release(cls, session, uuid, key): """Releases the lock on an environment. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param account_number: Environment account number :type account_number: str :param name: Environment name :type name: str :param key: Time of the lock in milliseconds :type key: int :returns: True for lock released or no action, False otherwise :rtype: bool """ release_time = utils.milliseconds_now() with session.begin_transaction() as tx: instance = cls.find_transaction(tx, uuid) # Check for empty result if instance is None: # No node found, no instance to unlock return True # Check for instance is locked elif instance.locked: # If the instance is locked, the key must match the lock time if key == instance.locked: instance.locked = 0 instance._update(tx, release_time) return True # If the key does not match then do not release lock else: return False # Nothing to release if this far return True
def lock(cls, session, uuid, account_number, name): """Locks an environment with matching uuid. Lock is obtained in a single transaction. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param uuid: Environment uuid. :type uuid: str :param account_number: Account number :type account_number: str :param name: Name of the environment. :type name: str :returns: The time of the lock in milliseconds. This will be used as the key to release the lock :rtype: int """ lock_time = utils.milliseconds_now() with session.begin_transaction() as tx: instance = cls.find_transaction(tx, uuid) if instance is None: # No node found, create the node instance = cls( uuid=uuid, account_number=account_number, name=name, locked=lock_time ) instance._update(tx, lock_time) elif instance.locked == 0: instance.locked = lock_time instance._update(tx, lock_time) else: # Raise exception. The node exists and locked is not none raise EnvironmentLockedError(instance) return lock_time
parser = base_parser(description=( "Terminate an environment. Mark ending time on all relationships " "stemming from the environment.")) parser.add_argument('uuid', type=str, help='UUID of environment to terminate.') parser.add_argument('-s', '--skip', action='store_true', help='Skip interactive confirmation.') parser.add_argument('--time', type=int, help="Optional timestamp in ms. Defaults to utc now.", default=utils.milliseconds_now()) parser.add_argument( '--limit', type=int, help="Optional number of paths to consider at a time. Defaults to 2000.", default=2000) def set_to_until_zero(session, env, time, limit=2000): """Set current relationships' to attributes to time. Runs in a loop until there are no more current relationships. :param session: Neo4j driver session. :type session: neo4j.v1.session.BoltSession