Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -16,15 +16,13 @@ export type OutboxRequeueStatus = "PENDING" | "PROCESSING" | "DONE" | "FAILED" |
* 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"]);
export const OUTBOX_REQUEUE_REFUSED_STATUSES: ReadonlySet<OutboxRequeueStatus> = new Set(["PENDING", "PROCESSING", "DONE"]);
/**
* 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.
* Acceptable statuses for re-queue. DONE only proves enqueue, not consumer
* completion, so replaying it could overlap an active delivery.
*/
export const OUTBOX_REQUEUE_ALLOWED_STATUSES: ReadonlySet<OutboxRequeueStatus> = new Set(["FAILED", "DONE", "CANCELLED"]);
export const OUTBOX_REQUEUE_ALLOWED_STATUSES: ReadonlySet<OutboxRequeueStatus> = new Set(["FAILED", "CANCELLED"]);
export interface OutboxRequeueEventRow {
id: string;
@@ -32,6 +30,7 @@ export interface OutboxRequeueEventRow {
status: OutboxRequeueStatus;
tenantId: string | null;
retries: number;
deliveryGeneration: number;
createdAt: Date;
}
@@ -41,12 +40,13 @@ export interface OutboxRequeueEventRow {
*/
export interface OutboxRequeueClient {
outboxEvent: {
findUnique(args: { where: { id: string } }): Promise<OutboxRequeueEventRow | null>;
findFirst(args: { where: { id: string; tenantId: string } }): Promise<OutboxRequeueEventRow | null>;
update(args: {
where: { id: string };
data: {
status: OutboxRequeueStatus;
retries: number;
deliveryGeneration: { increment: number };
lastError: string | null;
scheduledAt: Date;
lockedUntil: null;
@@ -56,18 +56,25 @@ export interface OutboxRequeueClient {
findMany(args: {
where: {
eventType: string;
tenantId: string;
createdAt: { gte: Date };
status: { in: OutboxRequeueStatus[] };
};
select: { id: true; eventType: true; status: true; tenantId: true; retries: true; createdAt: true };
select: { id: true; eventType: true; status: true; tenantId: true; retries: true; deliveryGeneration: true; createdAt: true };
orderBy: { createdAt: "asc" };
take: number;
}): Promise<OutboxRequeueEventRow[]>;
updateMany(args: {
where: { id: { in: string[] } };
where: {
id: { in: string[] };
tenantId: string;
status?: { in: OutboxRequeueStatus[] };
deliveryGeneration?: number;
};
data: {
status: OutboxRequeueStatus;
retries: number;
deliveryGeneration: { increment: number };
lastError: string | null;
scheduledAt: Date;
lockedUntil: null;
@@ -85,6 +92,7 @@ export interface OutboxRequeueOutput {
}
export interface OutboxRequeueByIdArgs {
tenantId: string;
eventId: string;
json?: boolean;
now?: Date;
@@ -103,6 +111,14 @@ export function normalizeStatus(value: unknown): OutboxRequeueStatus {
throw new Error(`Unrecognised OutboxStatus value ${JSON.stringify(value)}`);
}
export function normalizeTenantId(value: unknown): string {
const normalized = typeof value === "string" ? value.trim() : "";
if (normalized.length === 0 || normalized.length > 191 || normalized.includes("\u0000")) {
throw new Error("--tenant must be a non-empty database identifier");
}
return normalized;
}
/**
* Re-queue a single event by id. Refuses to act on events that are
* currently PENDING or PROCESSING — re-queueing them would double-
@@ -113,8 +129,9 @@ export async function runOutboxRequeueById(
args: OutboxRequeueByIdArgs,
deps: { client: OutboxRequeueClient; output: OutboxRequeueOutput }
): Promise<OutboxRequeueByIdOutcome> {
const tenantId = normalizeTenantId(args.tenantId);
const now = args.now ?? new Date();
const existing = await deps.client.outboxEvent.findUnique({ where: { id: args.eventId } });
const existing = await deps.client.outboxEvent.findFirst({ where: { id: args.eventId, tenantId } });
if (!existing) {
if (args.json) {
deps.output.json({ ok: false, reason: "not_found", eventId: args.eventId });
@@ -143,21 +160,32 @@ export async function runOutboxRequeueById(
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 },
const requeued = await deps.client.outboxEvent.updateMany({
where: {
id: { in: [args.eventId] },
tenantId,
status: { in: [status] },
deliveryGeneration: existing.deliveryGeneration,
},
data: {
status: "PENDING",
retries: 0,
deliveryGeneration: { increment: 1 },
lastError: null,
scheduledAt: now,
lockedUntil: null,
processedAt: null,
},
});
if (requeued.count !== 1) {
const message = `outbox event ${args.eventId} changed while it was being re-queued; retry from fresh state`;
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 (args.json) {
deps.output.json({ ok: true, eventId: args.eventId, previousStatus: status });
@@ -168,13 +196,14 @@ export async function runOutboxRequeueById(
}
export interface OutboxRequeueBulkArgs {
tenantId: string;
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. */
/** Legacy flag. DONE replay is refused until consumer completion receipts exist. */
includeDone?: boolean;
json?: boolean;
now?: Date;
@@ -197,6 +226,7 @@ export async function runOutboxRequeueBulk(
args: OutboxRequeueBulkArgs,
deps: { client: OutboxRequeueClient; output: OutboxRequeueOutput }
): Promise<OutboxRequeueBulkOutcome> {
const tenantId = normalizeTenantId(args.tenantId);
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)})`;
@@ -207,6 +237,12 @@ export async function runOutboxRequeueBulk(
}
return { exitCode: 1, status: "invalid_args", matched: 0, applied: 0, eventType: args.type, since: null, message };
}
if (args.includeDone) {
const message = "--include-done is unsafe without a consumer completion receipt and is no longer supported";
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)})`;
@@ -218,14 +254,15 @@ export async function runOutboxRequeueBulk(
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 statusFilter: OutboxRequeueStatus[] = ["FAILED", "CANCELLED"];
const candidates = await deps.client.outboxEvent.findMany({
where: {
eventType: args.type,
tenantId,
createdAt: { gte: since },
status: { in: statusFilter },
},
select: { id: true, eventType: true, status: true, tenantId: true, retries: true, createdAt: true },
select: { id: true, eventType: true, status: true, tenantId: true, retries: true, deliveryGeneration: true, createdAt: true },
orderBy: { createdAt: "asc" },
take: limit,
});
@@ -258,21 +295,31 @@ export async function runOutboxRequeueBulk(
}
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()})`);
let applied = 0;
for (const candidate of candidates) {
const result = await deps.client.outboxEvent.updateMany({
where: {
id: { in: [candidate.id] },
tenantId,
status: { in: [candidate.status] },
deliveryGeneration: candidate.deliveryGeneration,
},
data: {
status: "PENDING",
retries: 0,
deliveryGeneration: { increment: 1 },
lastError: null,
scheduledAt: now,
lockedUntil: null,
processedAt: null,
},
});
applied += result.count;
}
return { exitCode: 0, status: "requeued", matched: candidates.length, applied: result.count, eventType: args.type, since };
if (args.json) {
deps.output.json({ ok: true, applied, matched: candidates.length, eventType: args.type, since: since.toISOString() });
} else {
deps.output.log(`outbox requeue: ${applied} event(s) re-queued (type=${args.type} since=${since.toISOString()})`);
}
return { exitCode: 0, status: "requeued", matched: candidates.length, applied, eventType: args.type, since };
}