feat: 管理后台可动态调整采集和导出的并行数量

This commit is contained in:
2026-10-30 10:14:32 +08:00
parent c232ccbec5
commit 510093235f
12 changed files with 267 additions and 44 deletions
+54 -30
View File
@@ -1,4 +1,4 @@
"""用户任务只读库里已有的数据,再写成表格。采集另走自己的队列。"""
"""用户任务只读库里已有的数据,再写成表格。采集和导出共用排队进程的并行数量。"""
from __future__ import annotations
@@ -16,7 +16,9 @@ from app.models import CellData, CollectionSource, ExportTask, NqiAccount, utcno
from app.nqi.client import NqiAuthError, NqiClient, NqiError
logger = logging.getLogger("decom")
_pool = threading.Semaphore(4)
MIN_CONCURRENCY = 1
MAX_CONCURRENCY = 32
DEFAULT_CONCURRENCY = 4
def check_period(days: int) -> int:
@@ -31,6 +33,29 @@ def check_refresh(seconds: int) -> int:
return seconds
def check_concurrency(value: int) -> int:
if value < MIN_CONCURRENCY or value > MAX_CONCURRENCY:
raise ValueError("并行数量需要在 1 到 32 之间")
return value
def current_concurrency() -> int:
from app.config import installed
if not installed():
return DEFAULT_CONCURRENCY
from app.models import AppSetting
with session_scope() as session:
setting = session.get(AppSetting, 1)
if setting is None:
return DEFAULT_CONCURRENCY
try:
return check_concurrency(int(setting.worker_concurrency))
except (TypeError, ValueError):
return DEFAULT_CONCURRENCY
def upsert_rows(session, source: str, rows: list[dict]) -> int:
count = 0
for row in rows:
@@ -87,37 +112,36 @@ def _optional_time(value) -> datetime | None:
def execute_export(task_id: int) -> None:
with _pool:
with session_scope() as session:
task = session.get(ExportTask, task_id)
if task is None or task.status not in {"queued", "running"}:
return
task.status = "running"
params = dict(task.params or {})
file_format = task.file_format
try:
with session_scope() as session:
rows = query_cells(session, params)
name, content = write_table(file_format, rows)
result_dir().mkdir(parents=True, exist_ok=True)
path = result_dir() / f"{task_id}-{name}"
path.write_bytes(content)
with session_scope() as session:
task = session.get(ExportTask, task_id)
if task is None or task.status not in {"queued", "running"}:
if task is None:
return
task.status = "running"
params = dict(task.params or {})
file_format = task.file_format
try:
with session_scope() as session:
rows = query_cells(session, params)
name, content = write_table(file_format, rows)
result_dir().mkdir(parents=True, exist_ok=True)
path = result_dir() / f"{task_id}-{name}"
path.write_bytes(content)
with session_scope() as session:
task = session.get(ExportTask, task_id)
if task is None:
return
task.status = "done"
task.result_name = path.name
task.finished_at = utcnow()
except Exception as exc:
logger.info("导出失败: %s", exc)
with session_scope() as session:
task = session.get(ExportTask, task_id)
if task is None:
return
task.status = "failed"
task.error = str(exc) or "导出失败"
task.finished_at = utcnow()
task.status = "done"
task.result_name = path.name
task.finished_at = utcnow()
except Exception as exc:
logger.info("导出失败: %s", exc)
with session_scope() as session:
task = session.get(ExportTask, task_id)
if task is None:
return
task.status = "failed"
task.error = str(exc) or "导出失败"
task.finished_at = utcnow()
def execute_collection(source_id: int) -> None: