fix: 下载文件改由任务自己的线程读写,消除并发竞争和漏下载
This commit is contained in:
@@ -49,6 +49,10 @@ class NqiAuthError(NqiError):
|
||||
pass
|
||||
|
||||
|
||||
class NqiNetworkError(NqiError):
|
||||
"""超时或连不上平台。下载任务遇到它会等一会儿自动重试。"""
|
||||
|
||||
|
||||
class TileReadError(NqiError):
|
||||
"""分块读取失败,调用方决定重试、拆分或记入失败块。"""
|
||||
|
||||
@@ -534,7 +538,9 @@ class NqiClient:
|
||||
done, inflight = await asyncio.wait(inflight, timeout=0.5, return_when=asyncio.FIRST_COMPLETED)
|
||||
if not done:
|
||||
continue
|
||||
# 交出去之前不记进 seen:这一轮中途停下时,没交出的编号下一轮还能再读到。
|
||||
fresh: list[str] = []
|
||||
batch_seen: set[int] = set()
|
||||
for task in done:
|
||||
try:
|
||||
kind, tile, depth, features, matched = task.result()
|
||||
@@ -561,14 +567,15 @@ class NqiClient:
|
||||
if not grid_id:
|
||||
continue
|
||||
number = int(grid_id)
|
||||
if number in seen:
|
||||
if number in seen or number in batch_seen:
|
||||
continue
|
||||
seen.add(number)
|
||||
batch_seen.add(number)
|
||||
fresh.append(grid_id)
|
||||
await progress(len(seen))
|
||||
await progress(len(seen) + len(fresh))
|
||||
for grid_id in fresh:
|
||||
if not await _wait_turn(cancel_event, run_event, halt_event):
|
||||
break
|
||||
seen.add(int(grid_id))
|
||||
yield grid_id
|
||||
if cancel_event.is_set():
|
||||
raise NqiError("已停止")
|
||||
@@ -679,9 +686,9 @@ class NqiClient:
|
||||
try:
|
||||
response = await send(url, **kwargs)
|
||||
except httpx.TimeoutException as exc:
|
||||
raise NqiError("平台响应超时,请稍后重试") from exc
|
||||
raise NqiNetworkError("平台响应超时,请稍后重试") from exc
|
||||
except httpx.RequestError as exc:
|
||||
raise NqiError("连接失败,请稍后重试") from exc
|
||||
raise NqiNetworkError("连接失败,请稍后重试") from exc
|
||||
except RuntimeError as exc:
|
||||
if "client has been closed" in str(exc):
|
||||
raise NqiAuthError("需要重新登录") from exc
|
||||
@@ -840,6 +847,8 @@ def _landed_on_login(response: httpx.Response) -> bool:
|
||||
def _json_or_error(response: httpx.Response, fallback: str) -> dict:
|
||||
if response.status_code in {401, 403}:
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
if response.status_code >= 500:
|
||||
raise NqiNetworkError("平台暂时不可用,请稍后重试")
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError as exc:
|
||||
|
||||
@@ -10,6 +10,7 @@ from datetime import datetime
|
||||
import httpx
|
||||
|
||||
from app.platform.client import NqiAuthError, NqiClient, NqiError
|
||||
from app.shared.files import write_atomic
|
||||
from app.shared.paths import DATA_ROOT
|
||||
|
||||
logger = logging.getLogger("nqi.auth")
|
||||
@@ -34,9 +35,7 @@ def _read() -> list[dict]:
|
||||
def _write(accounts: list[dict]) -> None:
|
||||
STORE.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = json.dumps({"accounts": accounts}, ensure_ascii=False, indent=2)
|
||||
temporary = STORE.with_suffix(".json.tmp")
|
||||
temporary.write_text(payload, encoding="utf-8")
|
||||
temporary.replace(STORE)
|
||||
write_atomic(STORE, payload.encode("utf-8"))
|
||||
|
||||
|
||||
def _upsert(user: dict, cookies: list[dict]) -> None:
|
||||
@@ -65,7 +64,11 @@ def remember(user: dict, cookies: list[dict]) -> None:
|
||||
|
||||
async def remember_account(user: dict, cookies: list[dict]) -> None:
|
||||
async with _write_lock:
|
||||
_upsert(user, cookies)
|
||||
try:
|
||||
_upsert(user, cookies)
|
||||
except OSError as exc:
|
||||
# 本机登录记录写不进去不影响这次登录,下次续期会再写。
|
||||
logger.warning("登录记录暂时写不进去: %s", exc)
|
||||
|
||||
|
||||
def forget(login_id: str) -> None:
|
||||
@@ -74,7 +77,10 @@ def forget(login_id: str) -> None:
|
||||
|
||||
async def forget_account(login_id: str) -> None:
|
||||
async with _write_lock:
|
||||
forget(login_id)
|
||||
try:
|
||||
forget(login_id)
|
||||
except OSError as exc:
|
||||
logger.warning("登录记录暂时写不进去: %s", exc)
|
||||
|
||||
|
||||
def public_view(item: dict) -> dict:
|
||||
@@ -139,7 +145,10 @@ async def _merge(changes: list[tuple[str, str, dict | None]]) -> None:
|
||||
by_id[login_id] = record
|
||||
if login_id not in order:
|
||||
order.insert(0, login_id)
|
||||
_write([by_id[key] for key in order if key in by_id])
|
||||
try:
|
||||
_write([by_id[key] for key in order if key in by_id])
|
||||
except OSError as exc:
|
||||
logger.warning("登录记录暂时写不进去: %s", exc)
|
||||
|
||||
|
||||
async def available_accounts() -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user