feat: 完成后清理进度文件,并支持迁移数据目录

This commit is contained in:
2026-10-26 09:39:03 +08:00
parent 30a0caeb8d
commit b90b142cbf
13 changed files with 860 additions and 10 deletions
+59 -2
View File
@@ -678,10 +678,16 @@ class JobManager:
try:
status = str(data.get("status") or "queued")
message = str(data.get("message") or "")
interrupted = status in _INTERRUPTED_STATUSES
paused_by = str(data.get("pausedBy") or "")
# 迁移前主动收起来的任务要保持暂停,重启后按记录继续,不要当成意外中断。
held_for_move = paused_by == "move"
interrupted = status in _INTERRUPTED_STATUSES and not held_for_move
if interrupted:
status = "cancelled"
message = "已中断"
elif held_for_move:
status = "paused"
message = "已暂停"
whole = bool(data.get("wholeCity"))
legacy_ids = [str(item) for item in data.get("gridIds") or []]
if data.get("gridCount") is None:
@@ -717,17 +723,51 @@ class JobManager:
written_rows=int(data.get("writtenRows") or 0),
duplicate_count=int(data.get("duplicateCount") or 0),
failures=list(data.get("failures") or []),
paused_by=str(data.get("pausedBy") or ""),
paused_by=paused_by,
stopping=bool(data.get("stopping") or False) and not interrupted,
failed_tiles=_combine_failed(data.get("failedTiles"), _spool_failed_tiles(path.parent)),
)
except (KeyError, TypeError, ValueError) as exc:
raise NqiError("任务记录损坏") from exc
_clear_temp_files(path.parent)
if status == "done":
release_finished_work(path.parent)
if interrupted:
job.save()
return job
async def shelve_for_move(self) -> list[str]:
"""停掉还开着文件的任务。正在下载和排队的记下来,迁移完成后继续。"""
self._discover()
jobs = [job for job in self.jobs.values() if not job.deleting]
for job in jobs:
if job.building or job.deleting:
raise NqiError("有任务还在创建或删除,请稍后再迁移")
resume: list[str] = []
for job in jobs:
should_resume = job.status in {"running", "queued"} or job.paused_by == "move"
if should_resume:
resume.append(job.id)
busy = job.is_active() or (job.files_closed is not None and not job.files_closed.done())
if busy:
try:
await _stop_and_wait(job)
except NqiError as exc:
if str(exc) == "任务还在收尾,请稍后再删":
raise NqiError("任务还在收尾,请稍后再迁移") from exc
raise
if not should_resume:
continue
job.status = "paused"
job.paused_by = "move"
job.message = "已暂停"
job.stopping = False
job.cancel_event = asyncio.Event()
job.save()
await job.flush_record()
await asyncio.wrap_future(RECORDS.barrier())
return resume
def _clear_temp_files(directory: Path) -> None:
"""进程被强行结束时可能留下替换前的临时文件。只在任务没在运行时清。"""
@@ -742,6 +782,21 @@ def _clear_temp_files(directory: Path) -> None:
pass
_FINISHED_WORK = ("progress.jsonl", "ids.spool", "ids.spool.meta")
def release_finished_work(directory: Path) -> None:
"""任务已经全部下完。进度和编号暂存只为继续下载服务,删掉以免一直占空间。"""
for name in _FINISHED_WORK:
path = directory / name
try:
path.unlink()
except FileNotFoundError:
continue
except OSError as exc:
logger.warning("完成后的进度文件暂时删不掉 %s: %s", path, exc)
async def _stop_and_wait(job: DownloadJob) -> None:
"""强制结束任务协程,并等它关完文件。"""
task = job.task
@@ -1119,6 +1174,8 @@ async def _close_files(job: DownloadJob, io: JobIO, journal: CsvJournal, spool:
await io.close()
if job.io is io:
job.io = None
if job.status == "done":
await asyncio.to_thread(release_finished_work, job.directory)
await job.flush_record()