feat: wire core FSM and events into services
这个提交存在于:
父节点
7546e6e0af
当前提交
069b5f58a3
共有 7 个文件被更改,包括 243 次插入 和 34 次删除
|
|
@ -6,6 +6,7 @@ from typing import Any
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from backend.dto.common import ApiError, ErrorCode
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
|
from core.model_router import SUPPORTED, UNSUPPORTED, route_from_capability_profile, select_model_route
|
||||||
from database.repositories.capability_repository import CapabilityRepository
|
from database.repositories.capability_repository import CapabilityRepository
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -123,24 +124,30 @@ class ApiDiscoveryService:
|
||||||
],
|
],
|
||||||
"warnings": [],
|
"warnings": [],
|
||||||
}
|
}
|
||||||
if profile["status"] != "confirmed_supported":
|
route = route_from_capability_profile(profile)
|
||||||
|
selection = select_model_route([route])
|
||||||
|
if selection.status != SUPPORTED:
|
||||||
|
code = (
|
||||||
|
ErrorCode.MODEL_CAPABILITY_UNSUPPORTED
|
||||||
|
if selection.status == UNSUPPORTED
|
||||||
|
else ErrorCode.MODEL_CAPABILITY_UNCONFIRMED
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
"valid": False,
|
"valid": False,
|
||||||
"status": profile["status"],
|
"status": selection.status,
|
||||||
"errors": [
|
"errors": [
|
||||||
{
|
{
|
||||||
"code": ErrorCode.MODEL_CAPABILITY_UNCONFIRMED.value,
|
"code": code.value,
|
||||||
"message": "model capabilities are not confirmed",
|
"message": selection.reason or "model capabilities are not confirmed",
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"warnings": [],
|
"warnings": [],
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
}
|
}
|
||||||
supported = set(profile["capabilities"].get("supports", []))
|
missing = [capability for capability in required if capability not in route.capabilities]
|
||||||
missing = [capability for capability in required if capability not in supported]
|
|
||||||
return {
|
return {
|
||||||
"valid": not missing,
|
"valid": not missing,
|
||||||
"status": profile["status"],
|
"status": selection.status,
|
||||||
"errors": [
|
"errors": [
|
||||||
{
|
{
|
||||||
"code": ErrorCode.MODEL_CAPABILITY_UNSUPPORTED.value,
|
"code": ErrorCode.MODEL_CAPABILITY_UNSUPPORTED.value,
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ from typing import Any
|
||||||
from backend.services.image_service import ImageService
|
from backend.services.image_service import ImageService
|
||||||
from backend.services.queue_service import QueueService
|
from backend.services.queue_service import QueueService
|
||||||
from backend.services.workflow_service import WorkflowService
|
from backend.services.workflow_service import WorkflowService
|
||||||
|
from core.event_bus import EventBus
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from engines.single_flow import SingleFlowEngine
|
from engines.single_flow import SingleFlowEngine
|
||||||
|
|
||||||
|
|
@ -18,11 +19,13 @@ class GenerationService:
|
||||||
queue_service: QueueService,
|
queue_service: QueueService,
|
||||||
image_service: ImageService,
|
image_service: ImageService,
|
||||||
single_flow_engine: SingleFlowEngine | None = None,
|
single_flow_engine: SingleFlowEngine | None = None,
|
||||||
|
event_bus: EventBus | None = None,
|
||||||
):
|
):
|
||||||
self.workflow_service = workflow_service
|
self.workflow_service = workflow_service
|
||||||
self.queue_service = queue_service
|
self.queue_service = queue_service
|
||||||
self.image_service = image_service
|
self.image_service = image_service
|
||||||
self.single_flow_engine = single_flow_engine or SingleFlowEngine()
|
self.single_flow_engine = single_flow_engine or SingleFlowEngine()
|
||||||
|
self.event_bus = event_bus
|
||||||
|
|
||||||
def generate_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
|
def generate_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
|
||||||
node = self.workflow_service.get_node(node_id)
|
node = self.workflow_service.get_node(node_id)
|
||||||
|
|
@ -39,6 +42,10 @@ class GenerationService:
|
||||||
},
|
},
|
||||||
ctx,
|
ctx,
|
||||||
)
|
)
|
||||||
|
self._publish(
|
||||||
|
"generation.node_queued",
|
||||||
|
{"workflow_id": workflow.workflow_id, "node_id": node_id, "task_id": task.task_id},
|
||||||
|
)
|
||||||
self.workflow_service.update_node(
|
self.workflow_service.update_node(
|
||||||
node_id,
|
node_id,
|
||||||
{
|
{
|
||||||
|
|
@ -54,6 +61,10 @@ class GenerationService:
|
||||||
)
|
)
|
||||||
self.queue_service.update_task(task.task_id, {"status": "running", "progress": 20}, ctx)
|
self.queue_service.update_task(task.task_id, {"status": "running", "progress": 20}, ctx)
|
||||||
running_node = self.workflow_service.update_node(node_id, {"status": "running"}, ctx)
|
running_node = self.workflow_service.update_node(node_id, {"status": "running"}, ctx)
|
||||||
|
self._publish(
|
||||||
|
"generation.node_running",
|
||||||
|
{"workflow_id": workflow.workflow_id, "node_id": node_id, "task_id": task.task_id},
|
||||||
|
)
|
||||||
assets = self.single_flow_engine.generate(running_node, task, payload)
|
assets = self.single_flow_engine.generate(running_node, task, payload)
|
||||||
images = [
|
images = [
|
||||||
self.image_service.create_image(
|
self.image_service.create_image(
|
||||||
|
|
@ -86,9 +97,22 @@ class GenerationService:
|
||||||
ctx,
|
ctx,
|
||||||
)
|
)
|
||||||
completed_node = self.workflow_service.update_node(node_id, {"status": "completed"}, ctx)
|
completed_node = self.workflow_service.update_node(node_id, {"status": "completed"}, ctx)
|
||||||
|
self._publish(
|
||||||
|
"generation.node_completed",
|
||||||
|
{
|
||||||
|
"workflow_id": workflow.workflow_id,
|
||||||
|
"node_id": node_id,
|
||||||
|
"task_id": completed_task.task_id,
|
||||||
|
"image_ids": [image.image_id for image in images],
|
||||||
|
},
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
"task_id": completed_task.task_id,
|
"task_id": completed_task.task_id,
|
||||||
"task_status": completed_task.status,
|
"task_status": completed_task.status,
|
||||||
"node_status": completed_node.status,
|
"node_status": completed_node.status,
|
||||||
"images": [image.to_dict() for image in images],
|
"images": [image.to_dict() for image in images],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _publish(self, event_type: str, payload: dict[str, Any]) -> None:
|
||||||
|
if self.event_bus:
|
||||||
|
self.event_bus.publish(event_type, payload)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ from uuid import uuid4
|
||||||
from backend.dto.common import ApiError, ErrorCode
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
from backend.dto.job_dto import GenerationTask, JobListResult
|
from backend.dto.job_dto import GenerationTask, JobListResult
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
|
from core.event_bus import EventBus
|
||||||
|
from core.fsm import task_transition
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from database.repositories.job_repository import JobRepository
|
from database.repositories.job_repository import JobRepository
|
||||||
|
|
||||||
|
|
@ -25,9 +27,10 @@ TASK_STATUSES = {
|
||||||
|
|
||||||
|
|
||||||
class QueueService:
|
class QueueService:
|
||||||
def __init__(self, repository: JobRepository, audit_service: AuditService):
|
def __init__(self, repository: JobRepository, audit_service: AuditService, event_bus: EventBus | None = None):
|
||||||
self.repository = repository
|
self.repository = repository
|
||||||
self.audit_service = audit_service
|
self.audit_service = audit_service
|
||||||
|
self.event_bus = event_bus
|
||||||
|
|
||||||
def create_task(self, payload: dict[str, Any], ctx: RequestContext) -> GenerationTask:
|
def create_task(self, payload: dict[str, Any], ctx: RequestContext) -> GenerationTask:
|
||||||
status = str(payload.get("status", "created"))
|
status = str(payload.get("status", "created"))
|
||||||
|
|
@ -61,6 +64,15 @@ class QueueService:
|
||||||
target_id=task.task_id,
|
target_id=task.task_id,
|
||||||
after_snapshot=task.to_dict(),
|
after_snapshot=task.to_dict(),
|
||||||
)
|
)
|
||||||
|
self._publish(
|
||||||
|
"generation.task_created",
|
||||||
|
{
|
||||||
|
"task_id": task.task_id,
|
||||||
|
"status": task.status,
|
||||||
|
"workflow_id": task.workflow_id,
|
||||||
|
"node_id": task.node_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
return task
|
return task
|
||||||
|
|
||||||
def list_jobs(
|
def list_jobs(
|
||||||
|
|
@ -108,6 +120,7 @@ class QueueService:
|
||||||
"task cannot be cancelled from current status",
|
"task cannot be cancelled from current status",
|
||||||
{"task_id": task_id, "status": before.status},
|
{"task_id": task_id, "status": before.status},
|
||||||
)
|
)
|
||||||
|
self._validate_task_status_transition(before.status, "cancelled")
|
||||||
after = self.repository.update(task_id, {"status": "cancelled"})
|
after = self.repository.update(task_id, {"status": "cancelled"})
|
||||||
if after is None:
|
if after is None:
|
||||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "task not found", {"task_id": task_id})
|
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "task not found", {"task_id": task_id})
|
||||||
|
|
@ -119,6 +132,7 @@ class QueueService:
|
||||||
before_snapshot=before.to_dict(),
|
before_snapshot=before.to_dict(),
|
||||||
after_snapshot=after.to_dict(),
|
after_snapshot=after.to_dict(),
|
||||||
)
|
)
|
||||||
|
self._publish_status_change(before, after)
|
||||||
return after
|
return after
|
||||||
|
|
||||||
def retry_job(self, task_id: str, ctx: RequestContext) -> GenerationTask:
|
def retry_job(self, task_id: str, ctx: RequestContext) -> GenerationTask:
|
||||||
|
|
@ -129,6 +143,7 @@ class QueueService:
|
||||||
"task cannot be retried from current status",
|
"task cannot be retried from current status",
|
||||||
{"task_id": task_id, "status": before.status},
|
{"task_id": task_id, "status": before.status},
|
||||||
)
|
)
|
||||||
|
self._validate_task_status_transition(before.status, "queued")
|
||||||
after = self.repository.update(
|
after = self.repository.update(
|
||||||
task_id,
|
task_id,
|
||||||
{
|
{
|
||||||
|
|
@ -148,17 +163,14 @@ class QueueService:
|
||||||
before_snapshot=before.to_dict(),
|
before_snapshot=before.to_dict(),
|
||||||
after_snapshot=after.to_dict(),
|
after_snapshot=after.to_dict(),
|
||||||
)
|
)
|
||||||
|
self._publish_status_change(before, after)
|
||||||
return after
|
return after
|
||||||
|
|
||||||
def update_task(self, task_id: str, payload: dict[str, Any], ctx: RequestContext) -> GenerationTask:
|
def update_task(self, task_id: str, payload: dict[str, Any], ctx: RequestContext) -> GenerationTask:
|
||||||
before = self.get_job(task_id)
|
before = self.get_job(task_id)
|
||||||
status = payload.get("status")
|
status = payload.get("status")
|
||||||
if status is not None and status not in TASK_STATUSES:
|
if status is not None:
|
||||||
raise ApiError(
|
self._validate_task_status_transition(before.status, str(status))
|
||||||
ErrorCode.VALIDATION_FAILED,
|
|
||||||
"unsupported task status",
|
|
||||||
{"field": "status", "allowed": sorted(TASK_STATUSES)},
|
|
||||||
)
|
|
||||||
after = self.repository.update(task_id, payload)
|
after = self.repository.update(task_id, payload)
|
||||||
if after is None:
|
if after is None:
|
||||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "task not found", {"task_id": task_id})
|
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "task not found", {"task_id": task_id})
|
||||||
|
|
@ -170,6 +182,9 @@ class QueueService:
|
||||||
before_snapshot=before.to_dict(),
|
before_snapshot=before.to_dict(),
|
||||||
after_snapshot=after.to_dict(),
|
after_snapshot=after.to_dict(),
|
||||||
)
|
)
|
||||||
|
if before.status != after.status:
|
||||||
|
self._publish_status_change(before, after)
|
||||||
|
self._publish("generation.task_updated", {"task_id": task_id, "status": after.status})
|
||||||
return after
|
return after
|
||||||
|
|
||||||
def queue_summary(self) -> dict[str, int]:
|
def queue_summary(self) -> dict[str, int]:
|
||||||
|
|
@ -211,6 +226,37 @@ class QueueService:
|
||||||
)
|
)
|
||||||
return {"moved": moved, "total": len(moved)}
|
return {"moved": moved, "total": len(moved)}
|
||||||
|
|
||||||
|
def _validate_task_status_transition(self, current: str, target: str) -> None:
|
||||||
|
if target not in TASK_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"unsupported task status",
|
||||||
|
{"field": "status", "allowed": sorted(TASK_STATUSES)},
|
||||||
|
)
|
||||||
|
transition = task_transition(current, target)
|
||||||
|
if not transition.allowed:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"task status transition is not allowed",
|
||||||
|
{"current": current, "target": target, "reason": transition.reason},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _publish_status_change(self, before: GenerationTask, after: GenerationTask) -> None:
|
||||||
|
self._publish(
|
||||||
|
"generation.task_status_changed",
|
||||||
|
{
|
||||||
|
"task_id": after.task_id,
|
||||||
|
"workflow_id": after.workflow_id,
|
||||||
|
"node_id": after.node_id,
|
||||||
|
"before_status": before.status,
|
||||||
|
"after_status": after.status,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _publish(self, event_type: str, payload: dict[str, Any]) -> None:
|
||||||
|
if self.event_bus:
|
||||||
|
self.event_bus.publish(event_type, payload)
|
||||||
|
|
||||||
|
|
||||||
def new_task_id() -> str:
|
def new_task_id() -> str:
|
||||||
return f"task_{uuid4().hex}"
|
return f"task_{uuid4().hex}"
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ from uuid import uuid4
|
||||||
from backend.dto.common import ApiError, ErrorCode
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowDetail, WorkflowNode
|
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowDetail, WorkflowNode
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
|
from core.event_bus import EventBus
|
||||||
|
from core.fsm import node_transition, workflow_transition
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from database.repositories.profile_repository import ProfileRepository
|
from database.repositories.profile_repository import ProfileRepository
|
||||||
from database.repositories.workflow_repository import WorkflowRepository
|
from database.repositories.workflow_repository import WorkflowRepository
|
||||||
|
|
@ -37,10 +39,12 @@ class WorkflowService:
|
||||||
workflow_repository: WorkflowRepository,
|
workflow_repository: WorkflowRepository,
|
||||||
profile_repository: ProfileRepository,
|
profile_repository: ProfileRepository,
|
||||||
audit_service: AuditService,
|
audit_service: AuditService,
|
||||||
|
event_bus: EventBus | None = None,
|
||||||
):
|
):
|
||||||
self.workflow_repository = workflow_repository
|
self.workflow_repository = workflow_repository
|
||||||
self.profile_repository = profile_repository
|
self.profile_repository = profile_repository
|
||||||
self.audit_service = audit_service
|
self.audit_service = audit_service
|
||||||
|
self.event_bus = event_bus
|
||||||
|
|
||||||
def create_workflow(
|
def create_workflow(
|
||||||
self,
|
self,
|
||||||
|
|
@ -90,6 +94,15 @@ class WorkflowService:
|
||||||
target_id=workflow.workflow_id,
|
target_id=workflow.workflow_id,
|
||||||
after_snapshot=workflow.to_dict(),
|
after_snapshot=workflow.to_dict(),
|
||||||
)
|
)
|
||||||
|
self._publish(
|
||||||
|
"workflow.created",
|
||||||
|
{
|
||||||
|
"workflow_id": workflow.workflow_id,
|
||||||
|
"profile_id": workflow.profile_id,
|
||||||
|
"workflow_type": workflow.workflow_type,
|
||||||
|
"node_ids": [node.node_id for node in nodes],
|
||||||
|
},
|
||||||
|
)
|
||||||
return WorkflowDetail(workflow=workflow, nodes=nodes)
|
return WorkflowDetail(workflow=workflow, nodes=nodes)
|
||||||
|
|
||||||
def list_workflows(self, profile_id: str) -> list[ProfileWorkflow]:
|
def list_workflows(self, profile_id: str) -> list[ProfileWorkflow]:
|
||||||
|
|
@ -116,12 +129,8 @@ class WorkflowService:
|
||||||
ctx: RequestContext,
|
ctx: RequestContext,
|
||||||
) -> ProfileWorkflow:
|
) -> ProfileWorkflow:
|
||||||
before = self.get_workflow(workflow_id).workflow
|
before = self.get_workflow(workflow_id).workflow
|
||||||
if "status" in payload and payload["status"] not in WORKFLOW_STATUSES:
|
if "status" in payload:
|
||||||
raise ApiError(
|
self._validate_workflow_status_transition(before.status, str(payload["status"]))
|
||||||
ErrorCode.VALIDATION_FAILED,
|
|
||||||
"unsupported workflow status",
|
|
||||||
{"field": "status", "allowed": sorted(WORKFLOW_STATUSES)},
|
|
||||||
)
|
|
||||||
if "display_name" in payload and not str(payload["display_name"]).strip():
|
if "display_name" in payload and not str(payload["display_name"]).strip():
|
||||||
raise ApiError(
|
raise ApiError(
|
||||||
ErrorCode.VALIDATION_FAILED,
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
|
@ -142,6 +151,17 @@ class WorkflowService:
|
||||||
before_snapshot=before.to_dict(),
|
before_snapshot=before.to_dict(),
|
||||||
after_snapshot=after.to_dict(),
|
after_snapshot=after.to_dict(),
|
||||||
)
|
)
|
||||||
|
if before.status != after.status:
|
||||||
|
self._publish(
|
||||||
|
"workflow.status_changed",
|
||||||
|
{
|
||||||
|
"workflow_id": workflow_id,
|
||||||
|
"profile_id": after.profile_id,
|
||||||
|
"before_status": before.status,
|
||||||
|
"after_status": after.status,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self._publish("workflow.updated", {"workflow_id": workflow_id, "profile_id": after.profile_id})
|
||||||
return after
|
return after
|
||||||
|
|
||||||
def create_branch(
|
def create_branch(
|
||||||
|
|
@ -183,6 +203,14 @@ class WorkflowService:
|
||||||
target_id=branch.branch_id,
|
target_id=branch.branch_id,
|
||||||
after_snapshot={**branch.to_dict(), "copied_node_ids": [node.node_id for node in copied_nodes]},
|
after_snapshot={**branch.to_dict(), "copied_node_ids": [node.node_id for node in copied_nodes]},
|
||||||
)
|
)
|
||||||
|
self._publish(
|
||||||
|
"workflow.branch_created",
|
||||||
|
{
|
||||||
|
"workflow_id": workflow_id,
|
||||||
|
"branch_name": branch.branch_name,
|
||||||
|
"copied_node_ids": [node.node_id for node in copied_nodes],
|
||||||
|
},
|
||||||
|
)
|
||||||
return branch
|
return branch
|
||||||
|
|
||||||
def list_branches(self, workflow_id: str) -> list[WorkflowBranch]:
|
def list_branches(self, workflow_id: str) -> list[WorkflowBranch]:
|
||||||
|
|
@ -280,6 +308,10 @@ class WorkflowService:
|
||||||
target_id=node.node_id,
|
target_id=node.node_id,
|
||||||
after_snapshot=node.to_dict(),
|
after_snapshot=node.to_dict(),
|
||||||
)
|
)
|
||||||
|
self._publish(
|
||||||
|
"workflow.node_created",
|
||||||
|
{"workflow_id": workflow_id, "node_id": node.node_id, "node_key": node.node_key},
|
||||||
|
)
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def list_nodes(self, workflow_id: str) -> list[WorkflowNode]:
|
def list_nodes(self, workflow_id: str) -> list[WorkflowNode]:
|
||||||
|
|
@ -294,12 +326,8 @@ class WorkflowService:
|
||||||
|
|
||||||
def update_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> WorkflowNode:
|
def update_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> WorkflowNode:
|
||||||
before = self.get_node(node_id)
|
before = self.get_node(node_id)
|
||||||
if "status" in payload and payload["status"] not in NODE_STATUSES:
|
if "status" in payload:
|
||||||
raise ApiError(
|
self._validate_node_status_transition(before.status, str(payload["status"]))
|
||||||
ErrorCode.VALIDATION_FAILED,
|
|
||||||
"unsupported node status",
|
|
||||||
{"field": "status", "allowed": sorted(NODE_STATUSES)},
|
|
||||||
)
|
|
||||||
if "title" in payload and not str(payload["title"]).strip():
|
if "title" in payload and not str(payload["title"]).strip():
|
||||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "node title is required", {"field": "title"})
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "node title is required", {"field": "title"})
|
||||||
update_payload = dict(payload)
|
update_payload = dict(payload)
|
||||||
|
|
@ -316,6 +344,17 @@ class WorkflowService:
|
||||||
before_snapshot=before.to_dict(),
|
before_snapshot=before.to_dict(),
|
||||||
after_snapshot=after.to_dict(),
|
after_snapshot=after.to_dict(),
|
||||||
)
|
)
|
||||||
|
if before.status != after.status:
|
||||||
|
self._publish(
|
||||||
|
"workflow.node_status_changed",
|
||||||
|
{
|
||||||
|
"workflow_id": after.workflow_id,
|
||||||
|
"node_id": node_id,
|
||||||
|
"before_status": before.status,
|
||||||
|
"after_status": after.status,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self._publish("workflow.node_updated", {"workflow_id": after.workflow_id, "node_id": node_id})
|
||||||
return after
|
return after
|
||||||
|
|
||||||
def _create_initial_nodes(
|
def _create_initial_nodes(
|
||||||
|
|
@ -388,6 +427,40 @@ class WorkflowService:
|
||||||
)
|
)
|
||||||
return workflow
|
return workflow
|
||||||
|
|
||||||
|
def _validate_workflow_status_transition(self, current: str, target: str) -> None:
|
||||||
|
if target not in WORKFLOW_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"unsupported workflow status",
|
||||||
|
{"field": "status", "allowed": sorted(WORKFLOW_STATUSES)},
|
||||||
|
)
|
||||||
|
transition = workflow_transition(current, target)
|
||||||
|
if not transition.allowed:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"workflow status transition is not allowed",
|
||||||
|
{"current": current, "target": target, "reason": transition.reason},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _validate_node_status_transition(self, current: str, target: str) -> None:
|
||||||
|
if target not in NODE_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"unsupported node status",
|
||||||
|
{"field": "status", "allowed": sorted(NODE_STATUSES)},
|
||||||
|
)
|
||||||
|
transition = node_transition(current, target)
|
||||||
|
if not transition.allowed:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"node status transition is not allowed",
|
||||||
|
{"current": current, "target": target, "reason": transition.reason},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _publish(self, event_type: str, payload: dict[str, Any]) -> None:
|
||||||
|
if self.event_bus:
|
||||||
|
self.event_bus.publish(event_type, payload)
|
||||||
|
|
||||||
|
|
||||||
def new_workflow_id() -> str:
|
def new_workflow_id() -> str:
|
||||||
return f"workflow_{uuid4().hex}"
|
return f"workflow_{uuid4().hex}"
|
||||||
|
|
|
||||||
20
core/fsm.py
20
core/fsm.py
|
|
@ -6,7 +6,8 @@ from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
WORKFLOW_TRANSITIONS: dict[str, set[str]] = {
|
WORKFLOW_TRANSITIONS: dict[str, set[str]] = {
|
||||||
"draft": {"running", "archived"},
|
"draft": {"active", "running", "completed", "failed", "cancelled", "archived"},
|
||||||
|
"active": {"running", "completed", "failed", "cancelled", "archived"},
|
||||||
"running": {"completed", "failed", "cancelled", "archived"},
|
"running": {"completed", "failed", "cancelled", "archived"},
|
||||||
"completed": {"archived"},
|
"completed": {"archived"},
|
||||||
"failed": {"running", "archived"},
|
"failed": {"running", "archived"},
|
||||||
|
|
@ -15,13 +16,16 @@ WORKFLOW_TRANSITIONS: dict[str, set[str]] = {
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_TRANSITIONS: dict[str, set[str]] = {
|
NODE_TRANSITIONS: dict[str, set[str]] = {
|
||||||
|
"draft": {"queued", "running", "completed", "selected", "failed", "skipped", "discarded"},
|
||||||
"pending": {"queued", "running", "completed", "failed", "skipped"},
|
"pending": {"queued", "running", "completed", "failed", "skipped"},
|
||||||
"queued": {"running", "completed", "failed", "cancelled"},
|
"queued": {"running", "completed", "selected", "failed", "cancelled", "discarded"},
|
||||||
"running": {"completed", "failed", "cancelled"},
|
"running": {"completed", "selected", "failed", "cancelled", "discarded"},
|
||||||
"completed": {"queued"},
|
"completed": {"queued", "selected", "discarded"},
|
||||||
"failed": {"queued", "skipped"},
|
"selected": {"queued", "completed", "discarded"},
|
||||||
"cancelled": {"queued"},
|
"failed": {"queued", "skipped", "discarded"},
|
||||||
"skipped": {"queued"},
|
"cancelled": {"queued", "discarded"},
|
||||||
|
"skipped": {"queued", "discarded"},
|
||||||
|
"discarded": set(),
|
||||||
}
|
}
|
||||||
|
|
||||||
TASK_TRANSITIONS: dict[str, set[str]] = {
|
TASK_TRANSITIONS: dict[str, set[str]] = {
|
||||||
|
|
@ -30,7 +34,7 @@ TASK_TRANSITIONS: dict[str, set[str]] = {
|
||||||
"running": {"completed", "succeeded", "failed", "cancelled"},
|
"running": {"completed", "succeeded", "failed", "cancelled"},
|
||||||
"completed": set(),
|
"completed": set(),
|
||||||
"succeeded": set(),
|
"succeeded": set(),
|
||||||
"failed": {"queued", "dead_letter"},
|
"failed": {"queued", "cancelled", "dead_letter"},
|
||||||
"cancelled": {"queued"},
|
"cancelled": {"queued"},
|
||||||
"dead_letter": {"queued"},
|
"dead_letter": {"queued"},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from backend.routes.node_routes import generate_node
|
from backend.routes.node_routes import generate_node
|
||||||
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
from backend.services.generation_service import GenerationService
|
from backend.services.generation_service import GenerationService
|
||||||
from backend.services.image_service import ImageService
|
from backend.services.image_service import ImageService
|
||||||
from backend.services.profile_service import ProfileService
|
from backend.services.profile_service import ProfileService
|
||||||
|
|
@ -12,6 +13,7 @@ from backend.services.queue_service import QueueService
|
||||||
from backend.services.workflow_service import WorkflowService
|
from backend.services.workflow_service import WorkflowService
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
from core.config_service import ConfigService
|
from core.config_service import ConfigService
|
||||||
|
from core.event_bus import EventBus
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from core.storage import VfsStorage
|
from core.storage import VfsStorage
|
||||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||||
|
|
@ -65,12 +67,14 @@ storage:
|
||||||
self.audit_service,
|
self.audit_service,
|
||||||
)
|
)
|
||||||
self.queue_service = QueueService(self.job_repository, self.audit_service)
|
self.queue_service = QueueService(self.job_repository, self.audit_service)
|
||||||
|
self.event_bus = EventBus()
|
||||||
self.config_service = ConfigService(self.config_path)
|
self.config_service = ConfigService(self.config_path)
|
||||||
self.generation_service = GenerationService(
|
self.generation_service = GenerationService(
|
||||||
workflow_service=self.workflow_service,
|
workflow_service=self.workflow_service,
|
||||||
queue_service=self.queue_service,
|
queue_service=self.queue_service,
|
||||||
image_service=self.image_service,
|
image_service=self.image_service,
|
||||||
single_flow_engine=SingleFlowEngine(VfsStorage(self.config_service)),
|
single_flow_engine=SingleFlowEngine(VfsStorage(self.config_service)),
|
||||||
|
event_bus=self.event_bus,
|
||||||
)
|
)
|
||||||
self.ctx = RequestContext.from_astrbot(
|
self.ctx = RequestContext.from_astrbot(
|
||||||
username="admin",
|
username="admin",
|
||||||
|
|
@ -119,6 +123,22 @@ storage:
|
||||||
self.assertTrue(VfsStorage(self.config_service).ensure_file(image["image_uri"]).exists())
|
self.assertTrue(VfsStorage(self.config_service).ensure_file(image["image_uri"]).exists())
|
||||||
self.assertTrue(VfsStorage(self.config_service).ensure_file(image["thumbnail_uri"]).exists())
|
self.assertTrue(VfsStorage(self.config_service).ensure_file(image["thumbnail_uri"]).exists())
|
||||||
|
|
||||||
|
self.assertEqual(self.event_bus.history("generation.node_queued")[0].payload["task_id"], data["task_id"])
|
||||||
|
self.assertEqual(self.event_bus.history("generation.node_completed")[0].payload["image_ids"][0], data["images"][0]["image_id"])
|
||||||
|
|
||||||
|
def test_queue_service_rejects_invalid_task_transition(self):
|
||||||
|
task = self.queue_service.create_task({"status": "succeeded"}, self.ctx)
|
||||||
|
|
||||||
|
with self.assertRaises(ApiError) as raised:
|
||||||
|
self.queue_service.retry_job(task.task_id, self.ctx)
|
||||||
|
|
||||||
|
self.assertEqual(raised.exception.error_code, ErrorCode.TASK_NOT_RETRYABLE)
|
||||||
|
|
||||||
|
with self.assertRaises(ApiError) as transition_error:
|
||||||
|
self.queue_service.update_task(task.task_id, {"status": "queued"}, self.ctx)
|
||||||
|
|
||||||
|
self.assertEqual(transition_error.exception.error_code, ErrorCode.VALIDATION_FAILED)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ from backend.dto.common import ApiError, ErrorCode
|
||||||
from backend.services.profile_service import ProfileService
|
from backend.services.profile_service import ProfileService
|
||||||
from backend.services.workflow_service import WorkflowService
|
from backend.services.workflow_service import WorkflowService
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
|
from core.event_bus import EventBus
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||||
from database.repositories.profile_repository import ProfileRepository
|
from database.repositories.profile_repository import ProfileRepository
|
||||||
|
|
@ -221,6 +222,40 @@ class WorkflowServiceTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(count, 1)
|
self.assertEqual(count, 1)
|
||||||
|
|
||||||
|
def test_workflow_service_publishes_status_events(self):
|
||||||
|
event_bus = EventBus()
|
||||||
|
workflow_service = WorkflowService(
|
||||||
|
workflow_repository=self.workflow_repository,
|
||||||
|
profile_repository=self.profile_repository,
|
||||||
|
audit_service=self.audit_service,
|
||||||
|
event_bus=event_bus,
|
||||||
|
)
|
||||||
|
detail = workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{"workflow_type": "single_image", "display_name": "Evented"},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
workflow_service.update_workflow(detail.workflow.workflow_id, {"status": "running"}, self.ctx)
|
||||||
|
workflow_service.update_node(detail.nodes[0].node_id, {"status": "queued"}, self.ctx)
|
||||||
|
|
||||||
|
self.assertEqual(event_bus.history("workflow.created")[0].payload["workflow_id"], detail.workflow.workflow_id)
|
||||||
|
self.assertEqual(event_bus.history("workflow.status_changed")[0].payload["after_status"], "running")
|
||||||
|
self.assertEqual(event_bus.history("workflow.node_status_changed")[0].payload["after_status"], "queued")
|
||||||
|
|
||||||
|
def test_invalid_workflow_status_transition_is_rejected(self):
|
||||||
|
detail = self.workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{"workflow_type": "single_image", "display_name": "Archived"},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
self.workflow_service.update_workflow(detail.workflow.workflow_id, {"status": "archived"}, self.ctx)
|
||||||
|
|
||||||
|
with self.assertRaises(ApiError) as raised:
|
||||||
|
self.workflow_service.update_workflow(detail.workflow.workflow_id, {"status": "running"}, self.ctx)
|
||||||
|
|
||||||
|
self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED)
|
||||||
|
|
||||||
def test_invalid_workflow_type_is_rejected(self):
|
def test_invalid_workflow_type_is_rejected(self):
|
||||||
with self.assertRaises(ApiError) as raised:
|
with self.assertRaises(ApiError) as raised:
|
||||||
self.workflow_service.create_workflow(
|
self.workflow_service.create_workflow(
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用