Skip to content

Commit

Permalink
feat: refresh existing AI cache on proposal start
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Apr 2, 2024
1 parent e05eae8 commit 59857d0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ export default class Cache {

return content;
}

toString() {
return `${this.constructor.name}#${this.id}`;
}
}
18 changes: 9 additions & 9 deletions src/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ 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;
}

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;
Expand All @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/lib/votesReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ class VotesReport extends Cache {
return votes;
};

toString() {
return `VotesReport#${this.id}`;
}

#formatCsvLine = (vote: Vote) => {
let choices: Vote['choice'][] = [];

Expand Down
6 changes: 3 additions & 3 deletions src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ 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)));
}
}

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);
Expand Down

0 comments on commit 59857d0

Please sign in to comment.