feat(cloud-store): persist task lease history

This commit is contained in:
2026-07-12 17:15:09 +08:00
parent 52d2a86a4c
commit 0a9392b7ea
5 changed files with 155 additions and 7 deletions
+82
View File
@@ -8,8 +8,10 @@ from uuid import uuid4
import pytest
from sqlalchemy import event
from sqlalchemy.orm import Session
from cloud.database import CloudDatabase
from cloud.db_models import TaskAttemptRow
from cloud.plugins import PluginManifest
from cloud.pool import PooledDevice
from cloud.repository import CloudRepository, LeasedAssignment, TaskAttemptRecord
@@ -196,3 +198,83 @@ def test_repository_state_survives_application_restart(database_url: str) -> Non
assert device == _device(device_id, host_id)
finally:
restarted_process.close()
def test_task_lease_and_terminal_fields_round_trip(database_url: str) -> None:
database = CloudDatabase(database_url)
task_id = _unique_id("lease-task")
created_at = datetime(2026, 7, 12, 1, 0, tzinfo=UTC)
lease_expires_at = datetime(2026, 7, 12, 1, 5, tzinfo=UTC)
updated_at = datetime(2026, 7, 12, 1, 1, tzinfo=UTC)
task = ScheduledTask(
id=task_id,
goal="capture diagnostics",
workflow_definition_id=None,
constraints=TaskConstraints(),
status="failed",
assigned_device_id="device-a",
assigned_host_id="host-a",
attempt_count=2,
lease_id="lease-a",
lease_expires_at=lease_expires_at,
terminal_result={"steps": 3, "status": "failed"},
failure_reason="device disconnected",
updated_at=updated_at,
created_at=created_at,
)
try:
database.repository.enqueue_task(task)
assert database.repository.get_task(task_id) == task
finally:
database.close()
def test_task_attempt_history_is_ordered_and_complete(database_url: str) -> None:
database = CloudDatabase(database_url)
task_id = _unique_id("attempt-task")
created_at = datetime(2026, 7, 12, 1, 0, tzinfo=UTC)
lease_expires_at = datetime(2026, 7, 12, 1, 5, tzinfo=UTC)
try:
with Session(database.engine) as session, session.begin():
session.add_all(
[
TaskAttemptRow(
task_id=task_id,
attempt=2,
lease_id="lease-2",
host_id="host-b",
device_id="device-b",
status="failed",
lease_expires_at=lease_expires_at.isoformat(),
created_at=created_at.isoformat(),
completed_at=lease_expires_at.isoformat(),
failure_reason="execution failed",
result_json='{"exit_code": 1}',
),
TaskAttemptRow(
task_id=task_id,
attempt=1,
lease_id="lease-1",
host_id="host-a",
device_id="device-a",
status="expired",
lease_expires_at=lease_expires_at.isoformat(),
created_at=created_at.isoformat(),
completed_at=lease_expires_at.isoformat(),
failure_reason="lease expired",
result_json=None,
),
]
)
attempts = database.repository.list_task_attempts(task_id)
assert [attempt.attempt for attempt in attempts] == [1, 2]
assert attempts[0].status == "expired"
assert attempts[1].terminal_result == {"exit_code": 1}
assert attempts[1].failure_reason == "execution failed"
finally:
database.close()