Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,278 @@
|
||||
/**
|
||||
* Shared helpers for the `outbox` CLI subtree.
|
||||
*
|
||||
* Both `requeue --event-id` and `requeue --type --since` flow through
|
||||
* the runners defined here so the citty command stays a thin wrapper
|
||||
* with full unit-test coverage on the business logic.
|
||||
*
|
||||
* The terminology mirrors the Prisma OutboxStatus enum (PENDING,
|
||||
* PROCESSING, DONE, FAILED, CANCELLED).
|
||||
*/
|
||||
|
||||
export type OutboxRequeueStatus = "PENDING" | "PROCESSING" | "DONE" | "FAILED" | "CANCELLED";
|
||||
|
||||
/**
|
||||
* Refusable statuses — we never reset an event that's currently being
|
||||
* worked on by the dispatcher (PROCESSING) or that's still waiting in
|
||||
* the queue (PENDING). Doing so would double-deliver the work.
|
||||
*/
|
||||
export const OUTBOX_REQUEUE_REFUSED_STATUSES: ReadonlySet<OutboxRequeueStatus> = new Set(["PENDING", "PROCESSING"]);
|
||||
|
||||
/**
|
||||
* Acceptable statuses for re-queue. DONE is included because the
|
||||
* operator may want to replay a side-effect (e.g. resend a billing
|
||||
* notification email that was lost in the receiver). The CLI emits a
|
||||
* warning for DONE so it's never an accident.
|
||||
*/
|
||||
export const OUTBOX_REQUEUE_ALLOWED_STATUSES: ReadonlySet<OutboxRequeueStatus> = new Set(["FAILED", "DONE", "CANCELLED"]);
|
||||
|
||||
export interface OutboxRequeueEventRow {
|
||||
id: string;
|
||||
eventType: string;
|
||||
status: OutboxRequeueStatus;
|
||||
tenantId: string | null;
|
||||
retries: number;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Narrow interface against `@toolctrl/db` — just the methods the
|
||||
* runner uses. Tests inject an in-memory fake.
|
||||
*/
|
||||
export interface OutboxRequeueClient {
|
||||
outboxEvent: {
|
||||
findUnique(args: { where: { id: string } }): Promise<OutboxRequeueEventRow | null>;
|
||||
update(args: {
|
||||
where: { id: string };
|
||||
data: {
|
||||
status: OutboxRequeueStatus;
|
||||
retries: number;
|
||||
lastError: string | null;
|
||||
scheduledAt: Date;
|
||||
lockedUntil: null;
|
||||
processedAt: null;
|
||||
};
|
||||
}): Promise<OutboxRequeueEventRow>;
|
||||
findMany(args: {
|
||||
where: {
|
||||
eventType: string;
|
||||
createdAt: { gte: Date };
|
||||
status: { in: OutboxRequeueStatus[] };
|
||||
};
|
||||
select: { id: true; eventType: true; status: true; tenantId: true; retries: true; createdAt: true };
|
||||
orderBy: { createdAt: "asc" };
|
||||
take: number;
|
||||
}): Promise<OutboxRequeueEventRow[]>;
|
||||
updateMany(args: {
|
||||
where: { id: { in: string[] } };
|
||||
data: {
|
||||
status: OutboxRequeueStatus;
|
||||
retries: number;
|
||||
lastError: string | null;
|
||||
scheduledAt: Date;
|
||||
lockedUntil: null;
|
||||
processedAt: null;
|
||||
};
|
||||
}): Promise<{ count: number }>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OutboxRequeueOutput {
|
||||
log(message: string): void;
|
||||
warn(message: string): void;
|
||||
error(message: string): void;
|
||||
json(payload: unknown): void;
|
||||
}
|
||||
|
||||
export interface OutboxRequeueByIdArgs {
|
||||
eventId: string;
|
||||
json?: boolean;
|
||||
now?: Date;
|
||||
}
|
||||
|
||||
export type OutboxRequeueByIdOutcome =
|
||||
| { exitCode: 0; status: "requeued"; eventId: string; previousStatus: OutboxRequeueStatus }
|
||||
| { exitCode: 1; status: "not_found" | "refused"; eventId: string; previousStatus?: OutboxRequeueStatus; message?: string };
|
||||
|
||||
function isStatus(value: unknown): value is OutboxRequeueStatus {
|
||||
return typeof value === "string" && ["PENDING", "PROCESSING", "DONE", "FAILED", "CANCELLED"].includes(value);
|
||||
}
|
||||
|
||||
export function normalizeStatus(value: unknown): OutboxRequeueStatus {
|
||||
if (isStatus(value)) return value;
|
||||
throw new Error(`Unrecognised OutboxStatus value ${JSON.stringify(value)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-queue a single event by id. Refuses to act on events that are
|
||||
* currently PENDING or PROCESSING — re-queueing them would double-
|
||||
* deliver the work because the dispatcher still has the original copy
|
||||
* in its pipeline.
|
||||
*/
|
||||
export async function runOutboxRequeueById(
|
||||
args: OutboxRequeueByIdArgs,
|
||||
deps: { client: OutboxRequeueClient; output: OutboxRequeueOutput }
|
||||
): Promise<OutboxRequeueByIdOutcome> {
|
||||
const now = args.now ?? new Date();
|
||||
const existing = await deps.client.outboxEvent.findUnique({ where: { id: args.eventId } });
|
||||
if (!existing) {
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: false, reason: "not_found", eventId: args.eventId });
|
||||
} else {
|
||||
deps.output.error(`outbox event ${args.eventId} not found`);
|
||||
}
|
||||
return { exitCode: 1, status: "not_found", eventId: args.eventId };
|
||||
}
|
||||
const status = normalizeStatus(existing.status);
|
||||
if (OUTBOX_REQUEUE_REFUSED_STATUSES.has(status)) {
|
||||
const message = `outbox event ${args.eventId} is currently ${status}; refuse to re-queue (would double-deliver)`;
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: false, reason: "refused", eventId: args.eventId, status });
|
||||
} else {
|
||||
deps.output.error(message);
|
||||
}
|
||||
return { exitCode: 1, status: "refused", eventId: args.eventId, previousStatus: status, message };
|
||||
}
|
||||
if (!OUTBOX_REQUEUE_ALLOWED_STATUSES.has(status)) {
|
||||
const message = `outbox event ${args.eventId} has unsupported status ${status}`;
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: false, reason: "refused", eventId: args.eventId, status });
|
||||
} else {
|
||||
deps.output.error(message);
|
||||
}
|
||||
return { exitCode: 1, status: "refused", eventId: args.eventId, previousStatus: status, message };
|
||||
}
|
||||
|
||||
if (status === "DONE") {
|
||||
deps.output.warn(`outbox event ${args.eventId} is DONE; re-queueing will replay the side effect`);
|
||||
}
|
||||
|
||||
await deps.client.outboxEvent.update({
|
||||
where: { id: args.eventId },
|
||||
data: {
|
||||
status: "PENDING",
|
||||
retries: 0,
|
||||
lastError: null,
|
||||
scheduledAt: now,
|
||||
lockedUntil: null,
|
||||
processedAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: true, eventId: args.eventId, previousStatus: status });
|
||||
} else {
|
||||
deps.output.log(`outbox event ${args.eventId} re-queued (was ${status})`);
|
||||
}
|
||||
return { exitCode: 0, status: "requeued", eventId: args.eventId, previousStatus: status };
|
||||
}
|
||||
|
||||
export interface OutboxRequeueBulkArgs {
|
||||
type: string;
|
||||
since: string;
|
||||
/** Optional cap to avoid runaway re-queues. Default 10_000. */
|
||||
limit?: number;
|
||||
/** `true` ⇒ confirm, perform the writes; `false` ⇒ dry-run summary only. */
|
||||
apply?: boolean;
|
||||
/** Include DONE events too. Off by default for safety. */
|
||||
includeDone?: boolean;
|
||||
json?: boolean;
|
||||
now?: Date;
|
||||
}
|
||||
|
||||
export type OutboxRequeueBulkOutcome = {
|
||||
exitCode: 0 | 1;
|
||||
status: "previewed" | "requeued" | "invalid_args";
|
||||
matched: number;
|
||||
applied: number;
|
||||
eventType: string;
|
||||
since: Date | null;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
const OUTBOX_REQUEUE_BULK_DEFAULT_LIMIT = 10_000;
|
||||
const OUTBOX_EVENT_TYPE_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*){2,}$/u;
|
||||
|
||||
export async function runOutboxRequeueBulk(
|
||||
args: OutboxRequeueBulkArgs,
|
||||
deps: { client: OutboxRequeueClient; output: OutboxRequeueOutput }
|
||||
): Promise<OutboxRequeueBulkOutcome> {
|
||||
const limit = Math.max(1, Math.min(args.limit ?? OUTBOX_REQUEUE_BULK_DEFAULT_LIMIT, 100_000));
|
||||
if (!OUTBOX_EVENT_TYPE_PATTERN.test(args.type)) {
|
||||
const message = `--type must match <domain>.<entity>.<verb> (got ${JSON.stringify(args.type)})`;
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: false, reason: "invalid_args", message });
|
||||
} else {
|
||||
deps.output.error(message);
|
||||
}
|
||||
return { exitCode: 1, status: "invalid_args", matched: 0, applied: 0, eventType: args.type, since: null, message };
|
||||
}
|
||||
const since = new Date(args.since);
|
||||
if (Number.isNaN(since.getTime())) {
|
||||
const message = `--since must be a valid ISO-8601 timestamp (got ${JSON.stringify(args.since)})`;
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: false, reason: "invalid_args", message });
|
||||
} else {
|
||||
deps.output.error(message);
|
||||
}
|
||||
return { exitCode: 1, status: "invalid_args", matched: 0, applied: 0, eventType: args.type, since: null, message };
|
||||
}
|
||||
|
||||
const statusFilter: OutboxRequeueStatus[] = args.includeDone ? ["FAILED", "DONE", "CANCELLED"] : ["FAILED", "CANCELLED"];
|
||||
const candidates = await deps.client.outboxEvent.findMany({
|
||||
where: {
|
||||
eventType: args.type,
|
||||
createdAt: { gte: since },
|
||||
status: { in: statusFilter },
|
||||
},
|
||||
select: { id: true, eventType: true, status: true, tenantId: true, retries: true, createdAt: true },
|
||||
orderBy: { createdAt: "asc" },
|
||||
take: limit,
|
||||
});
|
||||
|
||||
if (!args.apply) {
|
||||
if (args.json) {
|
||||
deps.output.json({
|
||||
ok: true,
|
||||
applied: 0,
|
||||
matched: candidates.length,
|
||||
eventType: args.type,
|
||||
since: since.toISOString(),
|
||||
preview: candidates.slice(0, 10).map((c) => ({ id: c.id, status: c.status, tenantId: c.tenantId })),
|
||||
});
|
||||
} else {
|
||||
deps.output.log(
|
||||
`outbox requeue (dry-run): ${candidates.length} event(s) matched type=${args.type} since=${since.toISOString()}; pass --apply to perform the re-queue`
|
||||
);
|
||||
}
|
||||
return { exitCode: 0, status: "previewed", matched: candidates.length, applied: 0, eventType: args.type, since };
|
||||
}
|
||||
|
||||
if (candidates.length === 0) {
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: true, applied: 0, matched: 0, eventType: args.type, since: since.toISOString() });
|
||||
} else {
|
||||
deps.output.log(`outbox requeue: no events matched type=${args.type} since=${since.toISOString()}`);
|
||||
}
|
||||
return { exitCode: 0, status: "requeued", matched: 0, applied: 0, eventType: args.type, since };
|
||||
}
|
||||
|
||||
const now = args.now ?? new Date();
|
||||
const result = await deps.client.outboxEvent.updateMany({
|
||||
where: { id: { in: candidates.map((c) => c.id) } },
|
||||
data: {
|
||||
status: "PENDING",
|
||||
retries: 0,
|
||||
lastError: null,
|
||||
scheduledAt: now,
|
||||
lockedUntil: null,
|
||||
processedAt: null,
|
||||
},
|
||||
});
|
||||
if (args.json) {
|
||||
deps.output.json({ ok: true, applied: result.count, matched: candidates.length, eventType: args.type, since: since.toISOString() });
|
||||
} else {
|
||||
deps.output.log(`outbox requeue: ${result.count} event(s) re-queued (type=${args.type} since=${since.toISOString()})`);
|
||||
}
|
||||
return { exitCode: 0, status: "requeued", matched: candidates.length, applied: result.count, eventType: args.type, since };
|
||||
}
|
||||
Reference in new issue
Block a user