From 9d44738ef55a91fc5f5753c728a68267cbb564a5 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:26:45 +0400 Subject: [PATCH] feat: refresh AI cache on proposal start --- src/lib/ai/summary.ts | 2 +- src/lib/ai/textToSpeech.ts | 2 +- src/lib/queue.ts | 10 +++++++++- src/webhook.ts | 11 +++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/lib/ai/summary.ts b/src/lib/ai/summary.ts index bd74af47..6b95de22 100644 --- a/src/lib/ai/summary.ts +++ b/src/lib/ai/summary.ts @@ -3,7 +3,7 @@ import { fetchProposal, Proposal } from '../../helpers/snapshot'; import { IStorage } from '../storage/types'; import Cache from '../cache'; -const tempCacheIds: Map = new Map(); +const tempCacheIds = new Map(); class Summary extends Cache { proposal?: Proposal | null; diff --git a/src/lib/ai/textToSpeech.ts b/src/lib/ai/textToSpeech.ts index dfd06f8f..af03f85f 100644 --- a/src/lib/ai/textToSpeech.ts +++ b/src/lib/ai/textToSpeech.ts @@ -6,7 +6,7 @@ import { IStorage } from '../storage/types'; const MIN_BODY_LENGTH = 500; const MAX_BODY_LENGTH = 4096; -const tempCacheIds: Map = new Map(); +const tempCacheIds = new Map(); export default class TextToSpeech extends Cache { proposal?: Proposal | null; diff --git a/src/lib/queue.ts b/src/lib/queue.ts index 951fe9c5..9cd437b6 100644 --- a/src/lib/queue.ts +++ b/src/lib/queue.ts @@ -1,7 +1,7 @@ import { sleep } from '../helpers/utils'; import { capture } from '@snapshot-labs/snapshot-sentry'; -import Cache from './cache'; import { timeQueueProcess } from './metrics'; +import type Cache from './cache'; const queues = new Map(); const processingItems = new Map(); @@ -11,6 +11,14 @@ async function processItem(cacheable: Cache) { try { const end = timeQueueProcess.startTimer({ name: cacheable.constructor.name }); processingItems.set(cacheable.id, cacheable); + + if ( + ['Summary', 'TextToSpeech'].includes(cacheable.constructor.name) && + !!(await cacheable.getCache()) + ) { + return; + } + await cacheable.createCache(); end(); } catch (e) { diff --git a/src/webhook.ts b/src/webhook.ts index 0df74889..d0061df6 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -2,14 +2,21 @@ import express from 'express'; import { rpcError, rpcSuccess, storageEngine } from './helpers/utils'; import { capture } from '@snapshot-labs/snapshot-sentry'; import VotesReport from './lib/votesReport'; +import Summary from './lib/ai/summary'; +import TextToSpeech from './lib/ai/textToSpeech'; import { queue } from './lib/queue'; const router = express.Router(); -function processVotesReport(id: string, event: string) { +function processEvent(id: string, event: string) { if (event == 'proposal/end') { queue(new VotesReport(id, storageEngine(process.env.VOTE_REPORT_SUBDIR))); } + + if (event === 'proposal/start') { + queue(new Summary(id, storageEngine(process.env.VOTE_REPORT_SUBDIR))); + queue(new TextToSpeech(id, storageEngine(process.env.VOTE_REPORT_SUBDIR))); + } } router.post('/webhook', (req, res) => { @@ -27,7 +34,7 @@ router.post('/webhook', (req, res) => { } try { - processVotesReport(id, event); + processEvent(id, event); return rpcSuccess(res, 'Webhook received', id); } catch (e) { capture(e, { body });