From ed1d248ab6601f773af7ac5dc3e8db6a6bc73894 Mon Sep 17 00:00:00 2001 From: Miodec Date: Fri, 25 Mar 2022 15:25:03 +0100 Subject: [PATCH] allowing to run backend without redis --- backend/init/redis.ts | 12 +++++++++--- backend/server.ts | 12 +++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/backend/init/redis.ts b/backend/init/redis.ts index b473f9f5c..2758d96e9 100644 --- a/backend/init/redis.ts +++ b/backend/init/redis.ts @@ -4,15 +4,20 @@ class RedisClient { static connection: IORedis.Redis; static connected = false; - static async connect(): Promise { + static async connect(): Promise { if (this.connected) { - return; + return true; } const { REDIS_URI } = process.env; if (!REDIS_URI) { - throw new Error("No redis configuration provided"); + if (process.env.MODE === "dev") { + console.log("No redis configuration provided. Running without redis."); + return false; + } else { + throw new Error("No redis configuration provided"); + } } this.connection = new IORedis(REDIS_URI, { @@ -24,6 +29,7 @@ class RedisClient { try { await this.connection.connect(); this.connected = true; + return true; } catch (error) { console.error(error.message); console.error( diff --git a/backend/server.ts b/backend/server.ts index c93250a45..ad6169b9e 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -31,12 +31,14 @@ async function bootServer(port: number): Promise { console.log("Live configuration fetched"); console.log("Connecting to redis..."); - await RedisClient.connect(); - console.log("Connected to redis"); + const redisConnected = await RedisClient.connect(); + if (redisConnected) console.log("Connected to redis"); - console.log("Initializing task queues..."); - George.initJobQueue(RedisClient.getConnection()); - console.log("Task queues initialized"); + if (redisConnected) { + console.log("Initializing task queues..."); + George.initJobQueue(RedisClient.getConnection()); + console.log("Task queues initialized"); + } console.log("Starting cron jobs..."); jobs.forEach((job) => job.start());