moved proxy redis init to app start

This commit is contained in:
Boki 2025-06-10 22:50:10 -04:00
parent a7ec942916
commit 35b0eb3783
2 changed files with 30 additions and 16 deletions

View file

@ -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();

View file

@ -105,10 +105,11 @@ async function resetProxyStats(): Promise<void> {
return Promise.resolve();
}
// Initialize shared resources
async function initializeSharedResources() {
if (!logger) {
/**
* Initialize proxy cache for use during application startup
* This should be called before any proxy operations
*/
export async function initializeProxyCache(): Promise<void> {
logger = getLogger('proxy-tasks');
cache = createCache({
keyPrefix: 'proxy:',
@ -116,24 +117,31 @@ async function initializeSharedResources() {
enableMetrics: true
});
// Always initialize httpClient and concurrencyLimit first
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);
// 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');
async function initializeSharedResources() {
if (!logger) {
// If not initialized at startup, initialize with fallback mode
logger = getLogger('proxy-tasks');
cache = createCache({
keyPrefix: 'proxy:',
ttl: PROXY_CONFIG.CACHE_TTL,
enableMetrics: true
});
httpClient = new HttpClient({ timeout: 10000 }, logger);
concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT);
logger.info('Proxy tasks initialized (fallback mode)');
}
}