From 59857d085853e3a89179c8692a391cd6c2ecd17a Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Tue, 2 Apr 2024 17:57:04 +0400 Subject: [PATCH] feat: refresh existing AI cache on proposal start --- src/lib/cache.ts | 4 ++++ src/lib/queue.ts | 18 +++++++++--------- src/lib/votesReport.ts | 4 ---- src/webhook.ts | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib/cache.ts b/src/lib/cache.ts index ddbb488..d7c8f43 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -43,4 +43,8 @@ export default class Cache { return content; } + + toString() { + return `${this.constructor.name}#${this.id}`; + } } diff --git a/src/lib/queue.ts b/src/lib/queue.ts index 9cd437b..7b0bd34 100644 --- a/src/lib/queue.ts +++ b/src/lib/queue.ts @@ -10,11 +10,11 @@ async function processItem(cacheable: Cache) { console.log(`[queue] Processing queue item: ${cacheable}`); try { const end = timeQueueProcess.startTimer({ name: cacheable.constructor.name }); - processingItems.set(cacheable.id, cacheable); + processingItems.set(cacheable.toString(), cacheable); if ( ['Summary', 'TextToSpeech'].includes(cacheable.constructor.name) && - !!(await cacheable.getCache()) + !(await cacheable.getCache()) ) { return; } @@ -22,17 +22,17 @@ async function processItem(cacheable: Cache) { await cacheable.createCache(); end(); } catch (e) { - capture(e, { id: cacheable.id }); + capture(e, { id: cacheable.toString() }); console.error(`[queue] Error while processing item`, e); } finally { - queues.delete(cacheable.id); - processingItems.delete(cacheable.id); + queues.delete(cacheable.toString()); + processingItems.delete(cacheable.toString()); } } export function queue(cacheable: Cache) { - if (!queues.has(cacheable.id)) { - queues.set(cacheable.id, cacheable); + if (!queues.has(cacheable.toString())) { + queues.set(cacheable.toString(), cacheable); } return queues.size; @@ -43,8 +43,8 @@ export function size() { } export function getProgress(id: string) { - if (processingItems.has(id)) { - return processingItems.get(id)?.generationProgress as number; + if (processingItems.has(id.toString())) { + return processingItems.get(id.toString())?.generationProgress as number; } return 0; diff --git a/src/lib/votesReport.ts b/src/lib/votesReport.ts index 3da7220..4ed6120 100644 --- a/src/lib/votesReport.ts +++ b/src/lib/votesReport.ts @@ -104,10 +104,6 @@ class VotesReport extends Cache { return votes; }; - toString() { - return `VotesReport#${this.id}`; - } - #formatCsvLine = (vote: Vote) => { let choices: Vote['choice'][] = []; diff --git a/src/webhook.ts b/src/webhook.ts index d0061df..79a0f70 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -14,8 +14,8 @@ function processEvent(id: string, event: string) { } if (event === 'proposal/start') { - queue(new Summary(id, storageEngine(process.env.VOTE_REPORT_SUBDIR))); - queue(new TextToSpeech(id, storageEngine(process.env.VOTE_REPORT_SUBDIR))); + queue(new Summary(id, storageEngine(process.env.AI_SUMMARY_SUBDIR))); + queue(new TextToSpeech(id, storageEngine(process.env.AI_TTS_SUBDIR))); } } @@ -23,7 +23,7 @@ router.post('/webhook', (req, res) => { const body = req.body || {}; const event = body.event?.toString() ?? ''; // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { type, id } = body.id?.toString().split('/'); + const [type, id] = body.id?.toString().split('/'); if (req.headers['authentication'] !== `${process.env.WEBHOOK_AUTH_TOKEN ?? ''}`) { return rpcError(res, 'UNAUTHORIZED', id);