mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 19:07:35 +01:00
feat(cron): prune delivered webhook deliveries older than 30 days
webhook_deliveries grows unbounded today — every successful fire
accumulates a row forever (worker writes status='delivered' then never
deletes), and the deliveries drawer would eventually take ages to
render. Silent issue at any scale beyond MVP.
The 02:00 UTC cleanup cron 3d9e9acb wired up already had a per-task
fanout pattern: add a new task to the `tasks` array, response widens
automatically. This commit drops in a second task next to the
idempotency_results pruner.
Service (deliveryService.pruneDeliveredWebhookDeliveries):
- Deletes status='delivered' rows older than retentionDays (default 30)
- Failed + pending rows are intentionally KEPT
failed → operator audit trail; never re-runs, useful for postmortems
pending → in-flight; the worker still owns these rows
- Errors logged + swallowed so the cleanup cron's other tasks still run.
- 30 days is well past the 24h exp-backoff cap and gives operators a
month of context.
Route doc-comment updated to describe both tasks honestly.
47 suites · 610 tests · 0 failures. tsc + eslint clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
246216a47d
commit
2d17a05ff2
2 files changed
+56
-1
No files matched your search
@@ -14,6 +14,9 @@
|
||||
* the original migration). Without this, every API write
|
||||
* accumulates a row forever; the cleanup keeps the table bounded
|
||||
* to roughly one day of recent writes.
|
||||
* - webhook_deliveries (delivered only): prune rows older than 30
|
||||
* days. Failed + pending rows are kept — failed is the operator
|
||||
* audit trail, pending is in-flight work the worker still owns.
|
||||
*
|
||||
* When you add another expirable plumbing table, add a new task to the
|
||||
* `tasks` array and the response will widen automatically.
|
||||
@@ -25,6 +28,7 @@ import { createAdminClient } from '@/lib/supabase/admin';
|
||||
import { DATABASE_TABLES } from '@/config/database-tables';
|
||||
import { logger } from '@/utils/logger';
|
||||
import { apiSuccess, apiError, apiUnauthorized } from '@/lib/api/standardResponse';
|
||||
import { pruneDeliveredWebhookDeliveries } from '@/services/webhooks/deliveryService';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const maxDuration = 60;
|
||||
@@ -42,6 +46,19 @@ interface CleanupResult {
|
||||
error?: string;
|
||||
}
|
||||
|
||||
async function pruneOldDeliveredWebhooks(): Promise<CleanupResult> {
|
||||
try {
|
||||
const deleted = await pruneDeliveredWebhookDeliveries();
|
||||
return { task: 'webhook_deliveries_delivered', deleted };
|
||||
} catch (error) {
|
||||
return {
|
||||
task: 'webhook_deliveries_delivered',
|
||||
deleted: 0,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneExpiredIdempotencyResults(): Promise<CleanupResult> {
|
||||
const admin = createAdminClient();
|
||||
const nowIso = new Date().toISOString();
|
||||
@@ -68,7 +85,10 @@ export async function GET(request: Request) {
|
||||
}
|
||||
|
||||
try {
|
||||
const tasks: Array<() => Promise<CleanupResult>> = [pruneExpiredIdempotencyResults];
|
||||
const tasks: Array<() => Promise<CleanupResult>> = [
|
||||
pruneExpiredIdempotencyResults,
|
||||
pruneOldDeliveredWebhooks,
|
||||
];
|
||||
|
||||
const results = await Promise.all(tasks.map(task => task()));
|
||||
|
||||
|
||||
@@ -221,6 +221,41 @@ export async function markDelivered(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prune old DELIVERED rows. Without this the table grows unbounded —
|
||||
* every successful webhook fire accumulates a row forever, and the
|
||||
* deliveries drawer would eventually take ages to render.
|
||||
*
|
||||
* Failed + pending rows are intentionally NOT pruned: failed is the
|
||||
* audit trail operators reach for during incidents; pending is in-flight
|
||||
* work the worker needs to see.
|
||||
*
|
||||
* Default retention: 30 days. That's well past any reasonable retry
|
||||
* window (24h cap on the exp-backoff schedule) and gives operators a
|
||||
* month of context for delivered events without unbounded growth.
|
||||
*
|
||||
* Returns the number of rows deleted. Errors are logged + swallowed so
|
||||
* the cleanup cron's other tasks still run.
|
||||
*/
|
||||
export async function pruneDeliveredWebhookDeliveries(
|
||||
retentionDays: number = 30,
|
||||
now: Date = new Date()
|
||||
): Promise<number> {
|
||||
const cutoff = new Date(now.getTime() - retentionDays * 24 * 60 * 60 * 1000);
|
||||
const admin = createAdminClient();
|
||||
const { count, error } = await admin
|
||||
.from(DATABASE_TABLES.WEBHOOK_DELIVERIES)
|
||||
.delete({ count: 'exact' })
|
||||
.eq('status', 'delivered')
|
||||
.lt('created_at', cutoff.toISOString());
|
||||
|
||||
if (error) {
|
||||
logger.warn('pruneDeliveredWebhookDeliveries failed (non-fatal)', { error });
|
||||
return 0;
|
||||
}
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
export async function markFailedOrRetry(
|
||||
delivery: DeliveryRow,
|
||||
responseStatus: number | null,
|
||||
|
||||
Reference in new issue
Block a user