feat(schedule): 新增平台任务参数契约与动态选项支持
This commit is contained in:
1 parent
2ce1aca023
commit
8b5b0b49bf
76 files changed
+7103
-1203
No files matched your search
@@ -1,3 +1,10 @@
|
||||
"""
|
||||
backend/app/services/schedule/sql_config.py
|
||||
|
||||
用途:排程 SQL/数据源配置 dataclass 与 ScheduleTaskConfig ORM 读写。
|
||||
依赖:models.ScheduleTask、ScheduleTaskConfig。
|
||||
逻辑:由 api/schedule/*、import_service、scheduler_service 调用 upsert/get。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
@@ -212,6 +219,63 @@ def get_all_schedule_sql_configs() -> dict[str, ScheduleSqlConfig]:
|
||||
return results
|
||||
|
||||
|
||||
def _sync_postgres_serial_sequence(table_name: str) -> None:
|
||||
"""
|
||||
将 PostgreSQL serial 序列校准为 MAX(id),避免手工导入或迁移后主键冲突。
|
||||
核心逻辑:仅对白名单表执行 setval;非 PostgreSQL 方言下无操作。
|
||||
|
||||
Args:
|
||||
table_name: 仅允许 schedule_task_configs。
|
||||
"""
|
||||
from app import db
|
||||
|
||||
if table_name != 'schedule_task_configs':
|
||||
raise ValueError(f'unsupported table for sequence sync: {table_name}')
|
||||
|
||||
bind = db.session.get_bind()
|
||||
if bind.dialect.name != 'postgresql':
|
||||
return
|
||||
|
||||
db.session.execute(
|
||||
db.text(
|
||||
"SELECT setval("
|
||||
"pg_get_serial_sequence('public.schedule_task_configs', 'id'), "
|
||||
"COALESCE((SELECT MAX(id) FROM public.schedule_task_configs), 1), "
|
||||
"true)"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _get_or_create_schedule_task_config_model(task_id: int):
|
||||
"""
|
||||
获取或创建 ScheduleTaskConfig ORM,优先走 task.config 关系避免重复 pending INSERT。
|
||||
核心逻辑:no_autoflush 下二次查库;新建前同步 PostgreSQL 序列。
|
||||
Args:
|
||||
task_id: schedule_tasks 主键。
|
||||
Returns:
|
||||
(ScheduleTask, ScheduleTaskConfig) 元组。
|
||||
Raises:
|
||||
ValueError: 排程任务不存在。
|
||||
"""
|
||||
from app import db
|
||||
from app.models import ScheduleTask, ScheduleTaskConfig
|
||||
|
||||
task = db.session.get(ScheduleTask, task_id)
|
||||
if task is None:
|
||||
raise ValueError(f'Schedule task {task_id} not found')
|
||||
|
||||
cfg_model = task.config
|
||||
if cfg_model is None:
|
||||
with db.session.no_autoflush:
|
||||
cfg_model = ScheduleTaskConfig.query.filter_by(schedule_task_id=task_id).first()
|
||||
if cfg_model is None:
|
||||
_sync_postgres_serial_sequence('schedule_task_configs')
|
||||
cfg_model = ScheduleTaskConfig(schedule_task_id=task_id)
|
||||
task.config = cfg_model
|
||||
db.session.add(cfg_model)
|
||||
return task, cfg_model
|
||||
|
||||
|
||||
def upsert_schedule_task_config(
|
||||
task_id: int,
|
||||
data: dict[str, Any],
|
||||
@@ -219,12 +283,8 @@ def upsert_schedule_task_config(
|
||||
commit: bool = True,
|
||||
) -> ScheduleSqlConfig:
|
||||
from app import db
|
||||
from app.models import ScheduleTask, ScheduleTaskConfig
|
||||
|
||||
cfg_model = ScheduleTaskConfig.query.filter_by(schedule_task_id=task_id).first()
|
||||
if not cfg_model:
|
||||
cfg_model = ScheduleTaskConfig(schedule_task_id=task_id)
|
||||
db.session.add(cfg_model)
|
||||
_task, cfg_model = _get_or_create_schedule_task_config_model(task_id)
|
||||
|
||||
for k, v in data.items():
|
||||
if k == "data_source" and isinstance(v, dict):
|
||||
|
||||
Reference in new issue
Block a user