mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 02:02:01 +00:00
定时任务小修:数字按 isdecimal 认,输入生成失败的原因不再说成模板写错
- 「²」这类字符 isdigit 为真、int() 却认不得:写进 cron 或每页条数时会冒出英文的 ValueError(被全局处理器 回成 400)。改按 isdecimal 认,照常回中文的 422 - 库里的 cron 或时区后来坏了时,立即运行记下的原因以前写「输入模板无法渲染」,其实模板没错:改成照实说 「无法生成这次运行的输入」,后面跟是哪一项
This commit is contained in:
@@ -494,7 +494,7 @@ async def list_fires(schedule_id: str, limit: str | None = None, before: str | N
|
||||
await _get(session, schedule_id)
|
||||
size = 20
|
||||
if limit is not None:
|
||||
if not limit.strip().isdigit() or not 1 <= int(limit) <= 100:
|
||||
if not limit.strip().isdecimal() or not 1 <= int(limit) <= 100:
|
||||
raise _invalid(f"每页条数需要是 1 到 100 之间的整数,当前为「{limit}」", "limit")
|
||||
size = int(limit)
|
||||
stmt = select(ScheduleFire).where(ScheduleFire.schedule_id == schedule_id)
|
||||
|
||||
@@ -128,7 +128,8 @@ def _parse_item(item: str, key: str, label: str, low: int, high: int) -> set[int
|
||||
base, slash, step_text = item.partition("/")
|
||||
step = 1
|
||||
if slash:
|
||||
if not step_text.isdigit() or int(step_text) == 0:
|
||||
# isdecimal 而不是 isdigit:「²」算 digit,int() 却认不得它
|
||||
if not step_text.isdecimal() or int(step_text) == 0:
|
||||
raise CronError(f"「{label}」字段「{item}」的步长需要是正整数,现在是「{step_text}」", key)
|
||||
step = int(step_text)
|
||||
if base == "*":
|
||||
@@ -150,7 +151,7 @@ def _parse_item(item: str, key: str, label: str, low: int, high: int) -> set[int
|
||||
|
||||
def _value(token: str, item: str, key: str, label: str, low: int, high: int, *, range_end: bool = False) -> int:
|
||||
word = token.strip().upper()
|
||||
if word.isdigit():
|
||||
if word.isdecimal():
|
||||
number = int(word)
|
||||
if not low <= number <= high:
|
||||
raise CronError(f"「{label}」字段的 {number} 超出范围,允许 {_ALLOWED[key]}", key)
|
||||
|
||||
@@ -240,7 +240,8 @@ async def fire_once(schedule_id: str, fire_at: datetime, kind: str, *, actor: st
|
||||
try:
|
||||
inputs = render_inputs(check_template(template), template_values(fire_at, tz, cron_expr))
|
||||
except (TemplateError, cron.CronError, cron.TimezoneError) as e:
|
||||
return await _record(schedule_id, fire_at, kind, "failed_to_start", f"输入模板无法渲染:{e}", {})
|
||||
# 存进库之后才坏掉的(手改过库、时区库升级删了时区):照实说是哪一项,不发起
|
||||
return await _record(schedule_id, fire_at, kind, "failed_to_start", f"无法生成这次运行的输入:{e}", {})
|
||||
busy = await _active_run(schedule_id)
|
||||
if busy is not None:
|
||||
reason = f"上一次运行({busy[:8]})还没有结束,按设置跳过这次触发"
|
||||
|
||||
@@ -84,6 +84,8 @@ def test_expression_is_normalized():
|
||||
("0 9 * * L", ["「周」", "L"]),
|
||||
("0 9 1,,2 * *", ["「日」"]),
|
||||
("@daily", ["5 个字段"]),
|
||||
("² * * * *", ["「分」", "²"]),
|
||||
("*/² * * * *", ["「分」", "步长"]),
|
||||
])
|
||||
def test_invalid_expressions_explain_in_chinese(expr, fragments):
|
||||
with pytest.raises(cron.CronError) as caught:
|
||||
|
||||
@@ -436,6 +436,8 @@ async def test_fire_history_pages_and_last_fire(client):
|
||||
assert last["reason"] == "第 4 次" and set(last) == FIRE_KEYS
|
||||
bad = await client.get(f"/api/schedules/{sid}/fires", params={"limit": "many"})
|
||||
assert bad.status_code == 422 and bad.json()["field"] == "limit"
|
||||
bad = await client.get(f"/api/schedules/{sid}/fires", params={"limit": "²"})
|
||||
assert bad.status_code == 422 and bad.json()["field"] == "limit"
|
||||
bad = await client.get(f"/api/schedules/{sid}/fires", params={"before": "昨天"})
|
||||
assert bad.status_code == 422 and bad.json()["field"] == "before"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user