1.增加任务中间件
This commit is contained in:
parent
6f1da0cbb2
commit
90f256bcb6
32
main.py
32
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(
|
||||
|
Loading…
Reference in New Issue
Block a user