import { Redis } from 'ioredis';
import { config } from '../config.js';

let pub: Redis | null = null;
let sub: Redis | null = null;
let main: Redis | null = null;

export function getRedis(): Redis {
  if (!main) main = new Redis(config.REDIS_URL, { maxRetriesPerRequest: null });
  return main;
}

export function getPublisher(): Redis {
  if (!pub) pub = new Redis(config.REDIS_URL, { maxRetriesPerRequest: null });
  return pub;
}

export function getSubscriber(): Redis {
  if (!sub) sub = new Redis(config.REDIS_URL, { maxRetriesPerRequest: null });
  return sub;
}

export function buildChannel(buildId: string): string {
  return `build:${buildId}`;
}
