# Conflicts: # packages/cloud-platform/cloud/schema.py
This commit is contained in:
@@ -9,6 +9,7 @@ from core.models import Task
|
||||
from host_agent.execution import ExecutionFactories
|
||||
from host_agent.planner_context import bind_planner_execution_context
|
||||
from host_agent.progress import TaskProgressHolder, TaskProgressSnapshot
|
||||
from runtime.task import is_cancellation_reason
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -32,18 +33,24 @@ class AssignmentExecutor:
|
||||
assignment: AssignmentModel,
|
||||
*,
|
||||
should_stop: Callable[[], bool] | None = None,
|
||||
stop_reason: Callable[[], str | None] | None = None,
|
||||
) -> AssignmentExecutionResult:
|
||||
self._progress.clear()
|
||||
with bind_planner_execution_context(assignment):
|
||||
if should_stop is not None and should_stop():
|
||||
reason = stop_reason() if stop_reason is not None else None
|
||||
return AssignmentExecutionResult(
|
||||
status="failed",
|
||||
failure_reason="execution interrupted",
|
||||
status="cancelled" if is_cancellation_reason(reason) else "failed",
|
||||
failure_reason=reason or "execution interrupted",
|
||||
)
|
||||
if assignment.workflow_definition_id is not None:
|
||||
return self._execute_workflow(assignment, should_stop=should_stop)
|
||||
return self._execute_workflow(
|
||||
assignment, should_stop=should_stop, stop_reason=stop_reason
|
||||
)
|
||||
if assignment.goal is not None:
|
||||
return self._execute_goal(assignment, should_stop=should_stop)
|
||||
return self._execute_goal(
|
||||
assignment, should_stop=should_stop, stop_reason=stop_reason
|
||||
)
|
||||
return AssignmentExecutionResult(
|
||||
status="failed",
|
||||
failure_reason="assignment has neither goal nor workflow definition",
|
||||
@@ -54,6 +61,7 @@ class AssignmentExecutor:
|
||||
assignment: AssignmentModel,
|
||||
*,
|
||||
should_stop: Callable[[], bool] | None,
|
||||
stop_reason: Callable[[], str | None] | None,
|
||||
) -> AssignmentExecutionResult:
|
||||
task = Task(goal=assignment.goal or "", device_id=assignment.device_id)
|
||||
if self.factories.metadata_store is not None:
|
||||
@@ -67,9 +75,11 @@ class AssignmentExecutor:
|
||||
if should_stop is None:
|
||||
completed = runner.run(task)
|
||||
else:
|
||||
completed = runner.run(task, should_stop=should_stop)
|
||||
completed = runner.run(
|
||||
task, should_stop=should_stop, stop_reason=stop_reason
|
||||
)
|
||||
return AssignmentExecutionResult(
|
||||
status="done" if completed.status == "completed" else "failed",
|
||||
status=_terminal_status(completed.status),
|
||||
failure_reason=completed.failure_reason,
|
||||
metadata={
|
||||
"runtime_task_id": completed.id,
|
||||
@@ -82,6 +92,7 @@ class AssignmentExecutor:
|
||||
assignment: AssignmentModel,
|
||||
*,
|
||||
should_stop: Callable[[], bool] | None,
|
||||
stop_reason: Callable[[], str | None] | None,
|
||||
) -> AssignmentExecutionResult:
|
||||
definition_id = assignment.workflow_definition_id or ""
|
||||
definition = self.factories.workflow_store.get_definition(definition_id)
|
||||
@@ -98,9 +109,10 @@ class AssignmentExecutor:
|
||||
definition,
|
||||
device_id=assignment.device_id,
|
||||
should_stop=should_stop,
|
||||
stop_reason=stop_reason,
|
||||
)
|
||||
return AssignmentExecutionResult(
|
||||
status="done" if run.status == "completed" else "failed",
|
||||
status=_terminal_status(run.status),
|
||||
failure_reason=(
|
||||
None if run.status == "completed" else f"workflow ended as {run.status}"
|
||||
),
|
||||
@@ -109,3 +121,11 @@ class AssignmentExecutor:
|
||||
"workflow_status": run.status,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _terminal_status(runtime_status: str) -> str:
|
||||
if runtime_status == "completed":
|
||||
return "done"
|
||||
if runtime_status == "cancelled":
|
||||
return "cancelled"
|
||||
return "failed"
|
||||
|
||||
@@ -14,6 +14,7 @@ from cloud.internal_api.models import (
|
||||
DeviceSnapshotModel,
|
||||
HeartbeatResponse,
|
||||
HostEnrollmentResponse,
|
||||
HostTaskCancellationResponse,
|
||||
HostTaskSubmissionResponse,
|
||||
LeaseRenewalResponse,
|
||||
TaskProgressModel,
|
||||
@@ -219,6 +220,17 @@ class HostAgentClient:
|
||||
"control plane returned malformed success payload"
|
||||
) from exc
|
||||
|
||||
async def cancel_task(self, task_id: str) -> HostTaskCancellationResponse:
|
||||
response = await self._client.request(
|
||||
"POST",
|
||||
f"/internal/v1/hosts/{self.config.host_id}/tasks/{task_id}/cancel",
|
||||
json={"host_id": self.config.host_id},
|
||||
headers={"Authorization": f"Bearer {self.config.token}"},
|
||||
)
|
||||
if not response.is_success:
|
||||
_raise_api_error(response)
|
||||
return HostTaskCancellationResponse.model_validate(response.json())
|
||||
|
||||
async def claim(self) -> AssignmentModel | None:
|
||||
response = await self._request(
|
||||
"POST",
|
||||
|
||||
@@ -12,6 +12,7 @@ from cloud.internal_api.models import AssignmentModel
|
||||
from host_agent.assignment import AssignmentExecutionResult
|
||||
from host_agent.client import HostAgentAPIError, HostAgentClient, StaleLeaseError
|
||||
from host_agent.progress import TaskProgressSnapshot
|
||||
from runtime.task import is_cancellation_reason
|
||||
|
||||
|
||||
class InterruptibleAssignmentExecutor(Protocol):
|
||||
@@ -20,6 +21,7 @@ class InterruptibleAssignmentExecutor(Protocol):
|
||||
assignment: AssignmentModel,
|
||||
*,
|
||||
should_stop: Callable[[], bool] | None = None,
|
||||
stop_reason: Callable[[], str | None] | None = None,
|
||||
) -> AssignmentExecutionResult: ...
|
||||
|
||||
def latest_progress(self) -> TaskProgressSnapshot | None: ...
|
||||
@@ -36,6 +38,10 @@ class LeaseGuard:
|
||||
with self._lock:
|
||||
return self._reason
|
||||
|
||||
@property
|
||||
def is_cancellation(self) -> bool:
|
||||
return is_cancellation_reason(self.reason)
|
||||
|
||||
def is_lost(self) -> bool:
|
||||
return self._lost.is_set()
|
||||
|
||||
@@ -69,6 +75,7 @@ class ActiveAssignmentRunner:
|
||||
self.executor.execute,
|
||||
assignment,
|
||||
should_stop=lambda: guard.is_lost() or self._stop_requested.is_set(),
|
||||
stop_reason=lambda: guard.reason,
|
||||
)
|
||||
)
|
||||
renewal = asyncio.create_task(
|
||||
@@ -108,4 +115,7 @@ class ActiveAssignmentRunner:
|
||||
guard.mark_lost("lease renewal failed after transport retries")
|
||||
return
|
||||
else:
|
||||
if response.cancel_requested:
|
||||
guard.mark_lost("cancellation requested by control plane")
|
||||
return
|
||||
lease_expires_at = response.lease_expires_at
|
||||
|
||||
@@ -47,8 +47,11 @@ class AssignmentProcessor:
|
||||
self.status_tracker.mark_assignment_started(assignment)
|
||||
try:
|
||||
execution = await self.active_executor.run(assignment)
|
||||
status = "done" if execution.status == "done" else "failed"
|
||||
failure_reason = execution.failure_reason if status == "failed" else None
|
||||
if execution.status in {"done", "cancelled"}:
|
||||
status = execution.status
|
||||
else:
|
||||
status = "failed"
|
||||
failure_reason = execution.failure_reason if status != "done" else None
|
||||
response = await self.client.report_result(
|
||||
assignment,
|
||||
status=status,
|
||||
|
||||
@@ -41,7 +41,9 @@ CSRF_FORM_FIELD = "csrf_token"
|
||||
_LOOPBACK_BIND_HOSTS = frozenset({"127.0.0.1", "localhost", "::1"})
|
||||
|
||||
TaskSubmissionCallable = Callable[..., Awaitable[str]]
|
||||
TaskCancellationCallable = Callable[..., Awaitable[Any]]
|
||||
AUTOMATIC_DEVICE_VALUE = "__automatic__"
|
||||
_TERMINAL_LOCAL_TASK_STATUSES = frozenset({"completed", "failed", "cancelled"})
|
||||
|
||||
_ENV = jinja2.Environment(
|
||||
loader=jinja2.FileSystemLoader(Path(__file__).parent / "templates"),
|
||||
@@ -217,6 +219,7 @@ def create_console_app(
|
||||
enrollment_client: HostAgentEnrollmentClient | None,
|
||||
host_client: HostAgentClient | None = None,
|
||||
submit_self_task: TaskSubmissionCallable | None = None,
|
||||
cancel_task: TaskCancellationCallable | None = None,
|
||||
metadata_store: TaskMetadataStore | None = None,
|
||||
timeline: Timeline | None = None,
|
||||
executor: AssignmentExecutor | None = None,
|
||||
@@ -225,6 +228,8 @@ def create_console_app(
|
||||
cookie_secure = config.console_bind_host not in _LOOPBACK_BIND_HOSTS
|
||||
if submit_self_task is None and host_client is not None:
|
||||
submit_self_task = host_client.submit_self_task
|
||||
if cancel_task is None and host_client is not None:
|
||||
cancel_task = host_client.cancel_task
|
||||
submission_available = submit_self_task is not None
|
||||
|
||||
def _running_devices() -> list[dict[str, str]]:
|
||||
@@ -709,6 +714,7 @@ def create_console_app(
|
||||
@app.get("/tasks/{task_id}", response_class=HTMLResponse)
|
||||
async def task_detail_page(
|
||||
task_id: str,
|
||||
request: Request,
|
||||
session: SessionState = Depends(require_session),
|
||||
) -> HTMLResponse:
|
||||
if metadata_store is None:
|
||||
@@ -738,13 +744,57 @@ def create_console_app(
|
||||
if task.get(key) is not None
|
||||
]
|
||||
timeline_steps = [_timeline_step_context(record) for record in timeline_records]
|
||||
can_cancel = (
|
||||
cancel_task is not None
|
||||
and task.get("source_task_id") is not None
|
||||
and task.get("status") not in _TERMINAL_LOCAL_TASK_STATUSES
|
||||
)
|
||||
cancel_notice = (
|
||||
"Cancellation requested. It may take a moment to take effect."
|
||||
if request.query_params.get("cancelled") == "1"
|
||||
else None
|
||||
)
|
||||
cancel_error = (
|
||||
"Failed to request cancellation. Try again."
|
||||
if request.query_params.get("cancel_error") == "1"
|
||||
else None
|
||||
)
|
||||
return _render(
|
||||
"task_detail.html",
|
||||
title=f"Task {task_id}",
|
||||
session=session,
|
||||
csrf_token=session.csrf_token,
|
||||
task=task,
|
||||
task_rows=task_rows,
|
||||
timeline_steps=timeline_steps,
|
||||
can_cancel=can_cancel,
|
||||
cancel_notice=cancel_notice,
|
||||
cancel_error=cancel_error,
|
||||
)
|
||||
|
||||
@app.post("/tasks/{task_id}/cancel")
|
||||
async def tasks_cancel(
|
||||
task_id: str,
|
||||
session: SessionState = Depends(require_csrf),
|
||||
) -> Response:
|
||||
if metadata_store is None:
|
||||
raise HTTPException(
|
||||
status_code=503, detail="task metadata store not configured"
|
||||
)
|
||||
task = await asyncio.to_thread(metadata_store.get_task, task_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="task not found")
|
||||
source_task_id = task.get("source_task_id")
|
||||
if cancel_task is None or source_task_id is None:
|
||||
return RedirectResponse(
|
||||
url=f"/tasks/{task_id}?cancel_error=1", status_code=303
|
||||
)
|
||||
try:
|
||||
await cancel_task(source_task_id)
|
||||
except HostAgentAPIError:
|
||||
return RedirectResponse(
|
||||
url=f"/tasks/{task_id}?cancel_error=1", status_code=303
|
||||
)
|
||||
return RedirectResponse(url=f"/tasks/{task_id}?cancelled=1", status_code=303)
|
||||
|
||||
return app
|
||||
|
||||
@@ -42,6 +42,14 @@
|
||||
<thead><tr><th>Field</th><th>Value</th></tr></thead>
|
||||
<tbody>{% for row in task_rows %}<tr><td>{{ row[0] }}</td><td>{{ row[1] }}</td></tr>{% endfor %}</tbody>
|
||||
</table>
|
||||
{% if cancel_notice %}<p class="notice" id="cancel-notice">{{ cancel_notice }}</p>{% endif %}
|
||||
{% if cancel_error %}<p class="error" id="cancel-error">{{ cancel_error }}</p>{% endif %}
|
||||
{% if can_cancel %}
|
||||
<form method="post" action="/tasks/{{ task['id'] }}/cancel">
|
||||
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
||||
<p><button type="submit">Cancel task</button></p>
|
||||
</form>
|
||||
{% endif %}
|
||||
<h2>Timeline</h2>
|
||||
{% if not timeline_steps %}
|
||||
<p>No timeline records.</p>
|
||||
|
||||
Reference in New Issue
Block a user