From 3469374d8eb3e9e530ceeddffdc73311650d1325 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 24 Sep 2024 16:15:26 -0500 Subject: [PATCH] pipeline: insert event even if stats fail --- src/pipeline.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index a1222767..57db0e20 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -135,10 +135,18 @@ async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise { - await updateStats({ event, store, kysely }).catch((e) => console.error(e)); - await store.event(event, { signal }); - }); + try { + await store.transaction(async (store, kysely) => { + await updateStats({ event, store, kysely }); + await store.event(event, { signal }); + }); + } catch (e) { + // If the failure is only because of updateStats (which runs first), insert the event anyway. + // We can't catch this in the transaction because the error aborts the transaction on the Postgres side. + if (e instanceof Error && e.message.includes('event_stats' satisfies keyof DittoTables)) { + await store.event(event, { signal }); + } + } } /** Parse kind 0 metadata and track indexes in the database. */