feat(schedule-update): v0.11e+ dirty check + step machine + enhanceFromBackend (Phase A2+C1)

schedule/update.ts:
- import dirtyCheck + validateScheduleUpdate + enhanceFromBackend/formatErrorLine
- 加 --startTime/--endTime/--expected-enCode/--force 选项
- 跑 dirty check (A2 防 AI 误覆盖)
- 跑 step machine (C1 cron 格式 + SYS131 时间关系)
- catch 后端错时 enhanceFromBackend (A1)
This commit is contained in:
ssw committed 2026-06-27 03:49:21 +08:00
1 parent 7632aa50fa
commit 3c2b5bf3d4
1 file changed
+67 -6
+67 -6
View File
@@ -1,43 +1,95 @@
/**
* commands/schedule/update.ts — 定时任务修改
* commands/schedule/update.ts — 定时任务修改 (v0.11e+ Phase A2+C1 wire up)
*
* v0.11+ Phase 2: webapi 化 (loadSchedule.getInfo + update)
* 替代 v0.10 直接 c.get/c.putJson
*
* v0.11e+ Phase A2: dirty check + Phase C1: step machine + Phase A1: enhanceFromBackend
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadSchedule } from '../../webapi/schedule.ts'
import { dirtyCheck, dirtyCheckFailAndExit } from '../_dirty_check.ts'
import { validateScheduleUpdate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
interface Opts {
id: string
fullName?: string
enCode?: string
cron?: string
startTime?: string
endTime?: string
executeContent?: string
enabledMark?: string
expectedEnCode?: string
force?: boolean
json?: boolean
}
export function buildScheduleUpdateCommand(): Command {
const cmd = new Command('update')
.description('定时任务修改 (改 cron/executeContent/enabledMark, PUT 语义, v0.11+ webapi)')
.description('定时任务修改 (改 cron/executeContent/enabledMark, PUT 语义, v0.11+ webapi + v0.11e+ dirty check)')
.requiredOption('--id <id>', '任务 id')
.option('--fullName <n>', '任务名')
.option('--enCode <e>', '英文编码')
.option('--cron <c>', 'cron 表达式 (如 "0 0 * * * ?")')
.option('--startTime <iso>', 'v0.11e+ Phase C1: 开始时间 (ISO 格式)')
.option('--endTime <iso>', 'v0.11e+ Phase C1: 结束时间 (ISO 格式, SYS131)')
.option('--executeContent <c>', '执行内容 (Quartz class 全名 + 参数)')
.option('--enabledMark <0|1>', '启用/禁用 (JNPF 无独立 pause/resume API, 用此替代)')
.option('--expected-enCode <e>', 'v0.11e+ Phase A2: 校验 enCode 未变')
.option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false)
.option('--json', '原始 JSON 输出', false)
.action(async (opts: Opts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
const orig = (await loadSchedule.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
log.error(`schedule update 失败: 拉原任务 id=${opts.id} 失败 (id 不存在?)`)
const enhanced = enhanceFromBackend('拉原任务失败, id 不存在', 'id', { entity: 'schedule' })
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`)
process.exit(1)
}
// v0.11e+ Phase A2: dirty check
const dirtyResult = dirtyCheck({
expectedEnCode: opts.expectedEnCode,
force: opts.force,
current: {
enCode: orig.enCode as string | undefined,
state: orig.enabledMark as string | number | undefined,
fullName: orig.fullName as string | undefined,
type: 'di',
},
id: opts.id,
})
if (!dirtyResult.ok) {
dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error)
} else if (dirtyResult.skipped) {
log.info(`⚠ dirty check 已 --force 跳过`)
}
// v0.11e+ Phase C1: step machine 校验 (cron 格式 + SYS131)
const validation = validateScheduleUpdate({
fullName: opts.fullName || orig.fullName as string,
enCode: opts.enCode || orig.enCode as string,
cron: opts.cron || orig.cron as string,
startTime: opts.startTime,
endTime: opts.endTime,
enabledMark: opts.enabledMark,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
process.exit(1)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
@@ -47,7 +99,8 @@ export function buildScheduleUpdateCommand(): Command {
log.info(`loadSchedule.update (id=${opts.id})`)
const r = (await loadSchedule.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
log.error(`schedule update 失败 (code=${r.code}): ${r.msg}`)
const enhanced = enhanceFromBackend(r.msg || '', 'schedule', { entity: 'schedule' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
if (opts.json) printJson(r)
process.exit(1)
}
@@ -55,7 +108,7 @@ export function buildScheduleUpdateCommand(): Command {
if (opts.json) printJson(r.data)
})
installAiHelpHook(cmd, {
purpose: '定时任务修改 (改 cron/executeContent/enabledMark, v0.11+ webapi).',
purpose: '定时任务修改 (改 cron/executeContent/enabledMark, v0.11+ webapi + v0.11e+ dirty check + step machine)',
required: { '--id': '任务 id' },
optional: {
'--fullName': '任务名',
@@ -63,13 +116,21 @@ export function buildScheduleUpdateCommand(): Command {
'--cron': 'cron 表达式',
'--executeContent': '执行内容 (Quartz class + 参数)',
'--enabledMark': '启用/禁用 0|1 (替代 pause/resume)',
'--expected-enCode': '防 AI 误覆盖 (A2)',
'--force': '跳过 dirty check (A2)',
},
example: [
'dacli schedule update --id=123 --cron="0 0 2 * * ?"',
'dacli schedule update --id=123 --enabledMark=0 (禁用)',
],
source: { api: 'PUT /api/scheduletask/{id}', ref: 'web/.../api/system/task.ts update()' },
notes: ['v0.11+ 改用 webapi 镜像 (loadSchedule.update)', 'enabledMark 替代 pause/resume'],
notes: [
'v0.11+ 改用 webapi 镜像 (loadSchedule.update)',
'enabledMark 替代 pause/resume',
'v0.11e+ Phase A2: dirty check --expected-enCode (防 AI session 误覆盖)',
'v0.11e+ Phase C1: step machine cron 格式 + SYS131 时间关系',
'v0.11e+ Phase A1: catch enhanceFromBackend (错误码自动分类)',
],
})
return cmd
}