make redis client conditional

This commit is contained in:
Maidul Islam
2023-09-15 17:55:43 -04:00
parent 5494bc6c3c
commit 859cec49d1
2 changed files with 12 additions and 3 deletions

View File

@@ -1,3 +1,12 @@
import { Redis } from "ioredis"
export const redisClient = new Redis(process.env.REDIS_URL as string);
let redisClient: Redis | null;
if (process.env.REDIS_URL) {
redisClient = new Redis(process.env.REDIS_URL as string);
} else {
console.warn("Redis URL not set, skipping Redis initialization.");
redisClient = null
}
export { redisClient }

View File

@@ -689,7 +689,7 @@ export const backfillUserAuthMethods = async () => {
export const backfillPermission = async () => {
const lockKey = "backfill_permission_lock";
const timeout = 5000; // Lock timeout in milliseconds
const lock = await redisClient.set(lockKey, 1, "PX", timeout, "NX");
const lock = await redisClient?.set(lockKey, 1, "PX", timeout, "NX");
if (lock) {
try {
@@ -807,7 +807,7 @@ export const backfillPermission = async () => {
} catch (error) {
console.error("An error occurred when running script [backfillPermission]:", error);
} finally {
await redisClient.del(lockKey);
await redisClient?.del(lockKey);
}
} else {
console.info("Could not acquire lock for script [backfillPermission], skipping");