From 3c2b5bf3d40413e9aa89c6fb53dc4276542862ea Mon Sep 17 00:00:00 2001 From: ssw Date: Sat, 27 Jun 2026 03:49:21 +0800 Subject: [PATCH] feat(schedule-update): v0.11e+ dirty check + step machine + enhanceFromBackend (Phase A2+C1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- src/commands/schedule/update.ts | 73 ++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/src/commands/schedule/update.ts b/src/commands/schedule/update.ts index 7d98ae8..48d19eb 100644 --- a/src/commands/schedule/update.ts +++ b/src/commands/schedule/update.ts @@ -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') .option('--fullName ', '任务名') .option('--enCode ', '英文编码') .option('--cron ', 'cron 表达式 (如 "0 0 * * * ?")') + .option('--startTime ', 'v0.11e+ Phase C1: 开始时间 (ISO 格式)') + .option('--endTime ', 'v0.11e+ Phase C1: 结束时间 (ISO 格式, SYS131)') .option('--executeContent ', '执行内容 (Quartz class 全名 + 参数)') .option('--enabledMark <0|1>', '启用/禁用 (JNPF 无独立 pause/resume API, 用此替代)') + .option('--expected-enCode ', '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 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 = { ...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 }