How big the app's data is, and a webhook delivery log with Send again

Settings -> Data -> Storage measures the database, backups and sessions and every
table (size, rows, growth, how long it is kept, how far back it reaches), and flags
histories that are not being trimmed or have no limit.

Event webhooks now keep a delivery log (last 100 per webhook, 14 days): what was
sent, how it went, the message itself, and Send again, which resends it exactly as it
was (same id and signature). Resends are audited and in the Change log.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-21 20:58:57 +02:00
1 parent aebf3c6ce1
commit e114567d64
22 files changed
+6833 -19

No files matched your search

+31 -4
View File
@@ -11,9 +11,9 @@
* Settings) and forgotten.
*/
import { createHmac, randomUUID } from "node:crypto";
import { eq } from "drizzle-orm";
import { and, desc, eq, lt, notInArray } from "drizzle-orm";
import { db } from "../db/client.js";
import { eventWebhooks, webhookEvents, type WebhookEvent } from "../db/schema.js";
import { eventWebhooks, webhookDeliveries, webhookEvents, type WebhookEvent } from "../db/schema.js";
import { decryptSecret, encryptSecret } from "../crypto.js";
import { env } from "../env.js";
@@ -76,23 +76,50 @@ async function post(row: EventWebhookRow, event: string, body: string, deliveryI
}
}
/** One message to one webhook, retried once if it didn't go through. The outcome is written on the webhook. */
/** How many deliveries are kept for each webhook, and for how long. */
export const KEEP_DELIVERIES = 100;
export const KEEP_DELIVERY_DAYS = 14;
/** A body longer than this is not kept, so it cannot be sent again by hand. */
export const MAX_STORED_BODY = 16_000;
/** One message to one webhook, retried once if it didn't go through. The outcome is written on the webhook and in its delivery log. */
export async function deliver(row: EventWebhookRow, event: string, data: unknown, options: { retryAfterMs?: number } = {}): Promise<Delivery> {
const deliveryId = randomUUID();
const body = JSON.stringify({ event, id: deliveryId, createdAt: new Date().toISOString(), source: "homelab-manager", data });
return sendAndNote(row, event, body, deliveryId, options, null);
}
/** Sends a message that was sent before, exactly as it was (the same id, so a receiver that already handled it can ignore it). Says how it went, and writes that down as a delivery of its own. */
export async function redeliver(row: EventWebhookRow, earlier: typeof webhookDeliveries.$inferSelect, options: { retryAfterMs?: number } = {}): Promise<Delivery> {
return sendAndNote(row, earlier.event, earlier.body, earlier.deliveryId, options, earlier.id);
}
async function sendAndNote(row: EventWebhookRow, event: string, body: string, deliveryId: string, options: { retryAfterMs?: number }, retryOf: number | null): Promise<Delivery> {
let attempts = 1;
let outcome = await post(row, event, body, deliveryId);
if (!outcome.ok) {
await new Promise((r) => setTimeout(r, options.retryAfterMs ?? RETRY_AFTER_MS));
outcome = await post(row, event, body, deliveryId);
attempts = 2;
}
const now = new Date();
try {
await db.update(eventWebhooks).set({ lastAttemptAt: new Date().toISOString(), lastResult: outcome.result }).where(eq(eventWebhooks.id, row.id));
await db.update(eventWebhooks).set({ lastAttemptAt: now.toISOString(), lastResult: outcome.result }).where(eq(eventWebhooks.id, row.id));
await db.insert(webhookDeliveries).values({ webhookId: row.id, event, deliveryId, attempts, ok: outcome.ok, result: outcome.result, body: body.slice(0, MAX_STORED_BODY + 1), retryOf, createdAt: now.toISOString() });
await pruneDeliveries(row.id, now);
} catch (err) {
console.error("[webhooks] couldn't note the result:", err);
}
return outcome;
}
/** Keeps the last hundred deliveries of a webhook, and none older than two weeks. */
export async function pruneDeliveries(webhookId: number, now: Date = new Date()): Promise<void> {
const keep = await db.select({ id: webhookDeliveries.id }).from(webhookDeliveries).where(eq(webhookDeliveries.webhookId, webhookId)).orderBy(desc(webhookDeliveries.createdAt), desc(webhookDeliveries.id)).limit(KEEP_DELIVERIES);
if (keep.length === KEEP_DELIVERIES) await db.delete(webhookDeliveries).where(and(eq(webhookDeliveries.webhookId, webhookId), notInArray(webhookDeliveries.id, keep.map((k) => k.id))));
await db.delete(webhookDeliveries).where(and(eq(webhookDeliveries.webhookId, webhookId), lt(webhookDeliveries.createdAt, new Date(now.getTime() - KEEP_DELIVERY_DAYS * 86_400_000).toISOString())));
}
/**
* Sends an event to every enabled webhook that wants it. Returns at once — delivery carries on in the background and can never delay or
* fail what caused the event.