def insert_messages(self, names, bodies): """ """ start = time.time() successes, failures, ellapsed_time, is_bulk = 0, 0, 0, True (ctx, dao) = (None, None) try: # Get a site DAO to work with ctx = DAOContext.getDAOContext(section=self.dbsection) dao = DAOFactory.getDAOFactory().getDAOObject(ctx, 'xrootd', 'XRootDDAO') # Try to make a bulk insert if len(bodies) > 0: try: bodies = self.validate_length(bodies) dao.insertMessages(bodies, self.transfers_db_table) successes = len(bodies) except Exception as msg: is_bulk = False self._logger.warning("couldn't feed all the data: %s" % msg) self._logger.warning("failed to insert %s messages. Inserting messages one by one" % len(bodies)) # Try to insert the messages one by one if any exception for body in bodies: try: dao.insertMessages(body, self.transfers_db_table) successes += 1 except Exception as msg: failures += 1 # Try to insert the malformed message in a table without any constraint if self.transfers_db_table_rejected is not None: try: body['exception'] = str(msg) dao.insertMessages(body, self.transfers_db_table_rejected) except: self._logger.warning("Couldn't feed data: %s" % msg) ctx.commit() self.delete_messages(names) except Exception as msg: # maybe it would be necessary to manage if something is wrong in the database (downtime for instance) self._logger.error("%s" % msg) ctx.destroy() raise Exception end = time.time() ms = 1000 * (end - start) return (successes, failures, int(ms), is_bulk)
def insert_messages(self, names, bodies): """ """ start = time.time() successes, failures, ellapsed_time, is_bulk = 0, 0, 0, True (ctx, dao) = (None, None) try: # Get a site DAO to work with ctx = DAOContext.getDAOContext(section=self.dbsection) dao = DAOFactory.getDAOFactory().getDAOObject(ctx,'xrootd','XRootDDAO') # Try to make a bulk insert if len(bodies) > 0: try: bodies = self.validate_length(bodies) dao.insertMessages(bodies, self.transfers_db_table) successes = len(bodies) except Exception, msg: is_bulk = False self._logger.warning("couldn't feed all the data: %s" % msg) self._logger.warning("failed to insert %s messages. Inserting messages one by one" % len(bodies)) # Try to insert the messages one by one if any exception for body in bodies: try: dao.insertMessages(body, self.transfers_db_table) successes += 1 except Exception, msg: failures += 1 # Try to insert the malformed message in a table without any constraint if self.transfers_db_table_rejected is not None: try: body['exception'] = str(msg) dao.insertMessages(body, self.transfers_db_table_rejected) except: self._logger.warning("Couldn't feed data: %s" % msg)