#!/usr/bin/env bun /** * Debug script to test batch cleanup issue */ import { initializeBatchCache, processItems, QueueManager } from './src'; async function debugBatchCleanup() { console.log('🔍 Debugging batch cleanup...'); const queueManager = new QueueManager({ queueName: 'debug-cleanup-queue', workers: 1, concurrency: 2, }); // Register a simple test provider queueManager.registerProvider('test', { 'process-item': async payload => { console.log(`🔄 Processing item: ${JSON.stringify(payload)}`); await new Promise(resolve => setTimeout(resolve, 100)); return { processed: true, item: payload }; }, }); await queueManager.initialize(); await initializeBatchCache(queueManager); // Test data const items = Array.from({ length: 7 }, (_, i) => ({ id: i + 1, data: `item-${i + 1}`, })); console.log(`📦 Processing ${items.length} items in batches of 3...`); // Process in batches const result = await processItems(items, queueManager, { totalDelayHours: 0.0028, // 10 seconds useBatching: true, batchSize: 3, // This will create 3 batches: [3,3,1] priority: 1, provider: 'test', operation: 'process-item', removeOnComplete: 2, // Keep only 2 completed jobs removeOnFail: 2, }); console.log('📊 Processing result:', result); // Monitor queue and cache cleanup let iterations = 0; const monitor = setInterval(async () => { iterations++; const stats = await queueManager.getStats(); console.log(`📈 [${iterations}] Queue stats:`, { waiting: stats.waiting, active: stats.active, completed: stats.completed, failed: stats.failed, }); // Check if any jobs are stuck if (iterations > 20) { console.log('❌ Timeout reached, stopping monitor'); clearInterval(monitor); await queueManager.shutdown(); } if (stats.waiting === 0 && stats.active === 0) { console.log('✅ All jobs completed'); clearInterval(monitor); // Wait a bit more to see final cleanup setTimeout(async () => { const finalStats = await queueManager.getStats(); console.log('📊 Final stats:', finalStats); await queueManager.shutdown(); }, 2000); } }, 1000); } if (require.main === module) { debugBatchCleanup().catch(console.error); }