mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-15 07:14:08 +00:00
fix: record cancelled cron runs
maintainer edit: treat job-level CancelledError as a failed cron run so bound automation cancellations update run history and do not break subsequent scheduling.
This commit is contained in:
parent
a326ba40f4
commit
f82ab9f192
@ -448,6 +448,13 @@ class CronService:
|
||||
job.state.last_error = None
|
||||
logger.info("Cron: job '{}' completed", job.name)
|
||||
|
||||
except asyncio.CancelledError as e:
|
||||
current = asyncio.current_task()
|
||||
if current is not None and current.cancelling():
|
||||
raise
|
||||
job.state.last_status = "error"
|
||||
job.state.last_error = str(e) or e.__class__.__name__
|
||||
logger.exception("Cron: job '{}' was cancelled", job.name)
|
||||
except Exception as e:
|
||||
job.state.last_status = "error"
|
||||
job.state.last_error = str(e)
|
||||
|
||||
@ -137,6 +137,33 @@ async def test_run_history_records_errors(tmp_path) -> None:
|
||||
assert loaded.state.run_history[0].error == "boom"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_history_records_job_cancellation(tmp_path) -> None:
|
||||
store_path = tmp_path / "cron" / "jobs.json"
|
||||
|
||||
async def cancel(_):
|
||||
raise asyncio.CancelledError("turn cancelled")
|
||||
|
||||
service = CronService(store_path, on_job=cancel)
|
||||
job = service.add_job(
|
||||
name="cancel",
|
||||
schedule=CronSchedule(kind="every", every_ms=60_000),
|
||||
message="hello",
|
||||
session_key="websocket:chat-1",
|
||||
)
|
||||
|
||||
assert await service.run_job(job.id) is True
|
||||
|
||||
loaded = service.get_job(job.id)
|
||||
assert loaded is not None
|
||||
assert loaded.state.last_status == "error"
|
||||
assert loaded.state.last_error == "turn cancelled"
|
||||
assert len(loaded.state.run_history) == 1
|
||||
assert loaded.state.run_history[0].status == "error"
|
||||
assert loaded.state.run_history[0].error == "turn cancelled"
|
||||
assert loaded.state.next_run_at_ms is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_history_trimmed_to_max(tmp_path) -> None:
|
||||
store_path = tmp_path / "cron" / "jobs.json"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user