Configuration loader and Bull queue initialization

This commit is contained in:
Walusimbi Silver
2026-03-17 13:02:20 +03:00
parent 2981605394
commit 0382f813a8
2 changed files with 57 additions and 0 deletions

29
src/config/index.js Normal file
View File

@@ -0,0 +1,29 @@
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, "../../.env") });
module.exports = {
port: parseInt(process.env.PORT || "5100"),
host: process.env.HOST || "0.0.0.0",
redis: {
host: process.env.REDIS_HOST || "127.0.0.1",
port: parseInt(process.env.REDIS_PORT || "6379"),
password: process.env.REDIS_PASSWORD || undefined,
db: parseInt(process.env.REDIS_DB || "2"),
},
storage: {
uploadDir: path.resolve(process.env.UPLOAD_DIR || "./uploads"),
outputDir: path.resolve(process.env.OUTPUT_DIR || "./processed"),
maxFileSize: parseInt(process.env.MAX_FILE_SIZE || "20971520"), // 20MB
},
worker: {
concurrency: parseInt(process.env.CONCURRENCY || "2"),
},
bullBoard: {
user: process.env.BULL_BOARD_USER || "admin",
pass: process.env.BULL_BOARD_PASS || "changeme",
},
};

28
src/config/queue.js Normal file
View File

@@ -0,0 +1,28 @@
const Bull = require("bull");
const config = require("./index");
/**
* One queue, multiple job types.
* Bull stores everything in Redis db 2 under the "imgforge:" prefix.
* Job types: resize, convert, compress, watermark, strip-meta, pipeline
*/
const imageQueue = new Bull("imgforge", {
redis: {
host: config.redis.host,
port: config.redis.port,
password: config.redis.password,
db: config.redis.db,
},
defaultJobOptions: {
attempts: 3,
backoff: { type: "exponential", delay: 2000 },
removeOnComplete: { age: 3600 * 24 }, // keep completed jobs 24h
removeOnFail: { age: 3600 * 72 }, // keep failed jobs 72h
},
});
imageQueue.on("error", (err) => {
console.error("[Queue] Redis connection error:", err.message);
});
module.exports = imageQueue;