first
This commit is contained in:
@@ -10,6 +10,11 @@ from app.services.import_worker_service import import_domains_from_path
|
||||
|
||||
|
||||
_IMPORT_TASK_LOCK = threading.Lock()
|
||||
_SOURCE_TYPE_LABELS = {
|
||||
6: "手工录入",
|
||||
7: "TXT 导入",
|
||||
9: "其它",
|
||||
}
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
@@ -24,6 +29,12 @@ def _save_tasks(tasks: list[dict]) -> None:
|
||||
save_import_records(tasks)
|
||||
|
||||
|
||||
def _append_log_locked(target: dict, message: str) -> None:
|
||||
target.setdefault("logs", [])
|
||||
target["logs"].append(f"[{_now()}] {message}")
|
||||
target["logs"] = target["logs"][-200:]
|
||||
|
||||
|
||||
def _update_task(task_id: str, **patch: object) -> dict | None:
|
||||
with _IMPORT_TASK_LOCK:
|
||||
tasks = load_import_records()
|
||||
@@ -36,13 +47,73 @@ def _update_task(task_id: str, **patch: object) -> dict | None:
|
||||
return dict(target)
|
||||
|
||||
|
||||
def _update_task_with_log(task_id: str, log_message: str, **patch: object) -> dict | None:
|
||||
with _IMPORT_TASK_LOCK:
|
||||
tasks = load_import_records()
|
||||
target = next((item for item in tasks if item["task_id"] == task_id), None)
|
||||
if not target:
|
||||
return None
|
||||
target.update(patch)
|
||||
_append_log_locked(target, log_message)
|
||||
target["updated_at"] = _now()
|
||||
_save_tasks(tasks)
|
||||
return dict(target)
|
||||
|
||||
|
||||
def _phase_label(phase: str) -> str:
|
||||
mapping = {
|
||||
"queued": "排队中",
|
||||
"reading": "读取文件中",
|
||||
"normalizing": "清洗中",
|
||||
"importing": "入库中",
|
||||
"completed": "已完成",
|
||||
"failed": "失败",
|
||||
}
|
||||
return mapping.get(phase, phase)
|
||||
|
||||
|
||||
def _source_label(source_type: int) -> str:
|
||||
return _SOURCE_TYPE_LABELS.get(int(source_type or 7), "未知")
|
||||
|
||||
|
||||
def _run_import_task(task_id: str, file_path: str, source_type: int = 7) -> None:
|
||||
_update_task(task_id, status="running", started_at=_now(), message="导入任务开始执行")
|
||||
_update_task_with_log(
|
||||
task_id,
|
||||
f"导入任务开始执行,来源类型:{_source_label(source_type)}",
|
||||
status="running",
|
||||
started_at=_now(),
|
||||
message=f"导入任务开始执行,来源类型:{_source_label(source_type)}",
|
||||
phase="reading",
|
||||
phase_label=_phase_label("reading"),
|
||||
)
|
||||
try:
|
||||
result = import_domains_from_path(Path(file_path), source_type=source_type)
|
||||
stats = result.get("stats", {})
|
||||
_update_task(
|
||||
path = Path(file_path)
|
||||
_update_task_with_log(
|
||||
task_id,
|
||||
f"开始读取文件:{path.name}",
|
||||
phase="reading",
|
||||
phase_label=_phase_label("reading"),
|
||||
)
|
||||
raw_lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
|
||||
total_lines = len(raw_lines)
|
||||
non_empty = sum(1 for line in raw_lines if line.strip())
|
||||
_update_task_with_log(
|
||||
task_id,
|
||||
f"文件读取完成,共 {total_lines} 行,非空 {non_empty} 行",
|
||||
phase="normalizing",
|
||||
phase_label=_phase_label("normalizing"),
|
||||
message=f"文件读取完成,准备清洗 {non_empty} 条域名",
|
||||
)
|
||||
|
||||
result = import_domains_from_path(path, source_type=source_type)
|
||||
stats = result.get("stats", {})
|
||||
_update_task_with_log(
|
||||
task_id,
|
||||
(
|
||||
f"导入完成:总数 {stats.get('total', 0)},有效 {stats.get('valid', 0)},"
|
||||
f"新增 {stats.get('added', 0)},已存在 {stats.get('exists', 0)},无效 {stats.get('invalid', 0)},"
|
||||
f"来源类型 {result.get('source_label') or _source_label(source_type)}"
|
||||
),
|
||||
status="completed",
|
||||
completed_at=_now(),
|
||||
result=result,
|
||||
@@ -50,13 +121,18 @@ def _run_import_task(task_id: str, file_path: str, source_type: int = 7) -> None
|
||||
f"导入完成:总数 {stats.get('total', 0)},有效 {stats.get('valid', 0)},"
|
||||
f"新增 {stats.get('added', 0)},已存在 {stats.get('exists', 0)},无效 {stats.get('invalid', 0)}"
|
||||
),
|
||||
phase="completed",
|
||||
phase_label=_phase_label("completed"),
|
||||
)
|
||||
except Exception as exc:
|
||||
_update_task(
|
||||
_update_task_with_log(
|
||||
task_id,
|
||||
f"导入失败:{exc}",
|
||||
status="failed",
|
||||
completed_at=_now(),
|
||||
message=f"导入失败:{exc}",
|
||||
phase="failed",
|
||||
phase_label=_phase_label("failed"),
|
||||
)
|
||||
|
||||
|
||||
@@ -71,12 +147,16 @@ def create_import_task(content: bytes, filename: str, source_type: int = 7) -> d
|
||||
"filename": safe_name,
|
||||
"stored_path": str(target),
|
||||
"source_type": source_type,
|
||||
"source_label": _source_label(source_type),
|
||||
"status": "queued",
|
||||
"message": "文件已接收,等待处理",
|
||||
"message": f"文件已接收,等待后台处理,来源类型:{_source_label(source_type)}",
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
"started_at": "",
|
||||
"completed_at": "",
|
||||
"phase": "queued",
|
||||
"phase_label": _phase_label("queued"),
|
||||
"logs": [f"[{_now()}] 文件已接收,等待后台处理,来源类型:{_source_label(source_type)}"],
|
||||
"result": None,
|
||||
}
|
||||
|
||||
@@ -104,6 +184,9 @@ def retry_import_task(task_id: str) -> dict:
|
||||
target["completed_at"] = ""
|
||||
target["updated_at"] = _now()
|
||||
target["result"] = None
|
||||
target["phase"] = "queued"
|
||||
target["phase_label"] = _phase_label("queued")
|
||||
target["logs"] = [f"[{_now()}] 任务已重新加入队列,等待后台执行"]
|
||||
_save_tasks(tasks)
|
||||
stored_path = target["stored_path"]
|
||||
source_type = int(target.get("source_type", 7))
|
||||
|
||||
Reference in New Issue
Block a user