- root deno.json workspace (nodeModulesDir auto), npm versions registered once in imports, deno.lock; remove pnpm-lock/pnpm-workspace/root package.json; members keep minimal package.json (name/type/exports) - tasks: dev, typecheck (tsc 7.0.2 per member + deno check), lint/fmt (biome, useImportExtensions), test (vitest via deno), check:tokens, build (web + deno compile), ci, db:migrate/reset, gen:types - tsconfig base moved to config/ (no root tsconfig.json); deno types -> types/deno.d.ts; web drops baseUrl - server: Deno.serve entry with graceful shutdown, --role migrate|preflight|probe, WEB_ROOT static SPA hosting; drop @hono/node-server, tsx, esbuild build.mjs - logger: redact child() bindings and serialize err by message/stack; sync stdout destination; tests - relative imports get explicit extensions (sites/mp-sync paths left for follow-up) - crash-recovery tests spawn `deno run -A`; tasks tests use unique per-file schemas - single-container Dockerfile + entrypoint (migrate -> preflight -> api+worker watchdog), docker-compose.yml - docs: README, runbook, ADR-001 superseded, plan §1 validation row Co-Authored-By: Claude Fable 5.1 <[email protected]>
87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import { sql } from "kysely";
|
|
import { z } from "zod";
|
|
import { defineTask, TaskRegistry } from "../../src/index.ts";
|
|
import type { TestDb } from "../helpers.ts";
|
|
|
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
|
|
/** 测试任务;副作用写 side_effect 表,用来判断步骤是否重跑 */
|
|
export function createTestRegistry(db: TestDb) {
|
|
const mark = (runId: string, step: string, note: string | null = null) =>
|
|
sql`INSERT INTO side_effect (run_id, step, note, pid) VALUES (${runId}, ${step}, ${note}, ${process.pid})`.execute(
|
|
db,
|
|
);
|
|
|
|
const threeSteps = defineTask({
|
|
name: "threeSteps",
|
|
version: 1,
|
|
inputSchema: z.object({ label: z.string() }),
|
|
handler: async (ctx) => {
|
|
const a = await ctx.step.run("step1", async () => {
|
|
await mark(ctx.runId, "step1");
|
|
return 1;
|
|
});
|
|
const b = await ctx.step.run("step2", async () => {
|
|
await mark(ctx.runId, "step2");
|
|
return 2;
|
|
});
|
|
const c = await ctx.step.run("step3", async () => {
|
|
await mark(ctx.runId, "step3", "start");
|
|
// 崩溃测试:第一个 worker 卡在这里被 kill -9
|
|
if (process.env.HANG_STEP3 === "1") await sleep(10 * 60_000);
|
|
await mark(ctx.runId, "step3", "end");
|
|
return 3;
|
|
});
|
|
return { label: ctx.input.label, sum: a + b + c };
|
|
},
|
|
});
|
|
|
|
const cancellable = defineTask({
|
|
name: "cancellable",
|
|
version: 1,
|
|
inputSchema: z.object({}),
|
|
handler: async (ctx) => {
|
|
await ctx.step.run("first", () => mark(ctx.runId, "first").then(() => 1));
|
|
await ctx.step.run("long", async () => {
|
|
await mark(ctx.runId, "long", "start");
|
|
for (let i = 0; i < 300; i++) {
|
|
if (ctx.abortSignal.aborted) {
|
|
await mark(ctx.runId, "long", "aborted");
|
|
throw ctx.abortSignal.reason;
|
|
}
|
|
await sleep(100);
|
|
}
|
|
return 1;
|
|
});
|
|
await ctx.step.run("after", () => mark(ctx.runId, "after").then(() => 1));
|
|
return "done";
|
|
},
|
|
});
|
|
|
|
const waitFacts = defineTask({
|
|
name: "waitFacts",
|
|
version: 1,
|
|
inputSchema: z.object({}),
|
|
handler: async (ctx) => {
|
|
await ctx.step.run("before", async () => {
|
|
await mark(ctx.runId, "before");
|
|
await sleep(1500);
|
|
return 1;
|
|
});
|
|
const a = await ctx.step.waitForFact("a", { timeout: "60s" });
|
|
await ctx.step.run("got-a", () => mark(ctx.runId, "got-a").then(() => 1));
|
|
const b = await ctx.step.waitForFact("b", { timeout: "60s" });
|
|
return { a, b };
|
|
},
|
|
});
|
|
|
|
const echo = defineTask({
|
|
name: "echo",
|
|
version: 1,
|
|
inputSchema: z.object({ n: z.number() }),
|
|
handler: async (ctx) => ctx.step.run("echo", async () => ({ n: ctx.input.n, trace: ctx.traceId })),
|
|
});
|
|
|
|
return new TaskRegistry().register(threeSteps, cancellable, waitFacts, echo);
|
|
}
|