Merge pull request #2199 from bluewave-labs/feat/redis-service
feat: redis service, resovles #2112
This commit is contained in:
4 files changed
+115
-22
No files matched your search
+16
-11
@@ -81,7 +81,9 @@ import ServiceRegistry from "./service/serviceRegistry.js";
|
|||||||
|
|
||||||
import MongoDB from "./db/mongo/MongoDB.js";
|
import MongoDB from "./db/mongo/MongoDB.js";
|
||||||
|
|
||||||
|
// Redis Service and dependencies
|
||||||
import IORedis from "ioredis";
|
import IORedis from "ioredis";
|
||||||
|
import RedisService from "./service/redisService.js";
|
||||||
|
|
||||||
import TranslationService from "./service/translationService.js";
|
import TranslationService from "./service/translationService.js";
|
||||||
import languageMiddleware from "./middleware/languageMiddleware.js";
|
import languageMiddleware from "./middleware/languageMiddleware.js";
|
||||||
@@ -113,21 +115,16 @@ const shutdown = async () => {
|
|||||||
method: "shutdown",
|
method: "shutdown",
|
||||||
});
|
});
|
||||||
// flush Redis
|
// flush Redis
|
||||||
const settings =
|
const redisService = ServiceRegistry.get(RedisService.SERVICE_NAME);
|
||||||
ServiceRegistry.get(SettingsService.SERVICE_NAME).getSettings() || {};
|
await redisService.flushall();
|
||||||
|
|
||||||
const { redisUrl } = settings;
|
|
||||||
const redis = new IORedis(redisUrl, { maxRetriesPerRequest: null });
|
|
||||||
|
|
||||||
logger.info({ message: "Flushing Redis" });
|
|
||||||
await redis.flushall();
|
|
||||||
logger.info({ message: "Redis flushed" });
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}, SHUTDOWN_TIMEOUT);
|
}, SHUTDOWN_TIMEOUT);
|
||||||
try {
|
try {
|
||||||
server.close();
|
server.close();
|
||||||
await ServiceRegistry.get(JobQueue.SERVICE_NAME).obliterate();
|
await ServiceRegistry.get(JobQueue.SERVICE_NAME).obliterate();
|
||||||
await ServiceRegistry.get(MongoDB.SERVICE_NAME).disconnect();
|
await ServiceRegistry.get(MongoDB.SERVICE_NAME).disconnect();
|
||||||
|
await ServiceRegistry.get(RedisService.SERVICE_NAME).flushall();
|
||||||
logger.info({ message: "Graceful shutdown complete" });
|
logger.info({ message: "Graceful shutdown complete" });
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -185,7 +182,13 @@ const startApp = async () => {
|
|||||||
stringService
|
stringService
|
||||||
);
|
);
|
||||||
|
|
||||||
const jobQueue = new JobQueue(
|
const redisService = await RedisService.createInstance({
|
||||||
|
logger,
|
||||||
|
IORedis,
|
||||||
|
SettingsService: settingsService,
|
||||||
|
});
|
||||||
|
|
||||||
|
const jobQueue = new JobQueue({
|
||||||
db,
|
db,
|
||||||
statusService,
|
statusService,
|
||||||
networkService,
|
networkService,
|
||||||
@@ -194,8 +197,9 @@ const startApp = async () => {
|
|||||||
stringService,
|
stringService,
|
||||||
logger,
|
logger,
|
||||||
Queue,
|
Queue,
|
||||||
Worker
|
Worker,
|
||||||
);
|
redisService,
|
||||||
|
});
|
||||||
|
|
||||||
// Register services
|
// Register services
|
||||||
ServiceRegistry.register(JobQueue.SERVICE_NAME, jobQueue);
|
ServiceRegistry.register(JobQueue.SERVICE_NAME, jobQueue);
|
||||||
@@ -207,6 +211,7 @@ const startApp = async () => {
|
|||||||
ServiceRegistry.register(StatusService.SERVICE_NAME, statusService);
|
ServiceRegistry.register(StatusService.SERVICE_NAME, statusService);
|
||||||
ServiceRegistry.register(NotificationService.SERVICE_NAME, notificationService);
|
ServiceRegistry.register(NotificationService.SERVICE_NAME, notificationService);
|
||||||
ServiceRegistry.register(TranslationService.SERVICE_NAME, translationService);
|
ServiceRegistry.register(TranslationService.SERVICE_NAME, translationService);
|
||||||
|
ServiceRegistry.register(RedisService.SERVICE_NAME, redisService);
|
||||||
|
|
||||||
await translationService.initialize();
|
await translationService.initialize();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import IORedis from "ioredis";
|
|
||||||
|
|
||||||
const QUEUE_NAMES = ["uptime", "pagespeed", "hardware", "distributed"];
|
const QUEUE_NAMES = ["uptime", "pagespeed", "hardware", "distributed"];
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
const JOBS_PER_WORKER = 5;
|
const JOBS_PER_WORKER = 5;
|
||||||
@@ -18,7 +16,7 @@ const getSchedulerId = (monitor) => `scheduler:${monitor.type}:${monitor._id}`;
|
|||||||
class NewJobQueue {
|
class NewJobQueue {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
constructor(
|
constructor({
|
||||||
db,
|
db,
|
||||||
statusService,
|
statusService,
|
||||||
networkService,
|
networkService,
|
||||||
@@ -27,16 +25,14 @@ class NewJobQueue {
|
|||||||
stringService,
|
stringService,
|
||||||
logger,
|
logger,
|
||||||
Queue,
|
Queue,
|
||||||
Worker
|
Worker,
|
||||||
) {
|
redisService,
|
||||||
const settings = settingsService.getSettings() || {};
|
}) {
|
||||||
const { redisUrl } = settings;
|
this.connection = redisService.getConnection();
|
||||||
const connection = new IORedis(redisUrl, { maxRetriesPerRequest: null });
|
|
||||||
this.queues = {};
|
this.queues = {};
|
||||||
this.workers = {};
|
this.workers = {};
|
||||||
this.lastJobProcessedTime = {};
|
this.lastJobProcessedTime = {};
|
||||||
|
|
||||||
this.connection = connection;
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.networkService = networkService;
|
this.networkService = networkService;
|
||||||
this.statusService = statusService;
|
this.statusService = statusService;
|
||||||
@@ -47,7 +43,7 @@ class NewJobQueue {
|
|||||||
this.stringService = stringService;
|
this.stringService = stringService;
|
||||||
|
|
||||||
QUEUE_NAMES.forEach((name) => {
|
QUEUE_NAMES.forEach((name) => {
|
||||||
const q = new Queue(name, { connection });
|
const q = new Queue(name, { connection: this.connection });
|
||||||
this.lastJobProcessedTime[q.name] = Date.now();
|
this.lastJobProcessedTime[q.name] = Date.now();
|
||||||
q.on("error", (error) => {
|
q.on("error", (error) => {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
class RedisService {
|
||||||
|
static SERVICE_NAME = "RedisService";
|
||||||
|
|
||||||
|
constructor({ logger, IORedis, SettingsService }) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.IORedis = IORedis;
|
||||||
|
this.SettingsService = SettingsService;
|
||||||
|
this.connection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createInstance({ logger, IORedis, SettingsService }) {
|
||||||
|
const instance = new RedisService({ logger, IORedis, SettingsService });
|
||||||
|
await instance.connect();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
async connect() {
|
||||||
|
const settings = this.SettingsService.getSettings();
|
||||||
|
const { redisUrl } = settings;
|
||||||
|
this.connection = new this.IORedis(redisUrl, {
|
||||||
|
maxRetriesPerRequest: null,
|
||||||
|
retryStrategy: (times) => {
|
||||||
|
if (times >= 5) {
|
||||||
|
throw new Error("Failed to connect to Redis");
|
||||||
|
}
|
||||||
|
this.logger.debug({
|
||||||
|
message: "Retrying Redis connection",
|
||||||
|
service: RedisService.SERVICE_NAME,
|
||||||
|
details: { times },
|
||||||
|
});
|
||||||
|
return Math.min(times * 100, 2000);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
let errorOccurred = false;
|
||||||
|
|
||||||
|
this.connection.on("ready", () => {
|
||||||
|
if (!errorOccurred) {
|
||||||
|
this.logger.info({
|
||||||
|
message: "Redis connection established",
|
||||||
|
service: RedisService.SERVICE_NAME,
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.connection.on("error", (err) => {
|
||||||
|
errorOccurred = true;
|
||||||
|
this.logger.error({
|
||||||
|
message: "Redis connection error",
|
||||||
|
service: RedisService.SERVICE_NAME,
|
||||||
|
error: err,
|
||||||
|
});
|
||||||
|
setTimeout(() => reject(err), 5000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async flushall() {
|
||||||
|
this.logger.debug({
|
||||||
|
message: "Flushing all Redis data",
|
||||||
|
service: RedisService.SERVICE_NAME,
|
||||||
|
});
|
||||||
|
await this.connection.flushall();
|
||||||
|
}
|
||||||
|
|
||||||
|
getConnection() {
|
||||||
|
return this.connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RedisService;
|
||||||
+21
-1
@@ -1,4 +1,6 @@
|
|||||||
import { createLogger, format, transports } from "winston";
|
import { createLogger, format, transports } from "winston";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -40,8 +42,10 @@ class Logger {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const logLevel = process.env.LOG_LEVEL || "info";
|
||||||
|
|
||||||
this.logger = createLogger({
|
this.logger = createLogger({
|
||||||
level: "info",
|
level: logLevel,
|
||||||
format: format.combine(format.timestamp()),
|
format: format.combine(format.timestamp()),
|
||||||
transports: [
|
transports: [
|
||||||
new transports.Console({
|
new transports.Console({
|
||||||
@@ -107,6 +111,22 @@ class Logger {
|
|||||||
stack: config.stack,
|
stack: config.stack,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Logs a debug message.
|
||||||
|
* @param {Object} config - The configuration object.
|
||||||
|
* @param {string} config.message - The message to log.
|
||||||
|
* @param {string} config.service - The service name.
|
||||||
|
* @param {string} config.method - The method name.
|
||||||
|
* @param {Object} config.details - Additional details.
|
||||||
|
*/
|
||||||
|
debug(config) {
|
||||||
|
this.logger.debug(config.message, {
|
||||||
|
service: config.service,
|
||||||
|
method: config.method,
|
||||||
|
details: config.details,
|
||||||
|
stack: config.stack,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const logger = new Logger();
|
const logger = new Logger();
|
||||||
|
|||||||
Reference in new issue
Block a user