From c4920689241443caa1791185e3a274abab125ca7 Mon Sep 17 00:00:00 2001 From: Nimer Farahty Date: Sun, 20 Apr 2025 01:11:34 +0300 Subject: [PATCH] add sleep function on failure --- index.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/index.ts b/index.ts index 9e8ace8..af4f133 100644 --- a/index.ts +++ b/index.ts @@ -7,6 +7,35 @@ dotenv.config(); const queueName = process.env.QUEUE_NAME || "storage.file"; const rabbitUrl = process.env.RABBITMQ_URL || "amqp://localhost"; +function sleepWithCountdown(seconds: number): Promise { + return new Promise((resolve) => { + let remaining = seconds; + + const update = () => { + const msg = `⏳ Waiting ${remaining}s...`; + process.stdout.clearLine(0); // Clear current line + process.stdout.cursorTo(0); // Move cursor to start of line + process.stdout.write(msg); // Write message + }; + + update(); // Initial message + + const interval = setInterval(() => { + remaining--; + + if (remaining > 0) { + update(); + } else { + clearInterval(interval); + process.stdout.clearLine(0); + process.stdout.cursorTo(0); + process.stdout.write(`✅ Resuming now\n`); + resolve(); + } + }, 1000); + }); +} + async function start() { const conn = await amqp.connect(rabbitUrl); const channel = await conn.createChannel(); @@ -49,6 +78,7 @@ async function start() { } catch (err) { console.error("❌ Error processing message:", err); // Retry once by requeuing + await sleepWithCountdown(30); channel.nack(msg, false, true); } });