Files
FarmaFinder/apps/backend/redis-client.js
T
Antoni Nuñez Romeu e281bd4fa8
Run Tests on Branches / Frontend Mobile Tests (push) Successful in 1m58s
Run Tests on Branches / Detect Changes (push) Successful in 12s
Run Tests on Branches / Backend Tests (push) Failing after 1m58s
Run Tests on Branches / Frontend Tests (push) Has been skipped
fix: skip Redis connection in test mode — prevents CI hang
The top-level await redisClient.connect() in redis-client.js was
blocking module import when no Redis server is available (CI env).
Add NODE_ENV guard + reconnect strategy with max retries.
2026-07-14 13:25:17 +02:00

57 lines
1.5 KiB
JavaScript

import { createClient } from 'redis';
import * as appMetrics from './src/metrics.js';
// Create Redis client
const redisClient = createClient({
socket: {
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379,
reconnectStrategy: (retries) => {
if (retries > 10) return new Error('Redis max retries reached');
return Math.min(retries * 100, 3000);
}
},
password: process.env.REDIS_PASSWORD || undefined
});
// Error handler
redisClient.on('error', (err) => {
appMetrics.redisErrorsTotal.add(1);
console.error('Redis Client Error:', err);
});
// Connection handler
redisClient.on('connect', () => {
console.log('✅ Connected to Redis');
});
// Instrument get/setEx with command duration (used by the CIMA cache path).
const origGet = redisClient.get.bind(redisClient);
redisClient.get = async (...args) => {
const start = performance.now();
try {
return await origGet(...args);
} finally {
appMetrics.redisCmdDuration.record(performance.now() - start);
}
};
const origSetEx = redisClient.setEx.bind(redisClient);
redisClient.setEx = async (...args) => {
const start = performance.now();
try {
return await origSetEx(...args);
} finally {
appMetrics.redisCmdDuration.record(performance.now() - start);
}
};
// Connect to Redis
await redisClient.connect();
// Connect to Redis — skip in test (services are mocked) or when REDIS_URL is unset
if (process.env.NODE_ENV !== 'test') {
await redisClient.connect();
}
export default redisClient;