grr/server/grr_response_server/databases/mysql_flows.py
def LeaseClientMessages(self,
                          client_id,
                          lease_time=None,
                          limit=None,
                          cursor=None):
    """Leases available client messages for the client with the given id."""

    now = rdfvalue.RDFDatetime.Now()
    now_str = mysql_utils.RDFDatetimeToMysqlString(now)
    expiry = now + lease_time
    expiry_str = mysql_utils.RDFDatetimeToMysqlString(expiry)
    proc_id_str = utils.ProcessIdString()
    client_id_int = mysql_utils.ClientIDToInt(client_id)

    query = ("UPDATE client_messages "
             "SET leased_until=%s, leased_by=%s, leased_count=leased_count+1 "
             "WHERE client_id=%s AND "
             "(leased_until IS NULL OR leased_until < %s) "
             "LIMIT %s")
    args = [expiry_str, proc_id_str, client_id_int, now_str, limit]

    num_leased = cursor.execute(query, args)
    if num_leased == 0:
      return []

    query = ("SELECT message, leased_count FROM client_messages "
             "WHERE client_id=%s AND leased_until=%s AND leased_by=%s")

    cursor.execute(query, [client_id_int, expiry_str, proc_id_str])

    ret = []
    expired = []
    for msg, leased_count in cursor.fetchall():
      message = rdf_flows.GrrMessage.FromSerializedString(msg)
      message.leased_by = proc_id_str
      message.leased_until = expiry
      # > comparison since this check happens after the lease.
      if leased_count > db.Database.CLIENT_MESSAGES_TTL:
        expired.append((client_id, message.task_id))
      else:
        ret.append(message)

    if expired:
      self._DeleteClientMessages(expired, cursor=cursor)

    return sorted(ret, key=lambda msg: msg.task_id)
Leases available client messages for the client with the given id.
Leases available client messages for the client with the given id.
Leases available client messages for the client with the given id .
def LeaseClientMessages(self,
                          client_id,
                          lease_time=None,
                          limit=None,
                          cursor=None):
    now = rdfvalue.RDFDatetime.Now()
    now_str = mysql_utils.RDFDatetimeToMysqlString(now)
    expiry = now + lease_time
    expiry_str = mysql_utils.RDFDatetimeToMysqlString(expiry)
    proc_id_str = utils.ProcessIdString()
    client_id_int = mysql_utils.ClientIDToInt(client_id)
    query = ("UPDATE client_messages "
             "SET leased_until=%s, leased_by=%s, leased_count=leased_count+1 "
             "WHERE client_id=%s AND "
             "(leased_until IS NULL OR leased_until < %s) "
             "LIMIT %s")
    args = [expiry_str, proc_id_str, client_id_int, now_str, limit]
    num_leased = cursor.execute(query, args)
    if num_leased == 0:
      return []
    query = ("SELECT message, leased_count FROM client_messages "
             "WHERE client_id=%s AND leased_until=%s AND leased_by=%s")
    cursor.execute(query, [client_id_int, expiry_str, proc_id_str])
    ret = []
    expired = []
    for msg, leased_count in cursor.fetchall():
      message = rdf_flows.GrrMessage.FromSerializedString(msg)
      message.leased_by = proc_id_str
      message.leased_until = expiry
      if leased_count > db.Database.CLIENT_MESSAGES_TTL:
        expired.append((client_id, message.task_id))
      else:
        ret.append(message)
    if expired:
      self._DeleteClientMessages(expired, cursor=cursor)
    return sorted(ret, key=lambda msg: msg.task_id)
def LeaseClientMessages ( self , client_id , lease_time = None , limit = None , cursor = None ) : now = rdfvalue . RDFDatetime . Now ( ) now_str = mysql_utils . RDFDatetimeToMysqlString ( now ) expiry = now + lease_time expiry_str = mysql_utils . RDFDatetimeToMysqlString ( expiry ) proc_id_str = utils . ProcessIdString ( ) client_id_int = mysql_utils . ClientIDToInt ( client_id ) query = ( "UPDATE client_messages " "SET leased_until=%s, leased_by=%s, leased_count=leased_count+1 " "WHERE client_id=%s AND " "(leased_until IS NULL OR leased_until < %s) " "LIMIT %s" ) args = [ expiry_str , proc_id_str , client_id_int , now_str , limit ] num_leased = cursor . execute ( query , args ) if num_leased == 0 : return [ ] query = ( "SELECT message, leased_count FROM client_messages " "WHERE client_id=%s AND leased_until=%s AND leased_by=%s" ) cursor . execute ( query , [ client_id_int , expiry_str , proc_id_str ] ) ret = [ ] expired = [ ] for msg , leased_count in cursor . fetchall ( ) : message = rdf_flows . GrrMessage . FromSerializedString ( msg ) message . leased_by = proc_id_str message . leased_until = expiry if leased_count > db . Database . CLIENT_MESSAGES_TTL : expired . append ( ( client_id , message . task_id ) ) else : ret . append ( message ) if expired : self . _DeleteClientMessages ( expired , cursor = cursor ) return sorted ( ret , key = lambda msg : msg . task_id )
Leases available client messages for the client with the given id.
Leases available client messages for the client with the given id .
"""Leases available client messages for the client with the given id.""" # > comparison since this check happens after the lease.
self
client_id
lease_time
limit
cursor