@acadix/setup
Version:
Acadix Learning Management System backend application project setup
84 lines • 3.59 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../../config");
const logger_1 = require("../../logger");
class RedisManager {
constructor(env) {
this.env = env;
this.logger = (0, logger_1.Logger)(this.env);
this.redis = (0, config_1.Redis)(Object.assign({}, this.env.REDIS));
}
createRedisConnection() {
return __awaiter(this, void 0, void 0, function* () {
const maxRetries = 5;
let retries = 0;
while (retries < maxRetries) {
try {
this.redis.on("connect", () => __awaiter(this, void 0, void 0, function* () {
this.logger.info("Connection to the redis has been established successfully");
}));
yield this.redis.connect();
return; // Connection succeeded, exit the function
}
catch (error) {
this.logger.error("Failed to connect to Redis:", error);
retries++;
const delay = 5000; // 5 seconds delay between retries (adjust as needed)
yield new Promise((resolve) => setTimeout(resolve, delay));
}
}
process.exit(1);
});
}
saveToRedis(key, data, expiration) {
return __awaiter(this, void 0, void 0, function* () {
const serializedData = JSON.stringify(data);
yield this.redis.SETEX(key, expiration, serializedData);
return yield this.redis.quit();
});
}
getDataFromRedis(key) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.redis.get(key);
yield this.redis.quit();
return data ? JSON.parse(data) : null;
});
}
publishMessage(channel, message) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.redis.publish(channel, message);
});
}
subscribeToChannel(channel, callback) {
// this.redis.subscribe(channel,)
// this.redis.on('message', (ch, message))=> {
// if (ch == channel) {
// callback(JSON.parse(message))
// }
// }
}
closeRedisConnection() {
return __awaiter(this, void 0, void 0, function* () {
yield this.redis
.quit()
.then(() => {
this.logger.info("Redis connection closed");
})
.catch((err) => {
this.logger.error(`Error closing redis connection: ${err.message}`);
process.exit(1);
});
});
}
}
exports.default = RedisManager;
//# sourceMappingURL=connectRedis.js.map