/** * `toolctrl-cli outbox requeue` — re-queue failed (or dead-letter) outbox * events back into the dispatcher's pending pool. * * Two modes: * - `--event-id `: single-event re-queue. Refuses to act on * events whose current status is PENDING or PROCESSING (those are * still in flight; resetting them double-delivers). * - `--type --since `: bulk re-queue. By * default this is a dry-run (count + small preview); pass `--apply` * to perform the writes. * * Output: human summary by default, JSON when `--json` is set. The CLI * uses `@toolctrl/db` directly so the worker process does not need to * be reachable. */ import { defineCommand } from "citty"; import consola from "consola"; import { runOutboxRequeueById, runOutboxRequeueBulk, type OutboxRequeueClient, type OutboxRequeueOutput, } from "./_lib.js"; function makeOutput(json: boolean): OutboxRequeueOutput { return { log: (msg) => (json ? undefined : consola.success(msg)), warn: (msg) => consola.warn(msg), error: (msg) => (json ? undefined : consola.error(msg)), json: (payload) => consola.log(JSON.stringify(payload)), }; } export const outboxRequeueCommand = defineCommand({ meta: { name: "requeue", description: "Re-queue a single FAILED/DONE outbox event by id, or bulk-re-queue by eventType + since timestamp.", }, args: { "event-id": { type: "string", required: false, description: "Single-event mode: outbox event id to re-queue.", }, type: { type: "string", required: false, description: "Bulk mode: event type to re-queue (must include all three segments, e.g. 'billing.subscription.ended').", }, since: { type: "string", required: false, description: "Bulk mode: ISO-8601 lower bound on createdAt.", }, apply: { type: "boolean", required: false, default: false, description: "Bulk mode only: perform the write (default is a dry-run preview).", }, "include-done": { type: "boolean", required: false, default: false, description: "Bulk mode: include DONE events too (default is FAILED + CANCELLED only).", }, limit: { type: "string", required: false, description: "Bulk mode: cap on rows considered (default 10000).", }, json: { type: "boolean", required: false, default: false, description: "Emit JSON instead of human-readable output.", }, }, async run({ args }) { const json = Boolean(args.json); const eventId = typeof args["event-id"] === "string" && args["event-id"].length > 0 ? args["event-id"] : null; const typeArg = typeof args.type === "string" && args.type.length > 0 ? args.type : null; const sinceArg = typeof args.since === "string" && args.since.length > 0 ? args.since : null; if (eventId && (typeArg || sinceArg)) { consola.error("Pass --event-id OR (--type + --since), not both."); process.exitCode = 1; return; } if (!eventId && (!typeArg || !sinceArg)) { consola.error("Either --event-id or (--type + --since) is required."); process.exitCode = 1; return; } const { getDbClient } = await import("@toolctrl/db/client.server"); const prisma = getDbClient() as unknown as OutboxRequeueClient; const output = makeOutput(json); try { if (eventId) { const outcome = await runOutboxRequeueById( { eventId, json }, { client: prisma, output } ); process.exitCode = outcome.exitCode; return; } const limit = typeof args.limit === "string" && args.limit.length > 0 ? Number.parseInt(args.limit, 10) : undefined; const outcome = await runOutboxRequeueBulk( { type: typeArg!, since: sinceArg!, apply: Boolean(args.apply), includeDone: Boolean(args["include-done"]), ...(typeof limit === "number" && Number.isFinite(limit) ? { limit } : {}), json, }, { client: prisma, output } ); process.exitCode = outcome.exitCode; } catch (error) { const message = error instanceof Error ? error.message : String(error); if (json) { output.json({ ok: false, reason: "exception", message }); } else { consola.error(`outbox requeue failed: ${message}`); } process.exitCode = 1; } }, });