From 90f256bcb646fc5ab9669b741cac18f71bca1281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F?= <250213850@qq.com> Date: Thu, 20 Oct 2022 17:43:50 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E4=BB=BB=E5=8A=A1=E4=B8=AD?= =?UTF-8?q?=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index c2e869e..8a95451 100644 --- a/main.py +++ b/main.py @@ -118,8 +118,36 @@ def login_expired(conn: HTTPConnection, exc: Exception) -> Response: # return Response(schemas.Msg(code=0, msg='没有操作权限',data='').json()) # # return response - - +@app.middleware("http")#如反馈任务未启动,则启动任务 +async def run_task(request: Request, call_next): + """ + 重启反馈提醒任务 + """ + start_time = int(time.time() * 1000) + response = await call_next(request) + process_time = int(time.time() * 1000) - start_time + response.headers["X-Process-Time"] = str(process_time) + with open('task.json', 'r', encoding='utf-8') as f: + ff = f.read() + if ff == '': + return response + else: + data = json.loads(ff) + res = scheduler.queue + uid = [i.kwargs['uid'] for i in res] # 取所有的任务uid + for k, v in data.items(): + if k not in uid: # 在现有任务里面不在json文件里面,则启动json文件里面的一个任务 + now = str(time.time()).split('.')[0] + end_time = v['times'] - int(now) + # 没有过时的才会重启任务 + if end_time > 0: + scheduler.enter(end_time, 1, task, kwargs=v) + else: + data.pop(k) + jsontext = json.dumps(data) + write_task(jsontext) + t = threading.Thread(target=scheduler.run) + t.start() app.add_middleware(AuthenticationMiddleware, backend=BasicAuth(), on_error=login_expired) app.add_middleware(