This commit is contained in:
Boki 2025-06-10 15:07:37 -04:00
parent 32d0eaac2d
commit 9825e99540
3 changed files with 68 additions and 59 deletions

View file

@ -120,14 +120,17 @@ async function initializeSharedResources() {
httpClient = new HttpClient({ timeout: 10000 }, logger);
concurrencyLimit = pLimit(PROXY_CONFIG.CONCURRENCY_LIMIT);
// Try to connect to cache, but don't block initialization if it fails
try {
// Use longer timeout for cache connection
await cache.waitForReady(30000); // 30 seconds
logger.info('Cache connection established');
} catch (error) {
logger.warn('Cache connection failed, continuing with degraded functionality:', {error});
// Don't throw - allow the service to continue with cache fallbacks
// 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');

View file

@ -33,8 +33,8 @@ export class BatchProcessor {
ttl: cacheOptions?.ttl || 86400 * 2, // 48 hours default
enableMetrics: true
});
this.initialize();
}
/**
* Initialize the batch processor and wait for cache to be ready
*/
@ -55,19 +55,19 @@ export class BatchProcessor {
ttlHours: ((this.cacheOptions?.ttl || 86400 * 2) / 3600).toFixed(1)
});
} catch (error) {
logger.error('Failed to initialize BatchProcessor', {
logger.warn('BatchProcessor cache not ready within timeout, continuing with fallback mode', {
error: error instanceof Error ? error.message : String(error),
timeout
});
throw new Error(`BatchProcessor initialization failed: ${error instanceof Error ? error.message : String(error)}`);
// Don't throw - mark as ready anyway and let cache operations use their fallback mechanisms
this.isReady = true;
}
}
/**
* Check if the batch processor is ready
*/
getReadyStatus(): boolean {
return this.isReady && this.cacheProvider.isReady();
return this.isReady; // Don't require cache to be ready, let individual operations handle fallbacks
}
/**
* Generate a unique key for storing batch payload in Redis
@ -77,17 +77,11 @@ export class BatchProcessor {
return `payload:${jobNamePrefix}:${batchIndex}:${Date.now()}`;
}/**
* Store batch payload in Redis and return the key
*/
private async storeBatchPayload<T>(
*/ private async storeBatchPayload<T>(
items: T[],
config: BatchConfig<T>,
batchIndex: number
): Promise<string> {
// Ensure cache is ready before storing
if (!this.cacheProvider.isReady()) {
throw new Error('Cache provider not ready - cannot store batch payload');
}
const payloadKey = this.generatePayloadKey(config.jobNamePrefix, batchIndex);
const payload = {
items,
@ -101,21 +95,29 @@ export class BatchProcessor {
const ttlSeconds = (config.payloadTtlHours || 24) * 60 * 60;
await this.cacheProvider.set(
payloadKey,
JSON.stringify(payload),
ttlSeconds
);
try {
await this.cacheProvider.set(
payloadKey,
JSON.stringify(payload),
ttlSeconds
);
logger.info('Stored batch payload in Redis', {
payloadKey,
itemCount: items.length,
batchIndex,
ttlHours: config.payloadTtlHours || 24
});
logger.info('Stored batch payload in Redis', {
payloadKey,
itemCount: items.length,
batchIndex,
ttlHours: config.payloadTtlHours || 24
});
} catch (error) {
logger.error('Failed to store batch payload, job will run without caching', {
payloadKey,
error: error instanceof Error ? error.message : String(error)
});
// Don't throw - the job can still run, just without the cached payload
}
return payloadKey;
} /**
}/**
* Load batch payload from Redis
*/
private async loadBatchPayload<T>(payloadKey: string): Promise<{
@ -183,11 +185,16 @@ export class BatchProcessor {
if (items.length === 0) {
return { totalItems: 0, jobsCreated: 0 };
}
// Final readiness check
} // Final readiness check - wait briefly for cache to be ready
if (!this.cacheProvider.isReady()) {
throw new Error('Cache provider is not ready - cannot process items');
logger.warn('Cache provider not ready, waiting briefly...');
try {
await this.cacheProvider.waitForReady(10000); // Wait up to 10 seconds
logger.info('Cache provider became ready');
} catch (error) {
logger.warn('Cache provider still not ready, continuing with fallback mode');
// Don't throw error - let the cache operations use their fallback mechanisms
}
}
logger.info('Starting item processing', {
@ -324,7 +331,7 @@ export class BatchProcessor {
operation: `process-${jobNamePrefix}-batch`,
payload: {
// Optimized: only store reference and metadata
payloadKey: this.keyPrefix + payloadKey,
payloadKey: payloadKey,
batchIndex,
total: totalBatches,
itemCount: batchItems.length,