fix: 切换或退出登录前先暂停任务,重新登录后继续失效任务
This commit is contained in:
@@ -31,6 +31,28 @@ def _captcha_payload(session_id: str, challenge, **extra) -> dict:
|
||||
}
|
||||
|
||||
|
||||
async def _pause_and_close_client(mark: str) -> int:
|
||||
"""先暂停进行中的任务,等在途请求结束,再关闭当前客户端。"""
|
||||
paused = sum(1 for job in state.jobs.jobs.values() if job.status in {"running", "queued"})
|
||||
state.jobs.suspend_all("已暂停", mark)
|
||||
client = state.client
|
||||
state.client = None
|
||||
if client is not None:
|
||||
wait_idle = getattr(client, "wait_idle", None)
|
||||
if wait_idle is not None:
|
||||
await wait_idle(5)
|
||||
await client.aclose()
|
||||
return paused
|
||||
|
||||
|
||||
def _accept_login() -> None:
|
||||
state.expired = False
|
||||
state.jobs.resume_marked("expired")
|
||||
from app.features.download.router import fill_slots
|
||||
|
||||
fill_slots()
|
||||
|
||||
|
||||
async def purge_expired_flows(now: float | None = None) -> None:
|
||||
"""关掉超过 10 分钟的登录流程,并清掉内存里的密码。"""
|
||||
moment = time.monotonic() if now is None else now
|
||||
@@ -130,11 +152,12 @@ async def login(body: dict) -> dict:
|
||||
raise HTTPException(status_code=400, detail=message) from exc
|
||||
challenge.password = ""
|
||||
await remember_account(user, client.export_cookies())
|
||||
state.flows.pop(session_id, None)
|
||||
if state.client is not None and state.client is not client:
|
||||
await state.client.aclose()
|
||||
if state.client is not client:
|
||||
await _pause_and_close_client("switch")
|
||||
state.client = client
|
||||
state.user = user
|
||||
state.flows.pop(session_id, None)
|
||||
_accept_login()
|
||||
return {"status": "ok", "user": user}
|
||||
|
||||
|
||||
@@ -152,10 +175,11 @@ async def resume_saved(body: dict) -> dict:
|
||||
client, user = await resume(login_id)
|
||||
except NqiError as exc:
|
||||
raise _bad(str(exc)) from exc
|
||||
if state.client is not None and state.client is not client:
|
||||
await state.client.aclose()
|
||||
if state.client is not client:
|
||||
await _pause_and_close_client("switch")
|
||||
state.client = client
|
||||
state.user = user
|
||||
_accept_login()
|
||||
return {"status": "ok", "user": user}
|
||||
|
||||
|
||||
@@ -167,8 +191,7 @@ async def me() -> dict:
|
||||
|
||||
@router.post("/logout")
|
||||
async def logout() -> dict:
|
||||
if state.client is not None:
|
||||
await state.client.aclose()
|
||||
state.client = None
|
||||
paused = await _pause_and_close_client("logout")
|
||||
state.user = None
|
||||
return {"ok": True}
|
||||
state.expired = False
|
||||
return {"ok": True, "paused": paused}
|
||||
|
||||
Reference in New Issue
Block a user