43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
// worker/index.ts
|
|
import amqp from "amqplib";
|
|
import { processImage } from "./process-image";
|
|
import dotenv from "dotenv";
|
|
|
|
dotenv.config();
|
|
|
|
const queueName = process.env.QUEUE_NAME || "storage.file";
|
|
|
|
async function start() {
|
|
const conn = await amqp.connect(process.env.RABBITMQ_URL!);
|
|
const channel = await conn.createChannel();
|
|
await channel.assertQueue(queueName);
|
|
|
|
console.log(`🎧 Listening for messages on "${queueName}"...`);
|
|
|
|
channel.consume(queueName, async (msg) => {
|
|
if (!msg) return;
|
|
|
|
try {
|
|
const data = JSON.parse(msg.content.toString());
|
|
|
|
const eventName = data.Records[0].eventName;
|
|
const key = decodeURIComponent(data.Records[0].s3.object.key);
|
|
const bucket = data.Records[0].s3.bucket.name;
|
|
|
|
if (eventName !== "s3:ObjectCreated:Put") {
|
|
console.log(`❌ Skipped , Event is not s3:ObjectCreated:Put: ${key}`);
|
|
channel.ack(msg);
|
|
return;
|
|
}
|
|
|
|
await processImage(bucket, key);
|
|
|
|
channel.ack(msg);
|
|
} catch (err) {
|
|
console.error("❌ Error processing message:", err);
|
|
}
|
|
});
|
|
}
|
|
|
|
start();
|