218 lines
7.7 KiB
Python
218 lines
7.7 KiB
Python
|
"""
|
||
|
任务实例API模块
|
||
|
提供任务实例的增删改查接口
|
||
|
"""
|
||
|
from typing import Dict, Any, List, Optional
|
||
|
from fastapi import APIRouter, HTTPException, Query
|
||
|
from pydantic import BaseModel, Field
|
||
|
from services.task_instance_service import TaskInstanceService, TaskInstanceStatus
|
||
|
from api.models import ApiResponse
|
||
|
|
||
|
# 创建路由器
|
||
|
router = APIRouter(prefix="/task-instances", tags=["任务实例"])
|
||
|
|
||
|
# 创建服务实例
|
||
|
task_instance_service = TaskInstanceService()
|
||
|
|
||
|
# 请求模型
|
||
|
class TaskInstanceCreateInput(BaseModel):
|
||
|
"""任务实例创建输入模型"""
|
||
|
task_id: str = Field(..., description="任务ID")
|
||
|
name: Optional[str] = Field(None, description="任务名称")
|
||
|
variables: Optional[Dict[str, Any]] = Field(None, description="任务变量")
|
||
|
priority: int = Field(1, description="任务优先级")
|
||
|
input_params: Optional[Dict[str, Any]] = Field(None, description="任务输入参数")
|
||
|
block_outputs: Optional[Dict[str, Any]] = Field(None, description="块输出参数")
|
||
|
context_params: Optional[Dict[str, Any]] = Field(None, description="上下文参数")
|
||
|
|
||
|
class TaskInstanceUpdateInput(BaseModel):
|
||
|
"""任务实例更新输入模型"""
|
||
|
variables: Optional[Dict[str, Any]] = Field(None, description="任务变量")
|
||
|
priority: Optional[int] = Field(None, description="任务优先级")
|
||
|
input_params: Optional[Dict[str, Any]] = Field(None, description="任务输入参数")
|
||
|
block_outputs: Optional[Dict[str, Any]] = Field(None, description="块输出参数")
|
||
|
context_params: Optional[Dict[str, Any]] = Field(None, description="上下文参数")
|
||
|
|
||
|
# API接口
|
||
|
@router.post("/create", response_model=ApiResponse)
|
||
|
async def create_task_instance(instance_input: TaskInstanceCreateInput):
|
||
|
"""创建任务实例"""
|
||
|
try:
|
||
|
# 创建任务实例
|
||
|
instance = task_instance_service.create_instance(
|
||
|
task_id=instance_input.task_id,
|
||
|
name=instance_input.name,
|
||
|
variables=instance_input.variables,
|
||
|
priority=instance_input.priority,
|
||
|
input_params=instance_input.input_params,
|
||
|
block_outputs=instance_input.block_outputs,
|
||
|
context_params=instance_input.context_params
|
||
|
)
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "创建任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except ValueError as e:
|
||
|
return {
|
||
|
"code": 400,
|
||
|
"message": str(e),
|
||
|
"data": None
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"创建任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.get("/{instance_id}", response_model=ApiResponse)
|
||
|
async def get_task_instance(instance_id: str):
|
||
|
"""获取任务实例详情"""
|
||
|
try:
|
||
|
# 获取任务实例
|
||
|
instance = task_instance_service.get_instance_by_id(instance_id)
|
||
|
if not instance:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": f"任务实例不存在: {instance_id}",
|
||
|
"data": None
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "获取任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"获取任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.get("/task/{task_id}", response_model=ApiResponse)
|
||
|
async def get_task_instances(task_id: str):
|
||
|
"""获取任务的所有实例"""
|
||
|
try:
|
||
|
# 获取任务实例
|
||
|
instances = task_instance_service.get_instances_by_task_id(task_id)
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "获取任务实例列表成功",
|
||
|
"data": {
|
||
|
"instances": instances,
|
||
|
"total": len(instances)
|
||
|
}
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"获取任务实例列表失败: {str(e)}")
|
||
|
|
||
|
@router.get("/task/{task_id}/latest", response_model=ApiResponse)
|
||
|
async def get_latest_task_instance(task_id: str):
|
||
|
"""获取任务的最新实例"""
|
||
|
try:
|
||
|
# 获取最新任务实例
|
||
|
instance = task_instance_service.get_latest_instance_by_task_id(task_id)
|
||
|
if not instance:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": f"任务 {task_id} 没有实例",
|
||
|
"data": None
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "获取最新任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"获取最新任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.put("/{instance_id}", response_model=ApiResponse)
|
||
|
async def update_task_instance(instance_id: str, instance_input: TaskInstanceUpdateInput):
|
||
|
"""更新任务实例"""
|
||
|
try:
|
||
|
# 更新任务实例
|
||
|
instance = task_instance_service.update_instance(
|
||
|
instance_id=instance_id,
|
||
|
variables=instance_input.variables,
|
||
|
priority=instance_input.priority,
|
||
|
input_params=instance_input.input_params,
|
||
|
block_outputs=instance_input.block_outputs,
|
||
|
context_params=instance_input.context_params
|
||
|
)
|
||
|
|
||
|
if not instance:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": f"任务实例不存在: {instance_id}",
|
||
|
"data": None
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "更新任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"更新任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.delete("/{instance_id}", response_model=ApiResponse)
|
||
|
async def delete_task_instance(instance_id: str):
|
||
|
"""删除任务实例"""
|
||
|
try:
|
||
|
# 删除任务实例
|
||
|
success = task_instance_service.delete_instance(instance_id)
|
||
|
|
||
|
if not success:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": f"任务实例不存在: {instance_id}",
|
||
|
"data": None
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "删除任务实例成功",
|
||
|
"data": None
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"删除任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.post("/{instance_id}/publish", response_model=ApiResponse)
|
||
|
async def publish_task_instance(instance_id: str):
|
||
|
"""发布任务实例"""
|
||
|
try:
|
||
|
# 发布任务实例
|
||
|
instance = task_instance_service.publish_instance(instance_id)
|
||
|
|
||
|
if not instance:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": f"任务实例不存在: {instance_id}",
|
||
|
"data": None
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "发布任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"发布任务实例失败: {str(e)}")
|
||
|
|
||
|
@router.get("/task/{task_id}/editing", response_model=ApiResponse)
|
||
|
async def get_or_create_editing_instance(task_id: str):
|
||
|
"""获取或创建编辑中的任务实例"""
|
||
|
try:
|
||
|
# 获取或创建编辑中的任务实例
|
||
|
instance = task_instance_service.get_or_create_editing_instance(task_id)
|
||
|
|
||
|
return {
|
||
|
"code": 200,
|
||
|
"message": "获取编辑中的任务实例成功",
|
||
|
"data": instance
|
||
|
}
|
||
|
except ValueError as e:
|
||
|
return {
|
||
|
"code": 404,
|
||
|
"message": str(e),
|
||
|
"data": None
|
||
|
}
|
||
|
except Exception as e:
|
||
|
raise HTTPException(status_code=500, detail=f"获取编辑中的任务实例失败: {str(e)}")
|