diff --git a/src/index.ts b/src/index.ts index 88c6562..ec44960 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { createServer } from "./app"; import { Logger } from "./utils/logger"; import { startAllCrons } from "./cronjob"; import { getCommit } from "./utils/getCommit"; +import { connectionPromise } from "./utils/redis"; async function init() { process.on("unhandledRejection", (error: any) => { @@ -14,6 +15,7 @@ async function init() { try { await initDb(); + await connectionPromise; } catch (e) { Logger.error(`Init Db: ${e}`); process.exit(1); diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 13002c6..01761ae 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -42,12 +42,14 @@ const writeResponseTime: number[] = []; let lastResponseTimeLimit = 0; const maxStoredTimes = 200; +export let connectionPromise = Promise.resolve(); + if (config.redis?.enabled) { Logger.info("Connected to redis"); const client = createClient(config.redis); const readClient = config.redisRead?.enabled ? createClient(config.redisRead) : null; - void client.connect(); // void as we don't care about the promise - void readClient?.connect(); + connectionPromise = client.connect(); + void readClient?.connect(); // void as we don't care about the promise exportClient = client as RedisSB;