| import { createLoggerWithContext } from "@midday/logger"; |
|
|
| const logger = createLoggerWithContext("worker:config"); |
|
|
| const isProduction = |
| process.env.NODE_ENV === "production" || |
| process.env.RAILWAY_ENVIRONMENT === "production"; |
|
|
| |
| |
| |
| |
| function parseRedisUrl() { |
| const redisUrl = process.env.REDIS_QUEUE_URL; |
|
|
| if (!redisUrl) { |
| throw new Error("REDIS_QUEUE_URL environment variable is required"); |
| } |
|
|
| const url = new URL(redisUrl); |
|
|
| return { |
| host: url.hostname, |
| port: Number(url.port) || 6379, |
| password: url.password || undefined, |
| username: url.username || undefined, |
| |
| ...(url.protocol === "rediss:" && { |
| tls: {}, |
| }), |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getRedisConnection() { |
| const baseOptions = parseRedisUrl(); |
|
|
| return { |
| ...baseOptions, |
| |
| maxRetriesPerRequest: null, |
| enableReadyCheck: false, |
| |
| lazyConnect: false, |
| family: 4, |
| keepAlive: 30000, |
| connectTimeout: isProduction ? 15000 : 5000, |
| |
| |
| retryStrategy: (times: number) => { |
| const delay = Math.min(1000 * 2 ** times, 20000); |
| if (times > 5) { |
| logger.info( |
| `[Redis/Worker] Reconnecting in ${delay}ms (attempt ${times})`, |
| ); |
| } |
| return delay; |
| }, |
| |
| |
| |
| reconnectOnError: (err: Error) => { |
| const msg = err.message; |
| if (msg.includes("READONLY")) { |
| logger.info( |
| "[Redis/Worker] READONLY error detected (server upgrade/failover), reconnecting", |
| ); |
| return true; |
| } |
| if (msg.includes("timed out") || msg.includes("ETIMEDOUT")) { |
| logger.info("[Redis/Worker] Timeout error detected, reconnecting"); |
| return true; |
| } |
| return false; |
| }, |
| }; |
| } |
|
|
| |
| |
| |
| |
| export function getFlowRedisConnection() { |
| |
| return getRedisConnection(); |
| } |
|
|