def stop_job(self, job_id): job = Job(self.db) job.update_status(job_id, "stop") try: self.scheduler.remove_job("job." + job_id) except: pass
def start_job(self, job_id): job = Job(self.db) job_class = job.find(job_id) if (job_class == None): return None task_class = job_class["task"] if (task_class == None): return None timer_class = job_class["timer"] if (timer_class == None): return None job_id = job_class["id"] job.update_status(job_id, "active") self.scheduler.add_job(func=SchedulerJobExector({ "scheduler": self, "task": task_class, "timer": timer_class, "job_id": job_id, }), trigger=timer_class["type"], id="job." + job_id, **timer_class["param"], misfire_grace_time=3600, coalesce=True) return True
def ExecByTimer(self, task, timer): if timer["type"] == "once": return self.Exec(task) job = Job(self.db) job_id = job.insert({ "task_id": task["id"], "task": task, "timer_id": timer["id"], "timer": timer, }) if job_id == None: return { "code": 0, "message": "任务存储失败" } ret = self.scheduler.add_job(func=SchedulerJobExector({ "scheduler": self, "task": task, "timer": timer, "job_id": job_id, }), trigger=timer["type"], id="job." + job_id, **timer["param"], misfire_grace_time=3600, coalesce=True) return { "code": 1, "job_id": job_id }
def onStatusChange(self, event): job_id = event.job_id if (job_id.find("job.") >= 0): job = Job(self.db) job_id = job_id[4:] if event.exception: logging.error(str(event) + str(event.exception)) self.onJobResult({"job_id": job_id}, "exception", str(event.exception)) elif EVENT_JOB_EXECUTED == event.code: self.onJobResult({"job_id": job_id}, "success", "") else: logging.debug(event) self.onJobResult({"job_id": job_id}, "complete", "event.code." + str(event.code)) else: exector = Exector(self.db) job_id = job_id[8:] if event.exception: logging.error(str(event) + str(event.exception)) self.onExectorResult({"exector_id": job_id}, "exception", str(event.exception)) elif EVENT_JOB_EXECUTED == event.code: self.onExectorResult({"exector_id": job_id}, "success", "") else: logging.debug(event) self.onExectorResult({"exector_id": job_id}, "complete", "event.code." + str(event.code))
def onJobResult(self, content, status, message): job = Job(self.db) job_id = content["job_id"] if status == "success": result = { "status": status, } else: result = { "status": status, "message": message, } job.set_result(job_id, result) loop = False if ("timer" in content): loop = True if not loop: job.update_status(job_id, "complete")
def post(self,cmd): task = Task(self.application.db); job = Job(self.application.db); timer = Timer(self.application.db); exector = Exector(self.application.db); scheduler = self.application.scheduler; if cmd == "exec": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); task_id = data["id"]; res = task.find(task_id); if res == None: self.write(json.dumps({"success":0,"message":"no task"})); return; res = scheduler.Exec(res); self.write(json.dumps({"success":1,"data":res})); if cmd == "fixtime": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); task_id = data["id"]; res = task.find(task_id); if res == None: self.write(json.dumps({"success":0,"message":"no task"})); return; timing = adjustmentTiming(data["timing"]); print(timing) timing["task_id"] = task_id; timer_id = timer.insert(timing); timing["id"] = timer_id; res = scheduler.ExecByTimer(res,timing); if "code" in res: if "message" in res: self.write(json.dumps({"success":res["code"],"message":res["message"]})); elif "job_id" in res: self.write(json.dumps({"success":res["code"],"id":res["job_id"]})); else: self.write(json.dumps({"success":res["code"]})); else: self.write(json.dumps({"success":-1,"data":res})); if cmd == "restart": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); job_id = data["id"]; res = scheduler.start_job(job_id); self.write(json.dumps({"success":1})); if cmd == "stop": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); job_id = data["id"]; scheduler.stop_job(job_id); self.write(json.dumps({"success":1})); if cmd == "gets": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); options = self.get_options(data); data = exector.find_some(options); self.write(json.dumps(data)); if cmd == "del": data = self.request.body; data = data.decode("utf-8") data = json.loads(data); record_id = data["id"]; res = exector.remove(record_id); self.write(json.dumps({"success":int(res['ok']),"count":res['n']}));
def DB(self): return Job(self.application.db);
def activate(self): job = Job(self.db) job_ids = job.find_by_status("active") for i in range(len(job_ids)): self.start_job(job_ids[i])