import Redis from 'ioredis';
import { env } from '../../main/config/env';

export const redisClient = new Redis(env.REDIS_URL, {
  retryStrategy: (times: number) => {
    const delay = Math.min(times * 50, 2000);
    return delay;
  },
  maxRetriesPerRequest: 3,
});

redisClient.on('error', (error: Error) => {
  // eslint-disable-next-line no-console
  console.error('Redis client error', error);
});

redisClient.on('connect', () => {
  // eslint-disable-next-line no-console
  console.info('Redis client connected');
});

export const closeRedisClient = async (): Promise<void> => {
  await redisClient.quit();
};
