moved proxy redis init to app start
This commit is contained in:
parent
ebd8c94e70
commit
26eaaa6d61
2 changed files with 30 additions and 16 deletions
|
|
@ -7,6 +7,7 @@ import { Hono } from 'hono';
|
||||||
import { onShutdown, setShutdownTimeout } from '@stock-bot/shutdown';
|
import { onShutdown, setShutdownTimeout } from '@stock-bot/shutdown';
|
||||||
import { queueManager } from './services/queue.service';
|
import { queueManager } from './services/queue.service';
|
||||||
import { initializeBatchCache } from './utils/batch-helpers';
|
import { initializeBatchCache } from './utils/batch-helpers';
|
||||||
|
import { initializeProxyCache } from './providers/proxy.tasks';
|
||||||
import {
|
import {
|
||||||
healthRoutes,
|
healthRoutes,
|
||||||
queueRoutes,
|
queueRoutes,
|
||||||
|
|
@ -40,6 +41,11 @@ async function initializeServices() {
|
||||||
await initializeBatchCache();
|
await initializeBatchCache();
|
||||||
logger.info('Batch cache initialized');
|
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)
|
// Initialize queue service (Redis connections should be ready now)
|
||||||
logger.info('Starting queue service initialization...');
|
logger.info('Starting queue service initialization...');
|
||||||
await queueManager.initialize();
|
await queueManager.initialize();
|
||||||
|
|
|
||||||
|
|
@ -105,10 +105,32 @@ async function resetProxyStats(): Promise<void> {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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:',
|
||||||
|
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() {
|
async function initializeSharedResources() {
|
||||||
if (!logger) {
|
if (!logger) {
|
||||||
|
// If not initialized at startup, initialize with fallback mode
|
||||||
logger = getLogger('proxy-tasks');
|
logger = getLogger('proxy-tasks');
|
||||||
cache = createCache({
|
cache = createCache({
|
||||||
keyPrefix: 'proxy:',
|
keyPrefix: 'proxy:',
|
||||||
|
|
@ -116,24 +138,10 @@ async function initializeSharedResources() {
|
||||||
enableMetrics: true
|
enableMetrics: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// Always initialize httpClient and concurrencyLimit first
|
|
||||||
httpClient = new HttpClient({ timeout: 10000 }, logger);
|
httpClient = new HttpClient({ timeout: 10000 }, logger);
|
||||||
concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT);
|
concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT);
|
||||||
|
|
||||||
// Check if cache is ready, but don't block initialization
|
logger.info('Proxy tasks initialized (fallback mode)');
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue