sleep 10 second between each run

This commit is contained in:
Nimer Farahty 2025-04-20 01:56:37 +03:00
parent 0c0c836184
commit 956b0d9751
3 changed files with 32 additions and 29 deletions

View File

@ -1,41 +1,13 @@
import amqp from "amqplib"; import amqp from "amqplib";
import { processImage } from "./process-image"; import { processImage } from "./process-image";
import dotenv from "dotenv"; import dotenv from "dotenv";
import { sleepWithCountdown } from "./util";
dotenv.config(); 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();

View File

@ -1,6 +1,7 @@
import sharp from "sharp"; import sharp from "sharp";
import { getMinioClient } from "./minio"; import { getMinioClient } from "./minio";
import { lookup } from "mime-types"; import { lookup } from "mime-types";
import { sleepWithCountdown } from "./util";
export async function processImage( export async function processImage(
bucket: string, bucket: string,
@ -51,6 +52,8 @@ export async function processImage(
} }
try { try {
await sleepWithCountdown(5);
// 🖼️ Create thumbnail // 🖼️ Create thumbnail
const thumb = await sharp(buffer).resize(200).toBuffer(); const thumb = await sharp(buffer).resize(200).toBuffer();
await writeImage(`${filePath}/thumbs/${fileName}`, thumb, mime); await writeImage(`${filePath}/thumbs/${fileName}`, thumb, mime);

28
util.ts Normal file
View File

@ -0,0 +1,28 @@
export 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);
});
}