diff --git a/src/lib/ai/summary.ts b/src/lib/ai/summary.ts index bd74af4..6b95de2 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 dfd06f8..af03f85 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 951fe9c..9cd437b 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 0df7488..d0061df 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 });