def truncate_table(session, argsql): db = Database() flag = True count = 0 while flag and count <= 3 : try: for ite in argsql: time.sleep(1) db.execute_nonquery(session, ite) session.commit() flag = False count +=1 logger = getLog('Sync_Data') logger.info('Complete truncate table') print "completed" return 1 except Exception as e: logerr = getLog('Error_Sync_Data') logerr.error('Truncate table %s'%(e)) flag = True count += 1 finally: logger = getLog('Sync_Data') logger.info('Final truncate table') return 0
def __init__(self, cfghost=None): self.cfghost = cfghost #self.jobp_Q = Queue() #self.logger = getLog('Sync job param %s' %(cfghost['host'])) self.logger = getLog('Job_Param') self.logger.setLevel(logging.INFO) self.logerr = getLog('Error_Job_Param') self.db = Database(log = 'Job_Param', logerror = 'Error_Job_Param') self.session = self.db.connect()
def insert_job(session=None, host=None): logger = getLog('Sync_Data') logger.setLevel(logging.INFO) db = Database() session = db.connect() start = time.time() try: dataJob = get_job(host) logger.info('Get query insert Job %s completed %s and len %s'%(host['host'], time.time() - start, len(dataJob))) truncate_table(session, ["delete from job where host = '{0}';".format(host['host'])]) # truncate_table(session, ["delete from job_param where host = '{0}';".format(host['host'])]) db = Database() db.many_insert(session, 'job', dataJob, 'jid', 'host', 'state', 'status', 'prog', 'ver', 'startdate', 'enddate') #db.many_insert(session, 'job_param', dataPara, 'jid', 'host', 'name', 'wid', 'backup') except Exception as e: logerr = getLog('Error_Sync_Data') logerr.error('Get Log {0} and len jobs {1}'.format(e,len(dataJob))) finally: db.close_connect(session) print(session) return 0
def insert_workflow(session = None, host=None): logger = getLog('Sync_Data') logger.setLevel(logging.INFO) db = Database() session = db.connect() start = time.time() try: data = get_workflow(host) logger.info('Get query insert Workflow %s Completed in %s and len %s.' %(host['host'], time.time() - start,len(data))) #logger.info('Get query insert Workflow %s data is %s.' %(host['host'],data)) truncate_table(session, ["delete from workflow where host = '{0}';".format(host['host'])]) db = Database() db.many_insert(session, 'workflow',data, 'wid', 'host', 'name') except Exception as e: logerr = getLog('Error_Sync_Data') logerr.error('Get workflow error %s.'%(e)) raise finally: db.close_connect(session) print(session) return 0
def insert_node(session = None, host=None): db = Database() session = db.connect() logger = getLog('Sync_Data') logger.setLevel(logging.INFO) start = time.time() try: data_node = get_node(host) logger.info(' Get query insert Node %s Completed in %s.' %(host['host'] ,time.time() - start)) truncate_table(session,["delete from node where host = '{0}';".format(host['host'])]) db.many_insert(session, 'node', data_node, 'nid', 'host', 'cpu', 'alloccpu', 'mem', 'allocmem', 'status', 'state', 'uncreachable') data_nodedetail = get_node_detail(host) truncate_table(session,["delete from node_detail where host = '{0}';".format(host['host'])]) db.many_insert(session, 'node_detail', data_nodedetail, 'nid','host','jid') except Exception as e: logerr = getLog('Error_Sync_Data') logerr.error('Get Node %s' %(e)) raise finally: db.close_connect(session) print(session) return 0