def getFile(self,fileName): parent="/"+CABBAGE+"/"+FILES jobId = self.client.getData(parent+"/"+fileName+"/"+JOB_ID) jobName = self.client.getData(parent+"/"+fileName+"/"+JOB_NAME) filePath = self.client.getData(parent+"/"+fileName+"/"+FILE_PATH) fileType = self.client.getData(parent+"/"+fileName+"/"+FILE_TYPE) return File(fileName=jobName,jobId=jobId,jobName=jobName,filePath=filePath, fileType=fileType)
def getJob(self, jobId): parent = "/" + CABBAGE + "/" + JOBS + "/" + jobId # log.debug(self.client) jobName = self.client.getData(parent + "/" + JOB_NAME) log.debug(parent) filePath = self.client.getData(parent + "/" + FILE_PATH) fileName = self.client.getData(parent + "/" + FILE_NAME) fileType = self.client.getData(parent + "/" + FILE_TYPE) status = self.client.getData(parent + "/" + STATUS) auditStatus = self.client.getData(parent + "/" + AUDIT_STATUS) runStrategy = self.client.getData(parent + "/" + RUN_STRATEGY) resultBackend = None if self.client.isExistPath(parent + "/" + REULST_BACKEND): resultBackend = self.client.getData(parent + "/" + REULST_BACKEND) files = self.client.getChildren(parent + "/" + ATTACH_FILES) log.debug(parent + "/" + WORKS + "/" + LIST) ws = self.client.getChildren(parent + "/" + WORKS + "/" + LIST) attachFiles = [] works = [] for attachFile in files: fp = self.client.getData(parent + "/" + ATTACH_FILES + "/" + attachFile) ft = self.client.getData(parent + "/" + ATTACH_FILES + "/" + attachFile + "/" + FILE_TYPE) f = File(fileName=attachFile, filePath=fp, fileType=ft) attachFiles.append(f) for w in ws: pt = self.client.getData(parent + "/" + WORKS + "/" + LIST + "/" + w) works.append(Work(hostName=w, port=pt)) tasks = [] if self.client.isExistPath(parent + "/" + TASKS): tasks = self.client.getChildren(parent + "/" + TASKS) brokerServer = None brokerQueue = None if self.client.isExistPath(parent + "/" + BROKER_SERVER): brokerServer = self.client.getData(parent + "/" + BROKER_SERVER) if self.client.isExistPath(parent + "/" + QUEUE): brokerQueue = self.client.getData(parent + "/" + QUEUE) return Job(jobId=jobId, jobName=jobName, filePath=filePath, status=status, auditStatus=auditStatus, fileType=fileType, fileName=fileName, attachFiles=attachFiles, works=works, runStrategy=runStrategy, brokerServer=brokerServer, brokerQueue=brokerQueue, tasks=tasks, resultBackend=resultBackend)
def save_file(self): jobId = "job-9acc18b2-9bb2-4fbf-9c2b-95b77fe7bdd6" jobName = "12121212" f = File(fileName="11212", jobId=jobId, jobName=jobName, filePath="121212", fileType="python") self.store.saveFile(f)
def save_job(self): job = Job(jobName="12121212", filePath="1212", auditStatus="a1212", fileType="python", fileName="1212", status="1212", attachFiles=None, works=None, runStrategy="one") attachFiles = [] attachFiles.append( File(fileName="1212", jobId=job.jobId, jobName=job.jobName, filePath="121212", fileType="test")) works = [] works.append(Work(ip="1212", port="1212", status="aaa")) job.attachFiles = attachFiles job.works = works self.store.saveJob(job)
def post(self): try: jobName= self.getArgument("jobName") serverDir = ConfigHolder.getConfig().getProperty(BASE,SERVER_FILE_DIRECTORY) proType = self.getArgument("proType") params = self.getArgument("params") runStrategy = self.getArgument("runStrategy") strategyValue = self.getArgument("strategyValue") brokerServer = self.getArgument("brokerServer") brokerQueue = self.getArgument("brokerQueue") resultBackend = self.getArgument("resultBackend") if jobName is None or "mainFile" not in self.request.files or brokerServer is None or brokerQueue is None: raise Exception("参数不能为空!") return log.debug(params) jobName = str(jobName) proType=str(proType) job=Job(jobName=str(jobName),fileType=proType) job.runStrategy=str(runStrategy) job.strategyValue = str(strategyValue) job.brokerServer = brokerServer job.brokerQueue = brokerQueue jobApi= JobApi() mainFile = self.request.files['mainFile'][0] works =[] os.mkdir(serverDir+"/"+job.jobId) os.mkdir(serverDir+"/"+job.jobId+"/result") if mainFile: fileName=mainFile['filename'] fileName=str(fileName) body = mainFile['body'] p = serverDir+"/"+job.jobId+"/"+fileName fn = open(p,"w") fn.write(body) fn.close() job.filePath=p job.fileName=fileName nodes = BrokerQueueApi().getQueueByName(brokerQueue).works if nodes is None or len(nodes) ==0 : raise Exception("【%s】队列没有添加执行节点!"%brokerQueue) workApi=WorkApi() if nodes and nodes[0] == "-1": works =WorkApi().getWorks() elif nodes: for node in nodes: works.append(workApi.getWork(node)) else: works =WorkApi().getWorks() attachFiles = [] job.works=works tasks = [] for attachFile in self.request.files['attachs']: log.debug(attachFile) fileName=attachFile['filename'] fileName=str(fileName) body = attachFile['body'] fileType=PYTHON if attachFile['content_type'] == NewJobHandler.python_type or fileName.endswith(".py") else "text" p = serverDir+"/"+job.jobId+"/"+fileName fn = open(p,"w") fn.write(body) fn.close() attachFiles.append(File(fileName=fileName,jobId=job.jobId,jobName=job.jobName,filePath=p,fileType=fileType)) for attachFile in attachFiles: if attachFile.fileType == PYTHON: tasks += jobApi.getTasks(attachFile.fileName, job.jobId) job.attachFiles=attachFiles job.status=JOB_AUTH_PASS job.tasks = tasks job.resultBackend=resultBackend.lower() log.debug(works) jobApi.saveJob(job) time.sleep(5) for work in works: WorkApi().workChangeStatus(work, OFF_LINE) zope.event.notify(JobAuditEvent(job.jobId,JOB_AUTH_PASS)) self.write(job.jobId) except Exception as e: log.exception(e) self.render("new_job.html",works=WorkApi().getWorks(),servers=BrokerServerApi().getBrokerServers(),errorMessage="添加任务失败,原因:%s!" % str(e)) # time.sleep(15) # zope.event.notify(JobRunEvent(job.jobId))