Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix:创建多个同时触发的/cron任务后退出直接删除cron任务持久化文件,清空定时任务列表

openJiuwen-bot91e66f7debac
diff --git a/jiuwenswarm/gateway/cron/scheduler.py b/jiuwenswarm/gateway/cron/scheduler.py
index 709062b526..627ccfc497 100644
--- a/jiuwenswarm/gateway/cron/scheduler.py
+++ b/jiuwenswarm/gateway/cron/scheduler.py
@@ -115,9 +115,13 @@ class CronSchedulerService:
self._last_store_mtime = self._get_store_mtime()
async def _check_store_changed(self) -> bool:
- """If cron_jobs.json was modified externally, reload and return True."""
+ """If cron_jobs.json was modified or deleted externally, reload and return True."""
mtime = self._get_store_mtime()
- if mtime and mtime != self._last_store_mtime and self._last_store_mtime != 0.0:
+ # Detect: file modified (mtime changed, both nonzero),
+ # file deleted (mtime became 0.0 from nonzero),
+ # file recreated (mtime became nonzero from 0.0).
+ # Skip: no change (mtime == last), or both 0.0 (never had a file).
+ if mtime != self._last_store_mtime and (mtime or self._last_store_mtime):
logger.info(
"[Cron] store file changed (mtime %.3f -> %.3f), reloading",
self._last_store_mtime,
@@ -283,6 +287,19 @@ class CronSchedulerService:
job = self._jobs.get(ev.job_id)
if job is None and ev.kind != "push_update":
return
+ # For wake/push events: if the job exists in memory but no longer
+ # exists in the persistent store (e.g. cron_jobs.json was deleted),
+ # skip execution and reload to clear stale in-memory data.
+ if ev.kind in ("wake", "push") and job is not None:
+ store_job = await self._store.get_job(ev.job_id)
+ if store_job is None:
+ logger.info(
+ "[Cron] job %s no longer in store (file may have been deleted), "
+ "skipping event %s and triggering reload",
+ ev.job_id, ev.kind,
+ )
+ await self.reload()
+ return
if job is None and ev.kind == "push_update":
state = self._runs.get(ev.run_id)
if state is None:
diff --git a/tests/unit_tests/gateway/test_cron_scheduler.py b/tests/unit_tests/gateway/test_cron_scheduler.py
new file mode 100644
index 0000000000..e3db4b1d31
--- /dev/null
+++ b/tests/unit_tests/gateway/test_cron_scheduler.py
@@ -0,0 +1,298 @@
+"""Unit tests for CronSchedulerService: store file deletion and event validation bugs."""
+from __future__ import annotations
+
+import asyncio
+import time
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+
+from jiuwenswarm.gateway.cron.models import CronJob, CronRunState
+from jiuwenswarm.gateway.cron.scheduler import CronSchedulerService, _Event
+from jiuwenswarm.gateway.cron.store import CronJobStore
+
+
+# ── Helpers ──────────────────────────────────────────────────────────────────
+
+class _TestableScheduler(CronSchedulerService):
+ """Subclass that exposes protected members as public methods.
+
+ G.CLS.11 forbids accessing protected members from outside the class
+ hierarchy. By subclassing, we can access them legitimately and then
+ expose thin public wrappers for test assertions — no source changes needed.
+ """
+
+ async def check_store_changed(self):
+ # Delegate to protected method from within the subclass.
+ return await self._check_store_changed()
+
+ async def handle_event(self, ev):
+ return await self._handle_event(ev)
+
+ @property
+ def jobs(self):
+ return self._jobs
+
+ @property
+ def last_store_mtime(self):
+ return self._last_store_mtime
+
+ @property
+ def runs(self):
+ return self._runs
+
+
+def _make_job(job_id="job-1", name="test", **overrides):
+ """Build a CronJob with sensible defaults for testing."""
+ defaults = {
+ "id": job_id,
+ "name": name,
+ "enabled": True,
+ "expired": False,
+ "cron_expr": "0 0 9 * * ? *",
+ "timezone": "Asia/Shanghai",
+ "wake_offset_seconds": 300,
+ "description": "reminder",
+ "targets": "tui",
+ "created_at": time.time(),
+ "updated_at": time.time(),
+ }
+ defaults.update(overrides)
+ return CronJob(**defaults)
+
+
+class FakeAgentClient:
+ """Stub AgentServerClient that never calls a real agent."""
+
+ async def send_request(self, *a, **kw):
+ return {"content": {"output": "done", "result_type": "answer"}}
+
+
+class FakeMessageHandler:
+ """Stub MessageHandler that records published messages."""
+
+ def __init__(self):
+ self.published = []
+
+ async def publish_robot_messages(self, msg):
+ self.published.append(msg)
+
+
+async def _create_one_job(store, name="job", targets="tui"):
+ """Convenience: create a single cron job via the store."""
+ return await store.create_job(
+ name=name,
+ cron_expr="0 0 9 * * ? *",
+ timezone="Asia/Shanghai",
+ description="reminder",
+ targets=targets,
+ )
+
+
+def _make_scheduler(store, handler=None):
+ """Build a _TestableScheduler with fake deps for testing."""
+ return _TestableScheduler(
+ store=store,
+ agent_client=FakeAgentClient(),
+ message_handler=handler or FakeMessageHandler(),
+ )
+
+
+# ── _check_store_changed ─────────────────────────────────────────────────────
+
+
+class TestCheckStoreChanged:
+ """_check_store_changed detects file deletion, modification, recreation."""
+
+ @pytest.mark.asyncio
+ async def test_file_deleted_triggers_reload(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ await _create_one_job(store)
+ assert store_file.exists()
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+ assert svc.last_store_mtime != 0.0
+ assert len(svc.jobs) == 1
+
+ # Delete file -> mtime becomes 0.0
+ store_file.unlink()
+ assert not store_file.exists()
+
+ changed = await svc.check_store_changed()
+ assert changed is True
+ assert len(svc.jobs) == 0
+
+ @pytest.mark.asyncio
+ async def test_file_modified_triggers_reload(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ await _create_one_job(store, name="job-1")
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+
+ # Modify file externally via second store
+ store2 = CronJobStore(path=store_file)
+ await _create_one_job(store2, name="job-2", targets="web")
+
+ changed = await svc.check_store_changed()
+ assert changed is True
+ assert len(svc.jobs) == 2
+
+ @pytest.mark.asyncio
+ async def test_file_recreated_triggers_reload(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ await _create_one_job(store)
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+
+ # Delete -> triggers first reload -> mtime becomes 0.0
+ store_file.unlink()
+ changed1 = await svc.check_store_changed()
+ assert changed1 is True
+ assert len(svc.jobs) == 0
+
+ # Recreate with a new job
+ store3 = CronJobStore(path=store_file)
+ await _create_one_job(store3, name="new-job", targets="web")
+
+ changed2 = await svc.check_store_changed()
+ assert changed2 is True
+ assert len(svc.jobs) == 1
+ assert "new-job" in [j.name for j in svc.jobs.values()]
+
+ @pytest.mark.asyncio
+ async def test_no_change_does_not_reload(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ await _create_one_job(store)
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+
+ changed = await svc.check_store_changed()
+ assert changed is False
+ assert len(svc.jobs) == 1
+
+ @pytest.mark.asyncio
+ async def test_never_had_file_does_not_reload(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ # File never created
+ store = CronJobStore(path=store_file)
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+ assert svc.last_store_mtime == 0.0
+
+ changed = await svc.check_store_changed()
+ assert changed is False
+
+
+# ── _handle_event ────────────────────────────────────────────────────────────
+
+
+class TestHandleEventStoreValidation:
+ """_handle_event skips wake/push when job absent from store."""
+
+ @pytest.mark.asyncio
+ async def test_wake_skipped_when_job_absent_from_store(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ job = await _create_one_job(store)
+
+ handler = FakeMessageHandler()
+ svc = _make_scheduler(store, handler)
+ await svc.reload()
+ assert len(svc.jobs) == 1
+
+ # Delete file -> store.get_job returns None
+ store_file.unlink()
+
+ ev = _Event(at_ts=time.time(), seq=1, kind="wake", job_id=job.id, run_id=f"{job.id}:1234")
+ await svc.handle_event(ev)
+
+ # Reload clears memory; wake not executed; no messages published
+ assert len(svc.jobs) == 0
+ assert len(handler.published) == 0
+
+ @pytest.mark.asyncio
+ async def test_push_skipped_when_job_absent_from_store(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ job = await _create_one_job(store)
+
+ handler = FakeMessageHandler()
+ svc = _make_scheduler(store, handler)
+ await svc.reload()
+
+ store_file.unlink()
+
+ ev = _Event(at_ts=time.time(), seq=1, kind="push", job_id=job.id, run_id=f"{job.id}:1234")
+ await svc.handle_event(ev)
+
+ assert len(svc.jobs) == 0
+ assert len(handler.published) == 0
+
+ @pytest.mark.asyncio
+ async def test_push_update_not_skipped_when_job_absent(self, tmp_path):
+ # push_update must always proceed — it delivers results of
+ # already-triggered runs that must not be lost.
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ job = await _create_one_job(store)
+
+ handler = FakeMessageHandler()
+ svc = _make_scheduler(store, handler)
+ await svc.reload()
+
+ # Simulate a completed run with a result to deliver
+ run_id = f"{job.id}:1234"
+ svc.runs[run_id] = CronRunState(
+ run_id=run_id,
+ job_id=job.id,
+ wake_at_iso="2026-06-09T08:55:00+08:00",
+ push_at_iso="2026-06-09T09:00:00+08:00",
+ job_name=job.name,
+ targets=job.targets,
+ session_id=None,
+ chat_type=None,
+ timezone=job.timezone,
+ result_text="result: 9am now",
+ )
+
+ # Delete file — job gone from store
+ store_file.unlink()
+
+ ev = _Event(at_ts=time.time(), seq=1, kind="push_update", job_id=job.id, run_id=run_id)
+ await svc.handle_event(ev)
+
+ # push_update still delivered even though store file is gone
+ assert len(handler.published) == 1
+
+ @pytest.mark.asyncio
+ async def test_wake_executes_normally_when_job_present(self, tmp_path):
+ store_file = tmp_path / "cron_jobs.json"
+ store = CronJobStore(path=store_file)
+ job = await _create_one_job(store)
+
+ svc = _make_scheduler(store)
+ await svc.reload()
+
+ wake_called = False
+
+ async def _mock_on_wake(self, j, r):
+ nonlocal wake_called
+ wake_called = True
+
+ # patch.object targets the original class method name
+ with patch.object(CronSchedulerService, "_on_wake", _mock_on_wake):
+ ev = _Event(at_ts=time.time(), seq=1, kind="wake", job_id=job.id, run_id=f"{job.id}:1234")
+ await svc.handle_event(ev)
+
+ assert wake_called is True
+ assert len(svc.jobs) == 1
\ No newline at end of file