def __sendTaskJob(taskInfo):
     #set task queue status is send
     tmp_args = {}
     tmp_args.setdefault("status", "1")
     tmp_args.setdefault("taskId", taskInfo.get("id"))
     dataResult = TaskMetaqInfoDaoInterface().updateTaskStatus(tmp_args)
     if dataResult.getSuccess():
         #exec task
         return TaskCenterService.__execTaskJob(taskInfo)
     else:
         #set task is invaild
         logger.error("send task failed.reason:{0}".format(
             dataResult.getMessage()))
         return TaskMetaqInfoDaoInterface().deleteTaskInfo(tmp_args)
 def sendTask(self):
     args = {}
     args.setdefault("limit", SystemConfig.maxThreadSize)
     dataResult = TaskMetaqInfoDaoInterface().getWaitingTaskInfos(args)
     if dataResult.getSuccess():
         logger.info("send Task start:{0}".format(dataResult.getMessage()))
         #目前支持local执行,后期可支持远程consumer或者jenkins
         requests = threadpool.makeRequests(self.__sendTaskJob,
                                            dataResult.getMessage())
         for req in requests:
             time.sleep(0.1)
             pool.putRequest(req)
         pool.wait()
         return dataResult
     else:
         logger.error("get waiting task failure:{0}".format(
             dataResult.getMessage()))
         return dataResult
 def __execTaskJob(taskInfo):
     taskInstanceId = taskInfo.get("instanceid")
     logger.info("exec task [{0}]:".format(taskInstanceId))
     args = {}
     args.setdefault("instanceId", taskInstanceId)
     args.setdefault("build_start", datetime.datetime.now())
     result = DataResult()
     taskGlobalStatus = "Error"
     try:
         #set metaq queue status is received
         metaq_args = {}
         metaq_args.setdefault("status", "2")
         #hostname
         hostname = socket.gethostname()
         ip = socket.gethostbyname(hostname)
         metaq_args.setdefault("running_consumer",
                               "{0}|{1}".format(ip, hostname))
         metaq_args.setdefault("msg_id", os.getpid())
         metaq_args.setdefault("taskId", taskInfo.get("id"))
         TaskMetaqInfoDaoInterface().updateTaskInfo(metaq_args)
         #query task instance info by id
         logger.info("query task instance [{0}] ...".format(taskInstanceId))
         dataResult = TestCaseInstanceDaoInterface(
         ).getTestInstanceInfoById(args)
         if dataResult.getSuccess() and len(dataResult.getMessage()) > 0:
             suiteId = dataResult.getMessage()[0].get("suite_id")
             userId = dataResult.getMessage()[0].get("create_userid")
             #set instance Running
             args.setdefault("build_end", None)
             args.setdefault("status", "Running")
             TestCaseInstanceDaoInterface().updateTestInstance(args)
             logger.info("query suite [{0}]by instance [{1}] ...".format(
                 suiteId, taskInstanceId))
             dataResult = TestSuiteDaoInterface().getSuiteInfoById(
                 {"suiteId": suiteId})
             if dataResult.getSuccess() and len(
                     dataResult.getMessage()) > 0:
                 envId = dataResult.getMessage()[0].get("envid")
                 caseIds = dataResult.getMessage()[0].get("testcaseids")
                 #get global env param
                 envResult = EnvironmentDaoInterface(
                 ).getEnvironmentInfoById({"envId": envId})
                 if envResult.getSuccess() and len(
                         envResult.getMessage()) <= 0:
                     logger.error(
                         "Test suite [{0}]:No env config".format(suiteId))
                     taskGlobalStatus = "Failed"
                     return result
                 if caseIds is None or len(caseIds) == 0:
                     logger.info("Test suite [{0}]:No case".format(suiteId))
                     taskGlobalStatus = "Success"
                 else:
                     # get init case for save session
                     initResult = TaskCenterService.__getInitCaseJob(
                         caseIds)
                     if not initResult.getSuccess():
                         logger.error(
                             "Test suite [{0}]:No init case".format(
                                 suiteId))
                         taskGlobalStatus = "Failed"
                         return result
                     initCaseInfo = initResult.getMessage()[0]
                     initCaseId = initCaseInfo.get("id")
                     #return request Object
                     initRequest = TaskCenterService.__execTaskCaseJob(
                         initCaseId, userId, envResult[0], None, True,
                         taskInstanceId, "init")
                     for caseId in list(caseIds):
                         if caseId == initCaseId:
                             continue
                         dataResult = TestCaseInstanceDaoInterface(
                         ).getTestInstanceInfoById(args)
                         if dataResult.getMessage()[0].get(
                                 "status") not in ["Stopped", "TimeOut"]:
                             execResult = TaskCenterService.__execTaskCaseJob(
                                 caseId, None, envResult[0],
                                 initRequest.getMessage(), True,
                                 taskInstanceId, None)
                             if not execResult.getSuccess():
                                 #set instance db
                                 taskGlobalStatus = execResult.getMessage()
                                 return result
                         else:
                             logger.error(
                                 "Task instance [{0}] is stopped".format(
                                     args.get("instanceId")))
                             taskGlobalStatus = dataResult.getMessage(
                             )[0].get("status")
                             return result
                     taskGlobalStatus = "Success"
                     result.setSuccess(True)
                     return result
     except Exception as e:
         logger.error("Exception:{0}".format(traceback.format_exc()))
         return result
     finally:
         args.setdefault("build_end", datetime.datetime.now())
         args.setdefault("status", taskGlobalStatus)
         TestCaseInstanceDaoInterface().updateTestInstance(args)
Example #4
0
 def __init__(self):
     self.taskMetaqInfoDaoInterface = TaskMetaqInfoDaoInterface()