Files
admin b3ff11dd6d feat(sswflow): 实现 FlowSchedule 周期调度器(service+ticker+接口+前端页+单测)
- flowschedule.service.ts: 8 方法(listByFlow/setEnabled/listDue/previewNextFires/
  computeNextFire/triggerNow/fire/doFire),单条失败隔离,失败仍推进 nextFireTime
- flowschedule.ticker.ts: 30s 轮询(首 5s),到点自动 start run,与 Quartz 解耦
- flowschedule.controller.ts: 补 ListByFlow/SetEnabled/Preview/TriggerNow 四端点
- sswflow.module.ts: 注册 Ticker 为 provider
- schedule/spring-cron.ts: Spring 6 位 cron(秒 分 时 日 月 周)本地时区解析
- 前端 FlowSchedule.tsx: 定制页(启用停用/预览/立即触发/cron 录入)
- scripts/test-spring-cron.sh: 13 断言;sswflow-smoke.sh 增调度段 15 项
- MEMORY.md: 精简超限记忆,注明 FlowSchedule 已闭环
2026-11-11 17:35:43 +08:00

72 lines
3.5 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# spring-cron 单测 —— 把 spring-cron.ts 单独编译后跑断言(不依赖后端 / 不依赖 PGlite)。
#
# 覆盖:6 位校验、? 互斥、日周取「或」、每周/每天、严格大于 from、非法 cron 返回 null、preview 数量。
# 用法:bash scripts/test-spring-cron.sh
# 退出码:0 全过 / 1 有失败
set -u
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT/src/backend" || exit 2
TS="src/sswflow/schedule/spring-cron.ts"
OUT="$(mktemp -d)"
trap 'rm -rf "$OUT"' EXIT
if ! npx tsc "$TS" --outDir "$OUT" --module commonjs --target es2020 --skipLibCheck --moduleResolution node --esModuleInterop >/tmp/spring-cron-tsc.log 2>&1; then
echo "❌ 编译 spring-cron.ts 失败:"
cat /tmp/spring-cron-tsc.log
exit 1
fi
CJS_OUT="$OUT" node -e '
const m = require(process.env.CJS_OUT + "/spring-cron.js");
const { parseSpringCron, isValidSpringCron, nextSpringCron, previewSpringCron } = m;
let pass = 0, fail = 0;
function eq(n, g, w) {
if (JSON.stringify(g) === JSON.stringify(w)) pass++;
else { fail++; console.error("FAIL " + n + ": got " + JSON.stringify(g) + " want " + JSON.stringify(w)); }
}
function ok(n, c) { if (c) pass++; else { fail++; console.error("FAIL " + n); } }
// ★ spring-cron 按「本地时区」计算(与 Spring CronExpression 一致),断言用本地字段比较,避免 UTC 偏移。
function parts(d){ return d ? [d.getFullYear(), d.getMonth()+1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()] : null; }
function eqL(n, gotD, y,mo,d,h,mi,s){ eq(n, parts(gotD), [y,mo,d,h,mi,s]); }
// 1. 6 位校验
ok("合法 6 位", isValidSpringCron("0 0 2 * * ?"));
ok("非法 5 位被拒", !isValidSpringCron("0 0 2 * *"));
ok("非法 7 位被拒", !isValidSpringCron("0 0 2 * * ? *"));
// 2. 日与周 ? 互斥
ok("日周同时 ? 被拒", !isValidSpringCron("0 0 0 ? * ?"));
// 3. 每天 02:00:00(from 当地当天 03:00 → 次日 02:00)
eqL("每天 2am 下一次", nextSpringCron("0 0 2 * * ?", new Date(2026,0,1,3,0,0)), 2026,1,2,2,0,0);
// 4. 严格大于 from(from 恰好 02:00:00 → 跳到次日,不返回 from 本身)
eqL("严格大于 from", nextSpringCron("0 0 2 * * ?", new Date(2026,0,1,2,0,0)), 2026,1,2,2,0,0);
// 5. 日周取「或」:每月 1 号 或 周一 00:00:00(from=周四 00:00 → 下周一 01-05)
eqL("日周 OR", nextSpringCron("0 0 0 1 * MON", new Date(2026,0,1,0,0,0)), 2026,1,5,0,0,0);
// 6. 每周一 09:30:00(dom 用 ? 仅限定 dow;from=周四 → 下周一 01-05 09:30)
eqL("每周一 9:30", nextSpringCron("0 30 9 ? * MON", new Date(2026,0,1,0,0,0)), 2026,1,5,9,30,0);
// 7. 指定月份名:每年 3 月 1 日 00:00(from=当地 2026-01-01 → 2026-03-01)
eqL("月份名 MAR", nextSpringCron("0 0 0 1 MAR ?", new Date(2026,0,1,0,0,0)), 2026,3,1,0,0,0);
// 8. 非法 cron 不触发(返回 null,绝不退化成每分钟)
ok("非法 cron → null", nextSpringCron("bad cron expr", new Date()) === null);
// 9. preview 数量(与 TZ 无关)
eq("preview 3 次", previewSpringCron("0 0 2 * * ?", 3, new Date(2026,0,1,3,0,0)).length, 3);
ok("preview 非法 cron → []", previewSpringCron("0 0 2 * *", 3).length === 0);
// 10. preview 的未来时刻严格递增(均为本地 Date,单调递增)
const fs = previewSpringCron("0 0 2 * * ?", 4, new Date(2026,0,1,3,0,0));
ok("preview 单调递增", fs.every((d,i,a)=> i===0 || a[i-1].getTime() < d.getTime()));
console.log("spring-cron 单测:PASS=" + pass + " FAIL=" + fail);
process.exit(fail > 0 ? 1 : 0);
'