def createNewTask(self,device_id): taskList = self.tasks.getItemsByDid(device_id,task_type=TASK_SCAN_TYPE) devInfo = betaDevice().getItemByID(device_id) if devInfo: print "Create New tasks for %s" %device_id adminStatus = devInfo["admin_status"] user_id = devInfo["user_id"] currTime = int(time.time()) scheduleTime = self.getOnBetaScheduleTime(device_id) if len(taskList) == 0: if adminStatus == ADMIN_STATUS_UP: self.tasks.create(device_id=device_id, start_time=currTime+scheduleTime, user_id=user_id, task_type=TASK_SCAN_TYPE) else: self.tasks.create(device_id=device_id, start_time=currTime+NOT_ON_BETA_SCHEDULE_TIME, user_id=user_id, task_type=TASK_SCAN_TYPE) elif len(taskList) == 1: task_id = taskList[0]["id"] if adminStatus == ADMIN_STATUS_UP: self.tasks.update(id=task_id, start_time=currTime+scheduleTime) else: self.tasks.update(id=task_id, start_time=currTime + NOT_ON_BETA_SCHEDULE_TIME) elif len(taskList) > 1: create_flag = 0 for i in taskList: start_time = i["start_time"] if create_flag: if start_time < currTime + self.timeout: self.tasks.delete(i["id"]) else: if start_time < currTime + self.timeout: self.tasks.update(id=i["id"], start_time = currTime + self.timeout) create_flag = 1 else: print "Get device Information failed"
def move2History(self, item): device_id = item["device_id"] task_id = item["id"] if device_id: start_time = item["start_time"] end_time = int(time.time()) result = item["result"] log = item["log"] result_info = item["result_info"] task_type = item["task_type"] user_id = item["user_id"] # print "Move the Task %s to History list" %item["id"] self.taskHistory.create(start_time=start_time, end_time=end_time, result=result, log=log, device_id=device_id, result_info=result_info, task_type=task_type, user_id=user_id) self.tasks.delete(task_id) if task_type == TASK_SCAN_TYPE: return self.createNewTask(device_id) elif task_type == TASK_DAILY_TYPE: devInfo = betaDevice().getItemByID(device_id) if devInfo["os"]=="WAF": return self.createNewDailyTask(device_id) else: print "The device may removed. Task will be delete" self.tasks.delete(task_id) return True
def __init__(self): self.tasks = betaTaskCurrent() self.taskHistory = betaTaskHistory() self.devices = betaDevice() self.timeout = TASK_TIMEOUT self.upScheduleTime = TASK_TIMEOUT self.runningtask = {} pass
def getOnBetaScheduleTime(self, device_id): """Get device's schedule time""" devInfo = betaDevice().getItemByID(device_id) user_id =devInfo["user_id"] setInfo = betaSetting().getSettingbyUid(user_id) if setInfo and setInfo.scantime: scheduleTime = setInfo.scantime*60 else: scheduleTime = ON_BETA_SCHEDULE_TIME return scheduleTime
def createNewDailyTask(self,device_id): taskList = self.tasks.getItemsByDid(device_id,task_type=TASK_DAILY_TYPE) devInfo = betaDevice().getItemByID(device_id) if devInfo: print "Create New daily report task for %s" %device_id adminStatus = devInfo["admin_status"] user_id = devInfo["user_id"] currTime = int(time.time()) dailyrepotime = self.getDailyReportTime(user_id=user_id) if len(taskList) == 0: if adminStatus == ADMIN_STATUS_UP: self.tasks.create(device_id=device_id, start_time=dailyrepotime, user_id=user_id, task_type=TASK_DAILY_TYPE) elif len(taskList) == 1: task_id = taskList[0]["id"] if adminStatus == ADMIN_STATUS_UP: self.tasks.update(id=task_id, start_time = dailyrepotime) elif len(taskList) > 1: self.tasks.update(id=taskList[0]["id"], start_time = dailyrepotime) for i in taskList[1:]: self.tasks.delete(i["id"]) else: print "Get device Information failed"