feat(schedule): 优化排程任务配置与进度契约处理逻辑
This commit is contained in:
1 parent
d54f51ddc3
commit
99453aa52a
65 files changed
+2837
-487
No files matched your search
@@ -2,8 +2,8 @@
|
||||
backend/app/services/schedule/import_service.py
|
||||
|
||||
用途:排程任务 JSON 批量导入(upsert by schedule_no)。
|
||||
依赖:models、scheduler_service、sql_config。
|
||||
逻辑:由 api/schedule/tasks.py import 路由调用。
|
||||
依赖:models、scheduler_service、schedule_contract、sql_config。
|
||||
逻辑:由 api/schedule/tasks.py import 路由调用;导入时自动迁移旧语义。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -12,6 +12,11 @@ from typing import Any
|
||||
from app import db
|
||||
from app.core.errors import ValidationError
|
||||
from app.models import ScheduleTask
|
||||
from app.services.schedule.schedule_contract import (
|
||||
apply_schedule_contract_to_payload,
|
||||
assert_schedule_config_writable,
|
||||
migrate_legacy_schedule_payload,
|
||||
)
|
||||
from app.services.schedule.scheduler_service import create_task, update_task
|
||||
from app.services.schedule.sql_config import upsert_schedule_task_config
|
||||
|
||||
@@ -38,67 +43,78 @@ def import_schedules_from_payload(schedules: list[Any], *, actor_id: int | None)
|
||||
skipped += 1
|
||||
continue
|
||||
try:
|
||||
name = str(item.get('name') or '').strip()
|
||||
cron_expr = str(item.get('cron_expr') or '').strip()
|
||||
owner_user_id = int(item.get('owner_user_id'))
|
||||
migrated = migrate_legacy_schedule_payload(item)
|
||||
name = str(migrated.get('name') or '').strip()
|
||||
cron_expr = str(migrated.get('cron_expr') or '').strip()
|
||||
owner_user_id = int(migrated.get('owner_user_id'))
|
||||
if not name or not cron_expr:
|
||||
raise ValueError('name or cron_expr missing')
|
||||
contract = apply_schedule_contract_to_payload({
|
||||
'schedule_type': migrated.get('schedule_type'),
|
||||
'data_source_type': migrated.get('data_source_type'),
|
||||
'platform_api_path': migrated.get('platform_api_path'),
|
||||
})
|
||||
cfg = migrated.get('config') if isinstance(migrated.get('config'), dict) else None
|
||||
assert_schedule_config_writable(
|
||||
schedule_type=contract['schedule_type'],
|
||||
platform_api_path=contract['platform_api_path'],
|
||||
config=cfg,
|
||||
)
|
||||
existing = None
|
||||
schedule_no = str(item.get('schedule_no') or '').strip()
|
||||
schedule_no = str(migrated.get('schedule_no') or '').strip()
|
||||
if schedule_no:
|
||||
existing = db.session.query(ScheduleTask).filter_by(schedule_no=schedule_no).first()
|
||||
task_payload = {
|
||||
'name': name,
|
||||
'schedule_type': str(item.get('schedule_type') or 'platform_app'),
|
||||
'schedule_type': contract['schedule_type'],
|
||||
'owner_user_id': owner_user_id,
|
||||
'cron_expr': cron_expr,
|
||||
'schedule_timezone': item.get('schedule_timezone'),
|
||||
'wait_seconds': int(item.get('wait_seconds') or 0),
|
||||
'dify_agent_id': item.get('dify_agent_id'),
|
||||
'platform_api_path': item.get('platform_api_path'),
|
||||
'notify_user_ids_json': item.get('notify_user_ids_json')
|
||||
if isinstance(item.get('notify_user_ids_json'), list)
|
||||
'schedule_timezone': migrated.get('schedule_timezone'),
|
||||
'wait_seconds': int(migrated.get('wait_seconds') or 0),
|
||||
'dify_agent_id': migrated.get('dify_agent_id'),
|
||||
'platform_api_path': contract['platform_api_path'],
|
||||
'data_source_type': contract['data_source_type'],
|
||||
'notify_user_ids_json': migrated.get('notify_user_ids_json')
|
||||
if isinstance(migrated.get('notify_user_ids_json'), list)
|
||||
else [],
|
||||
'notify_enabled': bool(item.get('notify_enabled', True)),
|
||||
'enabled': bool(item.get('enabled', True)),
|
||||
'data_source_type': str(item.get('data_source_type') or 'database'),
|
||||
'kb_sync_enabled': bool(item.get('kb_sync_enabled', False)),
|
||||
'kb_group_id': item.get('kb_group_id'),
|
||||
'notify_enabled': bool(migrated.get('notify_enabled', True)),
|
||||
'enabled': bool(migrated.get('enabled', True)),
|
||||
'kb_sync_enabled': bool(migrated.get('kb_sync_enabled', False)),
|
||||
'kb_group_id': migrated.get('kb_group_id'),
|
||||
}
|
||||
cfg = item.get('config') if isinstance(item.get('config'), dict) else None
|
||||
if existing:
|
||||
update_task(existing, payload=task_payload, actor_id=actor_id)
|
||||
existing.data_source_type = task_payload['data_source_type']
|
||||
update_task(existing, payload=task_payload, actor_id=actor_id, commit=False)
|
||||
if cfg:
|
||||
upsert_schedule_task_config(existing.id, cfg)
|
||||
upsert_schedule_task_config(existing.id, cfg, commit=False)
|
||||
else:
|
||||
task = create_task(
|
||||
name=name,
|
||||
schedule_type=task_payload['schedule_type'],
|
||||
schedule_type=contract['schedule_type'],
|
||||
owner_user_id=owner_user_id,
|
||||
cron_expr=cron_expr,
|
||||
schedule_timezone=task_payload['schedule_timezone'],
|
||||
wait_seconds=task_payload['wait_seconds'],
|
||||
dify_agent_id=task_payload['dify_agent_id'],
|
||||
platform_api_path=task_payload['platform_api_path'],
|
||||
platform_api_path=contract['platform_api_path'],
|
||||
data_source_type=contract['data_source_type'],
|
||||
notify_user_ids_json=task_payload['notify_user_ids_json'],
|
||||
notify_enabled=task_payload['notify_enabled'],
|
||||
enabled=task_payload['enabled'],
|
||||
kb_sync_enabled=task_payload['kb_sync_enabled'],
|
||||
kb_group_id=(
|
||||
int(task_payload['kb_group_id'])
|
||||
if task_payload.get('kb_group_id') not in (None, '', 0, '0')
|
||||
else None
|
||||
),
|
||||
actor_id=actor_id,
|
||||
commit=False,
|
||||
)
|
||||
task.data_source_type = task_payload['data_source_type']
|
||||
task.kb_sync_enabled = task_payload['kb_sync_enabled']
|
||||
task.kb_group_id = (
|
||||
int(task_payload['kb_group_id'])
|
||||
if task_payload.get('kb_group_id') not in (None, '', 0, '0')
|
||||
else None
|
||||
)
|
||||
db.session.commit()
|
||||
if cfg:
|
||||
upsert_schedule_task_config(task.id, cfg)
|
||||
upsert_schedule_task_config(task.id, cfg, commit=False)
|
||||
db.session.commit()
|
||||
imported += 1
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
errors.append(str(exc))
|
||||
skipped += 1
|
||||
db.session.commit()
|
||||
return {'ok': True, 'imported': imported, 'skipped': skipped, 'errors': errors[:20]}
|
||||
Reference in new issue
Block a user