feat(cron): add run history tracking for cron jobs
Record run_at_ms, status, duration_ms and error for each execution, keeping the last 20 entries per job in jobs.json. Adds CronRunRecord dataclass, get_job() lookup, and four regression tests covering success, error, trimming and persistence. Closes #1837 Made-with: Cursor
This commit is contained in:
@@ -10,7 +10,7 @@ from typing import Any, Callable, Coroutine
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.cron.types import CronJob, CronJobState, CronPayload, CronSchedule, CronStore
|
||||
from nanobot.cron.types import CronJob, CronJobState, CronPayload, CronRunRecord, CronSchedule, CronStore
|
||||
|
||||
|
||||
def _now_ms() -> int:
|
||||
@@ -63,10 +63,12 @@ def _validate_schedule_for_add(schedule: CronSchedule) -> None:
|
||||
class CronService:
|
||||
"""Service for managing and executing scheduled jobs."""
|
||||
|
||||
_MAX_RUN_HISTORY = 20
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
store_path: Path,
|
||||
on_job: Callable[[CronJob], Coroutine[Any, Any, str | None]] | None = None
|
||||
on_job: Callable[[CronJob], Coroutine[Any, Any, str | None]] | None = None,
|
||||
):
|
||||
self.store_path = store_path
|
||||
self.on_job = on_job
|
||||
@@ -113,6 +115,15 @@ class CronService:
|
||||
last_run_at_ms=j.get("state", {}).get("lastRunAtMs"),
|
||||
last_status=j.get("state", {}).get("lastStatus"),
|
||||
last_error=j.get("state", {}).get("lastError"),
|
||||
run_history=[
|
||||
CronRunRecord(
|
||||
run_at_ms=r["runAtMs"],
|
||||
status=r["status"],
|
||||
duration_ms=r.get("durationMs", 0),
|
||||
error=r.get("error"),
|
||||
)
|
||||
for r in j.get("state", {}).get("runHistory", [])
|
||||
],
|
||||
),
|
||||
created_at_ms=j.get("createdAtMs", 0),
|
||||
updated_at_ms=j.get("updatedAtMs", 0),
|
||||
@@ -160,6 +171,15 @@ class CronService:
|
||||
"lastRunAtMs": j.state.last_run_at_ms,
|
||||
"lastStatus": j.state.last_status,
|
||||
"lastError": j.state.last_error,
|
||||
"runHistory": [
|
||||
{
|
||||
"runAtMs": r.run_at_ms,
|
||||
"status": r.status,
|
||||
"durationMs": r.duration_ms,
|
||||
"error": r.error,
|
||||
}
|
||||
for r in j.state.run_history
|
||||
],
|
||||
},
|
||||
"createdAtMs": j.created_at_ms,
|
||||
"updatedAtMs": j.updated_at_ms,
|
||||
@@ -248,9 +268,8 @@ class CronService:
|
||||
logger.info("Cron: executing job '{}' ({})", job.name, job.id)
|
||||
|
||||
try:
|
||||
response = None
|
||||
if self.on_job:
|
||||
response = await self.on_job(job)
|
||||
await self.on_job(job)
|
||||
|
||||
job.state.last_status = "ok"
|
||||
job.state.last_error = None
|
||||
@@ -261,8 +280,17 @@ class CronService:
|
||||
job.state.last_error = str(e)
|
||||
logger.error("Cron: job '{}' failed: {}", job.name, e)
|
||||
|
||||
end_ms = _now_ms()
|
||||
job.state.last_run_at_ms = start_ms
|
||||
job.updated_at_ms = _now_ms()
|
||||
job.updated_at_ms = end_ms
|
||||
|
||||
job.state.run_history.append(CronRunRecord(
|
||||
run_at_ms=start_ms,
|
||||
status=job.state.last_status,
|
||||
duration_ms=end_ms - start_ms,
|
||||
error=job.state.last_error,
|
||||
))
|
||||
job.state.run_history = job.state.run_history[-self._MAX_RUN_HISTORY:]
|
||||
|
||||
# Handle one-shot jobs
|
||||
if job.schedule.kind == "at":
|
||||
@@ -366,6 +394,11 @@ class CronService:
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_job(self, job_id: str) -> CronJob | None:
|
||||
"""Get a job by ID."""
|
||||
store = self._load_store()
|
||||
return next((j for j in store.jobs if j.id == job_id), None)
|
||||
|
||||
def status(self) -> dict:
|
||||
"""Get service status."""
|
||||
store = self._load_store()
|
||||
|
||||
@@ -29,6 +29,15 @@ class CronPayload:
|
||||
to: str | None = None # e.g. phone number
|
||||
|
||||
|
||||
@dataclass
|
||||
class CronRunRecord:
|
||||
"""A single execution record for a cron job."""
|
||||
run_at_ms: int
|
||||
status: Literal["ok", "error", "skipped"]
|
||||
duration_ms: int = 0
|
||||
error: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class CronJobState:
|
||||
"""Runtime state of a job."""
|
||||
@@ -36,6 +45,7 @@ class CronJobState:
|
||||
last_run_at_ms: int | None = None
|
||||
last_status: Literal["ok", "error", "skipped"] | None = None
|
||||
last_error: str | None = None
|
||||
run_history: list[CronRunRecord] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user