|
|
@@ -3,21 +3,13 @@ from sqlalchemy import text
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
from core_domain import ServiceHealth
|
|
|
-from app.application.services import RuntimeApplicationService
|
|
|
+from app.application.services import RuntimeApplicationService, build_runtime_application_service
|
|
|
from app.bootstrap.settings import RuntimeServiceSettings
|
|
|
from app.db.session import get_db
|
|
|
-from app.domain.repositories import (
|
|
|
- ExecutionLogRepository,
|
|
|
- NodeArtifactRepository,
|
|
|
- NodeRunRepository,
|
|
|
- TraceSpanRepository,
|
|
|
- WorkflowRunRepository,
|
|
|
-)
|
|
|
-from app.infrastructure.code_runner_client import CodeRunnerClient, CodeRunnerClientError
|
|
|
-from app.infrastructure.executors import build_node_execution_dispatcher_with_clients
|
|
|
-from app.infrastructure.model_gateway_client import ModelGatewayClient, ModelGatewayClientError
|
|
|
-from app.infrastructure.tool_client import ToolServiceClient, ToolServiceClientError
|
|
|
-from app.infrastructure.workflow_client import WorkflowServiceClient, WorkflowServiceClientError
|
|
|
+from app.infrastructure.code_runner_client import CodeRunnerClientError
|
|
|
+from app.infrastructure.model_gateway_client import ModelGatewayClientError
|
|
|
+from app.infrastructure.tool_client import ToolServiceClientError
|
|
|
+from app.infrastructure.workflow_client import WorkflowServiceClientError
|
|
|
from app.schemas.run import (
|
|
|
ExecutionLogResponse,
|
|
|
NodeArtifactResponse,
|
|
|
@@ -30,6 +22,8 @@ from app.schemas.run import (
|
|
|
RunExecuteRequest,
|
|
|
RunExecuteResponse,
|
|
|
TraceSpanResponse,
|
|
|
+ WorkerExecuteNextRequest,
|
|
|
+ WorkerExecuteNextResponse,
|
|
|
WorkflowRunResponse,
|
|
|
WorkflowRunStatusUpdateRequest,
|
|
|
)
|
|
|
@@ -45,19 +39,7 @@ def get_runtime_application_service(
|
|
|
db: Session = Depends(get_db),
|
|
|
settings: RuntimeServiceSettings = Depends(get_runtime_settings),
|
|
|
) -> RuntimeApplicationService:
|
|
|
- return RuntimeApplicationService(
|
|
|
- workflow_run_repository=WorkflowRunRepository(db),
|
|
|
- node_run_repository=NodeRunRepository(db),
|
|
|
- execution_log_repository=ExecutionLogRepository(db),
|
|
|
- node_artifact_repository=NodeArtifactRepository(db),
|
|
|
- trace_span_repository=TraceSpanRepository(db),
|
|
|
- execution_dispatcher=build_node_execution_dispatcher_with_clients(
|
|
|
- code_runner_client=CodeRunnerClient(base_url=settings.code_runner_service_url),
|
|
|
- model_gateway_client=ModelGatewayClient(base_url=settings.model_gateway_service_url),
|
|
|
- tool_client=ToolServiceClient(base_url=settings.tool_service_url),
|
|
|
- ),
|
|
|
- workflow_client=WorkflowServiceClient(base_url=settings.workflow_service_url),
|
|
|
- )
|
|
|
+ return build_runtime_application_service(db=db, settings=settings)
|
|
|
|
|
|
|
|
|
@router.get("/health", response_model=ServiceHealth)
|
|
|
@@ -278,3 +260,34 @@ def execute_run(
|
|
|
node_runs=[NodeRunResponse.from_entity(item) for item in node_runs],
|
|
|
executor_names=executor_names,
|
|
|
)
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/workers/execute-next", response_model=WorkerExecuteNextResponse)
|
|
|
+def execute_next_worker_task(
|
|
|
+ payload: WorkerExecuteNextRequest,
|
|
|
+ settings: RuntimeServiceSettings = Depends(get_runtime_settings),
|
|
|
+ service: RuntimeApplicationService = Depends(get_runtime_application_service),
|
|
|
+) -> WorkerExecuteNextResponse:
|
|
|
+ try:
|
|
|
+ result = service.execute_next_claimed_node_run(
|
|
|
+ worker_key=payload.worker_key,
|
|
|
+ lease_seconds=payload.lease_seconds or settings.worker_lease_seconds,
|
|
|
+ )
|
|
|
+ except (
|
|
|
+ CodeRunnerClientError,
|
|
|
+ ModelGatewayClientError,
|
|
|
+ ToolServiceClientError,
|
|
|
+ WorkflowServiceClientError,
|
|
|
+ ) as exc:
|
|
|
+ raise HTTPException(status_code=502, detail=str(exc)) from exc
|
|
|
+
|
|
|
+ if result is None:
|
|
|
+ raise HTTPException(status_code=404, detail="queued worker task not found")
|
|
|
+
|
|
|
+ workflow_run, node_run, executor_name, released_lease_count = result
|
|
|
+ return WorkerExecuteNextResponse(
|
|
|
+ run=WorkflowRunResponse.from_entity(workflow_run),
|
|
|
+ node_run=NodeRunResponse.from_entity(node_run),
|
|
|
+ executor_name=executor_name,
|
|
|
+ released_lease_count=released_lease_count,
|
|
|
+ )
|