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

feat: 添加expire字段,单次任务执行后状态置为过期

Haowei_Dua1d864dfbc4a
diff --git a/jiuwenclaw/gateway/cron/models.py b/jiuwenclaw/gateway/cron/models.py
index e24688798e..6442209ee2 100644
--- a/jiuwenclaw/gateway/cron/models.py
+++ b/jiuwenclaw/gateway/cron/models.py
@@ -60,6 +60,8 @@ class CronJob:
timezone: str
wake_offset_seconds: int = 300
description: str = ""
+ # For one-shot schedules where croniter has no "next" after the run.
+ expired: bool = False
# Target channel ID to push results to (e.g. "web").
# JSON 字段名仍然叫 targets,用字符串保存频道 ID,兼容旧数据。
targets: str = ""
@@ -71,6 +73,7 @@ class CronJob:
"id": self.id,
"name": self.name,
"enabled": bool(self.enabled),
+ "expired": bool(self.expired),
"cron_expr": self.cron_expr,
"timezone": self.timezone,
"wake_offset_seconds": int(self.wake_offset_seconds),
@@ -87,6 +90,7 @@ class CronJob:
cron_expr = str(data.get("cron_expr") or "").strip()
timezone = str(data.get("timezone") or "").strip()
enabled = bool(data.get("enabled", False))
+ expired = bool(data.get("expired", False))
wake_offset_seconds_raw = data.get("wake_offset_seconds", 60)
try:
@@ -134,6 +138,7 @@ class CronJob:
id=job_id,
name=name,
enabled=enabled,
+ expired=expired,
cron_expr=cron_expr,
timezone=timezone,
wake_offset_seconds=wake_offset_seconds,
diff --git a/jiuwenclaw/gateway/cron/scheduler.py b/jiuwenclaw/gateway/cron/scheduler.py
index aff88180cc..e64b15e957 100644
--- a/jiuwenclaw/gateway/cron/scheduler.py
+++ b/jiuwenclaw/gateway/cron/scheduler.py
@@ -137,7 +137,20 @@ class CronSchedulerService:
try:
push_dt, wake_dt, run_id = self._compute_next_run(job, now_ts=now)
except Exception as exc: # noqa: BLE001
- logger.warning("[Cron] compute next run failed job=%s: %s", job.id, exc)
+ if self._is_croniter_no_next_date(exc):
+ # 已过期的 one-shot:标记 expired 并停用,避免 UI 仍显示 enabled。
+ try:
+ job.enabled = False
+ job.expired = True
+ await self._store.update_job(job.id, {"enabled": False, "expired": True})
+ except Exception as update_exc: # noqa: BLE001
+ logger.warning(
+ "[Cron] mark expired failed job=%s: %s",
+ job.id,
+ update_exc,
+ )
+ else:
+ logger.warning("[Cron] compute next run failed job=%s: %s", job.id, exc)
continue
self._schedule_event(wake_dt, "wake", job.id, run_id)
self._schedule_event(push_dt, "push", job.id, run_id)
@@ -175,6 +188,14 @@ class CronSchedulerService:
run_id = f"{job.id}:{int(push_dt.timestamp())}"
return push_dt, wake_dt, run_id
+ @staticmethod
+ def _is_croniter_no_next_date(exc: Exception) -> bool:
+ """croniter 找不到下一次日期(通常为单次 year 固定为过去)时视为过期。"""
+ return (
+ exc.__class__.__name__ == "CroniterBadDateError"
+ or "failed to find next date" in str(exc)
+ )
+
async def _loop(self) -> None:
while self._running:
try:
@@ -221,7 +242,20 @@ class CronSchedulerService:
self._schedule_event(wake_dt, "wake", job.id, next_run_id)
self._schedule_event(push_dt, "push", job.id, next_run_id)
except Exception as exc: # noqa: BLE001
- logger.warning("[Cron] compute next run failed after push job=%s: %s", job.id, exc)
+ if self._is_croniter_no_next_date(exc):
+ # 执行后无下一次:将任务标记为过期并停用。
+ try:
+ job.enabled = False
+ job.expired = True
+ await self._store.update_job(job.id, {"enabled": False, "expired": True})
+ except Exception as update_exc: # noqa: BLE001
+ logger.warning(
+ "[Cron] mark expired after push failed job=%s: %s",
+ job.id,
+ update_exc,
+ )
+ else:
+ logger.warning("[Cron] compute next run failed after push job=%s: %s", job.id, exc)
elif ev.kind == "push_update":
await self._on_push_update(job, ev.run_id)
diff --git a/jiuwenclaw/gateway/cron/store.py b/jiuwenclaw/gateway/cron/store.py
index e46f9892be..70eb58407a 100644
--- a/jiuwenclaw/gateway/cron/store.py
+++ b/jiuwenclaw/gateway/cron/store.py
@@ -91,9 +91,16 @@ class CronJobStore:
if "name" in patch:
updated = replace(updated, name=str(patch.get("name") or "").strip())
if "enabled" in patch:
- updated = replace(updated, enabled=bool(patch.get("enabled")))
+ enabled_val = bool(patch.get("enabled"))
+ updated = replace(updated, enabled=enabled_val)
+ # Re-enabling a job implies it is no longer expired, unless caller explicitly sets expired.
+ if enabled_val and "expired" not in patch:
+ updated = replace(updated, expired=False)
if "cron_expr" in patch:
updated = replace(updated, cron_expr=str(patch.get("cron_expr") or "").strip())
+ # Editing schedule implies it is no longer expired, unless caller explicitly sets expired.
+ if "expired" not in patch:
+ updated = replace(updated, expired=False)
if "timezone" in patch:
updated = replace(updated, timezone=str(patch.get("timezone") or "").strip())
if "wake_offset_seconds" in patch:
@@ -107,6 +114,8 @@ class CronJobStore:
updated = replace(updated, description=str(patch.get("description") or ""))
if "targets" in patch:
updated = replace(updated, targets=str(patch.get("targets") or "").strip())
+ if "expired" in patch:
+ updated = replace(updated, expired=bool(patch.get("expired")))
updated.updated_at = time.time()
CronJob.from_dict(updated.to_dict())
diff --git a/jiuwenclaw/web/src/components/CronPanel/index.tsx b/jiuwenclaw/web/src/components/CronPanel/index.tsx
index 9767dc82bd..666a215bf0 100644
--- a/jiuwenclaw/web/src/components/CronPanel/index.tsx
+++ b/jiuwenclaw/web/src/components/CronPanel/index.tsx
@@ -15,6 +15,7 @@ interface CronJob {
id: string;
name: string;
enabled: boolean;
+ expired?: boolean;
cron_expr: string;
timezone: string;
wake_offset_seconds: number;
@@ -733,8 +734,12 @@ export default function CronPanel({ sessionId }: CronPanelProps) {
{displayCron}
</td>
<td className="px-4 py-3">
- <span className={`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${job.enabled ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-700'}`}>
- {job.enabled ? t('cron.status.enabled') : t('cron.status.disabled')}
+ <span
+ className={`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${
+ job.expired ? 'bg-amber-100 text-amber-700' : job.enabled ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-700'
+ }`}
+ >
+ {job.expired ? t('cron.status.expired') : job.enabled ? t('cron.status.enabled') : t('cron.status.disabled')}
</span>
</td>
<td className="px-4 py-3 text-sm text-text-muted">
diff --git a/jiuwenclaw/web/src/i18n/locales/en.json b/jiuwenclaw/web/src/i18n/locales/en.json
index 21d977181e..8cdfb47d25 100644
--- a/jiuwenclaw/web/src/i18n/locales/en.json
+++ b/jiuwenclaw/web/src/i18n/locales/en.json
@@ -214,7 +214,8 @@
},
"status": {
"enabled": "Enabled",
- "disabled": "Disabled"
+ "disabled": "Disabled",
+ "expired": "Expired"
},
"actions": {
"enable": "Enable",
diff --git a/jiuwenclaw/web/src/i18n/locales/zh.json b/jiuwenclaw/web/src/i18n/locales/zh.json
index 416d2b2b4e..f1bec78f4f 100644
--- a/jiuwenclaw/web/src/i18n/locales/zh.json
+++ b/jiuwenclaw/web/src/i18n/locales/zh.json
@@ -400,7 +400,8 @@
"deleteConfirm": "确定要删除这个任务吗?",
"status": {
"enabled": "启用",
- "disabled": "禁用"
+ "disabled": "禁用",
+ "expired": "过期"
},
"columns": {
"name": "任务名称",