import { makeClient, initBiz, db } from "./common.ts"; import { sql } from "kysely"; await initBiz(); const { ow, backend, acquire, flaky, slow, burst } = await makeClient(); const [cmd, ...args] = process.argv.slice(2); const out = (x: unknown) => console.log(JSON.stringify(x, null, 0)); switch (cmd) { case "start-acquire": { const [downloadId, key, copyMs, retention] = args; const h = await acquire.run({ downloadId, copyMs: Number(copyMs ?? 0), retention: retention ?? "3s" }, key ? { idempotencyKey: key } : undefined); out({ runId: h.workflowRun.id, status: h.workflowRun.status, version: h.workflowRun.version }); break; } case "start-flaky": { const h = await flaky.run({ failTimes: Number(args[0]), maxAttempts: Number(args[1]) }); out({ runId: h.workflowRun.id }); break; } case "start-slow": { const h = await slow.run({}); out({ runId: h.workflowRun.id }); break; } case "start-burst": { const ids = []; for (let i = 0; i < Number(args[0]); i++) ids.push((await burst.run({ i })).workflowRun.id); out({ count: ids.length }); break; } case "signal": { const r = await ow.sendSignal({ signal: `download-done:${args[0]}`, data: { ok: true }, idempotencyKey: args[1] }); out(r); break; } case "cancel": { await ow.cancelWorkflowRun(args[0]); out({ canceled: args[0] }); break; } case "get": { const r = await backend.getWorkflowRun({ workflowRunId: args[0] }); out({ status: r?.status, version: r?.version, attempts: r?.attempts, error: r?.error, output: r?.output }); break; } case "steps": { const r = await backend.listStepAttempts({ workflowRunId: args[0], limit: 100 } as any); for (const s of [...r.data].sort((a,b)=>+a.createdAt-+b.createdAt)) console.log(s.stepName.padEnd(18), s.kind.padEnd(12), s.status, s.error ? JSON.stringify(s.error.message) : ""); break; } case "ledger": { const rows = await db.selectFrom("biz.side_effect_ledger").selectAll() .$if(!!args[0], (q) => q.where("run_id", "=", args[0])).orderBy("id").execute(); for (const r of rows) console.log(r.run_id.slice(0, 8), r.step.padEnd(16), r.worker.padEnd(6), r.note); break; } case "burst-dups": { const rows = await sql`SELECT count(*) total, count(DISTINCT run_id) runs, count(*) FILTER (WHERE worker='A') a, count(*) FILTER (WHERE worker='B') b FROM biz.side_effect_ledger WHERE step='work'`.execute(db); out(rows.rows[0]); break; } case "tables": { const r = await sql`SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema IN ('openworkflow','biz') ORDER BY 1,2`.execute(db); for (const t of r.rows) console.log(`${t.table_schema}.${t.table_name}`); break; } } await backend.stop(); await db.destroy();