cron: reload jobs store on each timer tick

This commit is contained in:
yzchen
2026-03-02 11:19:45 +08:00
parent a5962170f6
commit dba93ae83a
2 changed files with 31 additions and 0 deletions

View File

@@ -226,6 +226,8 @@ class CronService:
async def _on_timer(self) -> None: async def _on_timer(self) -> None:
"""Handle timer tick - run due jobs.""" """Handle timer tick - run due jobs."""
# Pick up external CLI/file changes before deciding due jobs.
self._load_store()
if not self._store: if not self._store:
return return

View File

@@ -1,3 +1,5 @@
import asyncio
import pytest import pytest
from nanobot.cron.service import CronService from nanobot.cron.service import CronService
@@ -28,3 +30,30 @@ def test_add_job_accepts_valid_timezone(tmp_path) -> None:
assert job.schedule.tz == "America/Vancouver" assert job.schedule.tz == "America/Vancouver"
assert job.state.next_run_at_ms is not None assert job.state.next_run_at_ms is not None
@pytest.mark.asyncio
async def test_running_service_honors_external_disable(tmp_path) -> None:
store_path = tmp_path / "cron" / "jobs.json"
called: list[str] = []
async def on_job(job) -> None:
called.append(job.id)
service = CronService(store_path, on_job=on_job)
job = service.add_job(
name="external-disable",
schedule=CronSchedule(kind="every", every_ms=200),
message="hello",
)
await service.start()
try:
external = CronService(store_path)
updated = external.enable_job(job.id, enabled=False)
assert updated is not None
assert updated.enabled is False
await asyncio.sleep(0.35)
assert called == []
finally:
service.stop()