From 3a13d5c140c687a7ba89084822eb4c5c1fc30899 Mon Sep 17 00:00:00 2001 From: pinks Date: Mon, 23 Oct 2023 02:40:58 +0200 Subject: [PATCH] fix: slow queue position updates --- app/generationQueue.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/app/generationQueue.ts b/app/generationQueue.ts index 5ad3b24..49700d0 100644 --- a/app/generationQueue.ts +++ b/app/generationQueue.ts @@ -306,24 +306,24 @@ async function processGenerationJob( export async function updateGenerationQueue() { while (true) { const jobs = await generationQueue.getAllJobs(); - let index = 0; - for (const job of jobs) { - if (job.lockUntil > new Date()) { - // job is currently being processed, the worker will update its status message - continue; + + await Promise.all(jobs.map(async (job) => { + if (job.status === "processing") { + // if the job is processing, the worker will update its status message + return; } - if (!job.state.replyMessage) { - // no status message, nothing to update - continue; - } - index++; + + // spread the updates in time randomly + await delay(Math.random() * 3_000); + await bot.api.editMessageText( job.state.replyMessage.chat.id, job.state.replyMessage.message_id, - `You are ${formatOrdinal(index)} in queue.`, + `You are ${formatOrdinal(job.place)} in queue.`, { maxAttempts: 1 }, ).catch(() => undefined); - } - await delay(3000); + })); + + await delay(10_000); } }