This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Literal, Protocol
|
||||
from typing import TYPE_CHECKING, Literal, Protocol
|
||||
|
||||
from cloud.internal_api.models import AssignmentModel
|
||||
from host_agent.assignment import AssignmentExecutionResult
|
||||
from host_agent.client import HostAgentClient
|
||||
from host_agent.status import AgentStatusTracker
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
|
||||
class ActiveAssignmentExecutor(Protocol):
|
||||
@@ -25,24 +29,42 @@ class AssignmentProcessor:
|
||||
self,
|
||||
client: HostAgentClient,
|
||||
active_executor: ActiveAssignmentExecutor,
|
||||
*,
|
||||
status_tracker: AgentStatusTracker | None = None,
|
||||
on_result: Callable[[AssignmentModel, AssignmentProcessingResult], None]
|
||||
| None = None,
|
||||
) -> None:
|
||||
self.client = client
|
||||
self.active_executor = active_executor
|
||||
self.status_tracker = status_tracker
|
||||
self.on_result = on_result
|
||||
|
||||
def request_stop(self) -> None:
|
||||
self.active_executor.request_stop()
|
||||
|
||||
async def process(self, assignment: AssignmentModel) -> AssignmentProcessingResult:
|
||||
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
|
||||
response = await self.client.report_result(
|
||||
assignment,
|
||||
status=status,
|
||||
failure_reason=failure_reason,
|
||||
result=dict(execution.metadata),
|
||||
)
|
||||
return AssignmentProcessingResult(
|
||||
execution=execution,
|
||||
report_status=response.status,
|
||||
)
|
||||
if self.status_tracker is not None:
|
||||
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
|
||||
response = await self.client.report_result(
|
||||
assignment,
|
||||
status=status,
|
||||
failure_reason=failure_reason,
|
||||
result=dict(execution.metadata),
|
||||
)
|
||||
result = AssignmentProcessingResult(
|
||||
execution=execution,
|
||||
report_status=response.status,
|
||||
)
|
||||
finally:
|
||||
if self.status_tracker is not None:
|
||||
self.status_tracker.mark_assignment_finished()
|
||||
if self.on_result is not None:
|
||||
try:
|
||||
self.on_result(assignment, result)
|
||||
except Exception:
|
||||
pass
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user