monkeytype/backend/init/redis.ts
Rizwan Mustafa 020ac5cb24
Add logging support from Winston with colored output (#2774)
* Converted initial outputs to color outputs

* Some more coloring

* Colored all error outputs

* Completed coloring of outputs

* Created basic logger instance

* Moved over to Winston for logging

* Remove unnnecessary stuff

* Added max file size

* Renamed log to logToDb

* minor refactor and added tab separation with timestamps

* Some changes. Thanks Bruception

* Created wrapper for logger

* Tiny refactor

* Some fixes

* Some fixes

* Update example env

* Remove general

* using default yellow, making colors bold

* removed square brackets

* removed square brackets

* using logger instead of console log

* updated timestamp format

* moved comment up

* Fixed typo

Co-authored-by: Miodec <bartnikjack@gmail.com>
2022-03-27 11:29:11 -04:00

52 lines
1.1 KiB
TypeScript

import IORedis from "ioredis";
import Logger from "../utils/logger";
class RedisClient {
static connection: IORedis.Redis;
static connected = false;
static async connect(): Promise<void> {
if (this.connected) {
return;
}
const { REDIS_URI } = process.env;
if (!REDIS_URI) {
if (process.env.MODE === "dev") {
Logger.warning(
"No redis configuration provided. Running without redis."
);
return;
}
throw new Error("No redis configuration provided");
}
this.connection = new IORedis(REDIS_URI, {
maxRetriesPerRequest: null, // These options are required for BullMQ
enableReadyCheck: false,
lazyConnect: true,
});
try {
await this.connection.connect();
this.connected = true;
} catch (error) {
Logger.error(error.message);
Logger.error(
"Failed to connect to redis. Exiting with exit status code 1."
);
process.exit(1);
}
}
static isConnected(): boolean {
return this.connected;
}
static getConnection(): IORedis.Redis {
return this.connection;
}
}
export default RedisClient;