fix: 给 soffice 加超时(120s)机制、worker 自动回收超时 running 任务,避免任务挂死阻塞队列

This commit is contained in:
sophie committed 2026-05-29 11:37:02 +08:00
1 parent 1d72018eb7
commit 0eb577df1a
11 files changed
+379 -88

No files matched your search

+16 -28
View File
@@ -1,47 +1,35 @@
"""
backend/edkb_system/skills/doc_to_pdf.py
文件用途:将 Word 文档转换为 PDF,供后续 PDF 解析链路使用
说明:
- Windows 优先走 Word COM(word_bridge);
- Linux/容器环境走 LibreOffice,转换超时由 soffice_util 统一控制。
"""
from __future__ import annotations
import os
import subprocess
from pathlib import Path
from edkb_system.contracts import Context
from edkb_system.skills.soffice_util import convert_with_soffice, find_soffice
from edkb_system.skills.word_bridge import convert_with_word
def _find_soffice() -> str | None:
candidates = [
os.environ.get("EDKB_SOFFICE"),
r"C:\Program Files\LibreOffice\program\soffice.exe",
r"C:\Program Files (x86)\LibreOffice\program\soffice.exe",
"soffice",
]
for c in candidates:
if not c:
continue
if c == "soffice":
return c
if Path(c).exists():
return c
return None
def _convert_with_word(path: Path, out_dir: Path) -> Path:
output = out_dir / f"{path.stem}.pdf"
return convert_with_word(path.resolve(), output.resolve(), 17)
def _convert_with_soffice(path: Path, out_dir: Path, soffice: str) -> Path:
"""通过 soffice 转 PDF;超时抛出 RuntimeError。"""
output = out_dir / f"{path.stem}.pdf"
cmd = [
proc = convert_with_soffice(
soffice,
"--headless",
"--convert-to",
"pdf",
str(path),
"--outdir",
str(out_dir),
]
proc = subprocess.run(cmd, capture_output=True, text=True)
convert_to="pdf",
source_path=path,
out_dir=out_dir,
)
if proc.returncode != 0:
stderr = (proc.stderr or "").strip() or "no stderr"
raise RuntimeError(
@@ -66,7 +54,7 @@ def run(input_data: dict, context: Context) -> dict:
except Exception as exc:
errors.append(f"word: {exc}")
soffice = _find_soffice()
soffice = find_soffice()
if soffice:
try:
pdf = _convert_with_soffice(file_path, out_dir, soffice)