性能优化: 任务列表瘦身+详情分页+auto状态独立存储+磁盘统计缓存+db异步同步; 统计数值未读取时显示-
- /api/tasks 响应 1.57MB -> 12KB (latest_run 只带摘要, 一次读 runs.json) - /api/stats 磁盘占用加 30s 缓存, 去除重复全量读 - 详情接口分页返回 results (默认100条/页), 前端表格分页+页码跳转 - auto 任务 pending/visited 队列迁移到 data/auto_state/ 独立文件 (tasks.json 1.6MB -> 14KB) - MySQL 同步改后台线程异步执行 (db.sync_run_async/upsert_task_async), 不再阻塞爬虫和 API - 启动时自动迁移存量 auto 状态 - 统计/回收站/运行中数值在未读到真实数据前显示 '-'
This commit is contained in:
@@ -583,9 +583,10 @@ class CrawlJob:
|
||||
run["out_dir"] = out_dir
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
|
||||
# 恢复持久化状态: 已爬集合 + 上次未爬完的缓存队列
|
||||
visited = set(auto.get("visited", []) or [])
|
||||
pending = auto.get("pending", []) or []
|
||||
# 恢复持久化状态: 已爬集合 + 上次未爬完的缓存队列 (独立状态文件, 不撑大 tasks.json)
|
||||
state = store.load_auto_state(self.task["id"], self.task)
|
||||
visited = set(state.get("visited", []) or [])
|
||||
pending = state.get("pending", []) or []
|
||||
# 起始网址每次运行都爬(不做去重), 缓存队列继续消费
|
||||
queue = [(seed, 0, "")]
|
||||
if pending:
|
||||
@@ -634,11 +635,12 @@ class CrawlJob:
|
||||
self._close_browser(p, browser, ctx, cookie_file)
|
||||
# 无论完成/停止/异常, 都保存缓存队列与已爬集合, 便于下次继续
|
||||
try:
|
||||
auto["pending"] = [
|
||||
{"url": u, "depth": d, "source": s} for u, d, s in queue]
|
||||
auto["visited"] = list(visited)
|
||||
store.upsert_task(self.task)
|
||||
db.upsert_task(self.task)
|
||||
store.save_auto_state(self.task["id"], {
|
||||
"pending": [
|
||||
{"url": u, "depth": d, "source": s} for u, d, s in queue],
|
||||
"visited": list(visited),
|
||||
})
|
||||
db.upsert_task_async(self.task)
|
||||
except Exception:
|
||||
pass
|
||||
self._persist()
|
||||
Reference in New Issue
Block a user