From 6fd0c273cb569c9c5302aa7d46364c4ee4284ff2 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 29 Oct 2024 11:07:51 -0300 Subject: [PATCH] fix: follow people create a new migration that replaces the function to NOTIFY only the event id --- src/db/migrations/041_pg_notify_id_only.ts | 27 ++++++++++++++++++++++ src/notify.ts | 11 +++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/db/migrations/041_pg_notify_id_only.ts diff --git a/src/db/migrations/041_pg_notify_id_only.ts b/src/db/migrations/041_pg_notify_id_only.ts new file mode 100644 index 00000000..192dd42f --- /dev/null +++ b/src/db/migrations/041_pg_notify_id_only.ts @@ -0,0 +1,27 @@ +import { Kysely, sql } from 'kysely'; + +export async function up(db: Kysely): Promise { + await sql`DROP TRIGGER IF EXISTS nostr_event_trigger ON nostr_events`.execute(db); + + await sql` + CREATE OR REPLACE FUNCTION notify_nostr_event() + RETURNS TRIGGER AS $$ + BEGIN + PERFORM pg_notify('nostr_event', NEW.id::text); + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + `.execute(db); + + await sql` + CREATE TRIGGER nostr_event_trigger + AFTER INSERT OR UPDATE ON nostr_events + FOR EACH ROW EXECUTE FUNCTION notify_nostr_event() + `.execute(db); +} + +export async function down(db: Kysely): Promise { + await sql`DROP TRIGGER nostr_event_trigger ON nostr_events`.execute(db); + await sql`DROP FUNCTION notify_nostr_event()`.execute(db); +} diff --git a/src/notify.ts b/src/notify.ts index 4033d6c6..69480875 100644 --- a/src/notify.ts +++ b/src/notify.ts @@ -1,5 +1,6 @@ import { Semaphore } from '@lambdalisue/async'; +import { Conf } from '@/config.ts'; import * as pipeline from '@/pipeline.ts'; import { Storages } from '@/storages.ts'; @@ -7,12 +8,18 @@ const sem = new Semaphore(1); export async function startNotify(): Promise { const { listen } = await Storages.database(); + const store = await Storages.db(); listen('nostr_event', (payload) => { sem.lock(async () => { try { - const event = JSON.parse(payload); - await pipeline.handleEvent(event, AbortSignal.timeout(5000)); + const id = payload; + const timeout = Conf.db.timeouts.default; + + const [event] = await store.query([{ ids: [id], limit: 1 }], { signal: AbortSignal.timeout(timeout) }); + if (event) { + await pipeline.handleEvent(event, AbortSignal.timeout(timeout)); + } } catch (e) { console.warn(e); }