def WriteClientSnapshot(self, snapshot, cursor=None): """Write new client snapshot.""" insert_history_query = ( "INSERT INTO client_snapshot_history(client_id, timestamp, " "client_snapshot) VALUES (%s, FROM_UNIXTIME(%s), %s)") insert_startup_query = ( "INSERT INTO client_startup_history(client_id, timestamp, " "startup_info) VALUES(%s, FROM_UNIXTIME(%s), %s)") now = rdfvalue.RDFDatetime.Now() current_timestamp = mysql_utils.RDFDatetimeToTimestamp(now) client_info = { "last_snapshot_timestamp": current_timestamp, "last_startup_timestamp": current_timestamp, "last_version_string": snapshot.GetGRRVersionString(), "last_platform": snapshot.knowledge_base.os, "last_platform_release": snapshot.Uname(), } update_clauses = [ "last_snapshot_timestamp = FROM_UNIXTIME(%(last_snapshot_timestamp)s)", "last_startup_timestamp = FROM_UNIXTIME(%(last_startup_timestamp)s)", "last_version_string = %(last_version_string)s", "last_platform = %(last_platform)s", "last_platform_release = %(last_platform_release)s", ] update_query = ( "UPDATE clients SET {} WHERE client_id = %(client_id)s".format( ", ".join(update_clauses))) int_client_id = db_utils.ClientIDToInt(snapshot.client_id) client_info["client_id"] = int_client_id startup_info = snapshot.startup_info snapshot.startup_info = None try: cursor.execute(insert_history_query, (int_client_id, current_timestamp, snapshot.SerializeToBytes())) cursor.execute(insert_startup_query, (int_client_id, current_timestamp, startup_info.SerializeToBytes())) cursor.execute(update_query, client_info) except MySQLdb.IntegrityError as e: if e.args and e.args[ 0] == mysql_error_constants.NO_REFERENCED_ROW_2: raise db.UnknownClientError(snapshot.client_id, cause=e) elif e.args and e.args[0] == mysql_error_constants.DUP_ENTRY: raise mysql_utils.RetryableError(cause=e) else: raise finally: snapshot.startup_info = startup_info
def RaisesRetryableError(cursor): raise mysql_utils.RetryableError()
def RaisesRetryableError(connection): nonlocal call_count call_count += 1 self.AddUser(connection, str(call_count), str(call_count)) if call_count == 1: raise mysql_utils.RetryableError()