From 26eaaa6d61063ee83c288870d109afa5330cf3fa Mon Sep 17 00:00:00 2001 From: Boki Date: Tue, 10 Jun 2025 22:50:10 -0400 Subject: [PATCH] moved proxy redis init to app start --- apps/data-service/src/index.ts | 6 +++ .../data-service/src/providers/proxy.tasks.ts | 40 +++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/apps/data-service/src/index.ts b/apps/data-service/src/index.ts index 49541ae..47133df 100644 --- a/apps/data-service/src/index.ts +++ b/apps/data-service/src/index.ts @@ -7,6 +7,7 @@ import { Hono } from 'hono'; import { onShutdown, setShutdownTimeout } from '@stock-bot/shutdown'; import { queueManager } from './services/queue.service'; import { initializeBatchCache } from './utils/batch-helpers'; +import { initializeProxyCache } from './providers/proxy.tasks'; import { healthRoutes, queueRoutes, @@ -40,6 +41,11 @@ async function initializeServices() { await initializeBatchCache(); logger.info('Batch cache initialized'); + // Initialize proxy cache - before queue service + logger.info('Starting proxy cache initialization...'); + await initializeProxyCache(); + logger.info('Proxy cache initialized'); + // Initialize queue service (Redis connections should be ready now) logger.info('Starting queue service initialization...'); await queueManager.initialize(); diff --git a/apps/data-service/src/providers/proxy.tasks.ts b/apps/data-service/src/providers/proxy.tasks.ts index 573102f..df160b5 100644 --- a/apps/data-service/src/providers/proxy.tasks.ts +++ b/apps/data-service/src/providers/proxy.tasks.ts @@ -105,10 +105,32 @@ async function resetProxyStats(): Promise { return Promise.resolve(); } +/** + * Initialize proxy cache for use during application startup + * This should be called before any proxy operations + */ +export async function initializeProxyCache(): Promise { + logger = getLogger('proxy-tasks'); + cache = createCache({ + keyPrefix: 'proxy:', + ttl: PROXY_CONFIG.CACHE_TTL, + enableMetrics: true + }); + + logger.info('Initializing proxy cache...'); + await cache.waitForReady(10000); + logger.info('Proxy cache initialized successfully'); + + // Initialize other shared resources that don't require cache + httpClient = new HttpClient({ timeout: 10000 }, logger); + concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT); + + logger.info('Proxy tasks initialized'); +} -// Initialize shared resources async function initializeSharedResources() { if (!logger) { + // If not initialized at startup, initialize with fallback mode logger = getLogger('proxy-tasks'); cache = createCache({ keyPrefix: 'proxy:', @@ -116,24 +138,10 @@ async function initializeSharedResources() { enableMetrics: true }); - // Always initialize httpClient and concurrencyLimit first httpClient = new HttpClient({ timeout: 10000 }, logger); concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT); - // Check if cache is ready, but don't block initialization - if (cache.isReady()) { - logger.info('Cache already ready'); - } else { - logger.info('Cache not ready yet, tasks will use fallback mode'); - // Try to wait briefly for cache to be ready, but don't block - cache.waitForReady(5000).then(() => { - logger.info('Cache became ready after initialization'); - }).catch(error => { - logger.warn('Cache connection timeout, continuing with fallback mode:', {error: error.message}); - }); - } - - logger.info('Proxy tasks initialized'); + logger.info('Proxy tasks initialized (fallback mode)'); } }