add sleep function on failure
This commit is contained in:
parent
9124c10726
commit
c492068924
30
index.ts
30
index.ts
@ -7,6 +7,35 @@ dotenv.config();
|
|||||||
const queueName = process.env.QUEUE_NAME || "storage.file";
|
const queueName = process.env.QUEUE_NAME || "storage.file";
|
||||||
const rabbitUrl = process.env.RABBITMQ_URL || "amqp://localhost";
|
const rabbitUrl = process.env.RABBITMQ_URL || "amqp://localhost";
|
||||||
|
|
||||||
|
function sleepWithCountdown(seconds: number): Promise<void> {
|
||||||
|
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() {
|
async function start() {
|
||||||
const conn = await amqp.connect(rabbitUrl);
|
const conn = await amqp.connect(rabbitUrl);
|
||||||
const channel = await conn.createChannel();
|
const channel = await conn.createChannel();
|
||||||
@ -49,6 +78,7 @@ async function start() {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ Error processing message:", err);
|
console.error("❌ Error processing message:", err);
|
||||||
// Retry once by requeuing
|
// Retry once by requeuing
|
||||||
|
await sleepWithCountdown(30);
|
||||||
channel.nack(msg, false, true);
|
channel.nack(msg, false, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user