Skip to content

Commit 3dc5e3c

Browse files
committed
fix: 定时任务创建失败时返回错误信息而非静默处理
1 parent 533a0bd commit 3dc5e3c

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

astrbot/core/cron/manager.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
from astrbot.core.star.context import Context
2323

2424

25+
class CronJobSchedulingError(Exception):
26+
"""Raised when a cron job fails to be scheduled."""
27+
pass
28+
29+
2530
class CronJobManager:
2631
"""Central scheduler for BasicCronJob and ActiveAgentCronJob."""
2732

@@ -59,7 +64,10 @@ async def sync_from_db(self) -> None:
5964
job.job_id,
6065
)
6166
continue
62-
self._schedule_job(job)
67+
try:
68+
self._schedule_job(job)
69+
except CronJobSchedulingError:
70+
continue # Error already logged in _schedule_job
6371

6472
async def add_basic_job(
6573
self,
@@ -181,8 +189,9 @@ def _schedule_job(self, job: CronJob) -> None:
181189
job.job_id, next_run_time=self._get_next_run_time(job.job_id)
182190
)
183191
)
184-
except Exception as e:
185-
logger.error(f"Failed to schedule cron job {job.job_id}: {e!s}")
192+
except (ValueError, TypeError) as e:
193+
logger.exception("Failed to schedule cron job %s", job.job_id)
194+
raise CronJobSchedulingError(str(e)) from e
186195

187196
def _get_next_run_time(self, job_id: str):
188197
aps_job = self.scheduler.get_job(job_id)

astrbot/core/tools/cron_tools.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from astrbot.core.agent.run_context import ContextWrapper
88
from astrbot.core.agent.tool import FunctionTool, ToolExecResult
99
from astrbot.core.astr_agent_context import AstrAgentContext
10+
from astrbot.core.cron.manager import CronJobSchedulingError
1011
from astrbot.core.tools.registry import builtin_tool
1112

1213
_CRON_TOOL_CONFIG = {
@@ -112,14 +113,17 @@ async def call(
112113
"origin": "tool",
113114
}
114115

115-
job = await cron_mgr.add_active_job(
116-
name=name,
117-
cron_expression=str(cron_expression) if cron_expression else None,
118-
payload=payload,
119-
description=note,
120-
run_once=run_once,
121-
run_at=run_at_dt,
122-
)
116+
try:
117+
job = await cron_mgr.add_active_job(
118+
name=name,
119+
cron_expression=str(cron_expression) if cron_expression else None,
120+
payload=payload,
121+
description=note,
122+
run_once=run_once,
123+
run_at=run_at_dt,
124+
)
125+
except CronJobSchedulingError:
126+
return "error: failed to schedule task due to invalid configuration."
123127
next_run = job.next_run_time or run_at_dt
124128
suffix = (
125129
f"one-time at {next_run}"
@@ -195,7 +199,10 @@ async def call(
195199
updates["cron_expression"] = cron_expression
196200
updates["payload"] = payload
197201

198-
job = await cron_mgr.update_job(str(job_id), **updates)
202+
try:
203+
job = await cron_mgr.update_job(str(job_id), **updates)
204+
except CronJobSchedulingError:
205+
return "error: failed to update task due to invalid configuration."
199206
if not job:
200207
return f"error: cron job {job_id} not found."
201208
return f"Updated future task {job.job_id} ({job.name})."

0 commit comments

Comments
 (0)