From 860781011724976f3e7d5a3774f6fdb0bd11f743 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Thu, 21 Nov 2024 22:03:55 +0530 Subject: [PATCH] delete events as they are streamed from db --- scripts/db-policy.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/scripts/db-policy.ts b/scripts/db-policy.ts index 1b65b961..4be3c4ef 100644 --- a/scripts/db-policy.ts +++ b/scripts/db-policy.ts @@ -2,21 +2,18 @@ import { policyWorker } from '@/workers/policy.ts'; import { Storages } from '@/storages.ts'; const db = await Storages.db(); -const ids = []; +let count = 0; for await (const msg of db.req([{}])) { const [type, , event] = msg; if (type === 'EOSE') console.log('EOSE'); if (type !== 'EVENT') continue; const [, , ok] = await policyWorker.call(event, AbortSignal.timeout(5000)); - if (!ok) ids.push(event.id); + if (!ok) { + await db.remove([{ ids: [event.id] }]); + count += 1; + } } -try { - await db.remove([{ ids }]); - console.log(`Cleaned up ${ids.length} events from the db.`); - Deno.exit(0); -} catch (e) { - console.error(e); - Deno.exit(1); -} +console.log(`Cleaned up ${count} events from the db.`); +Deno.exit(0);