added new queue lib with batch processor and provider
This commit is contained in:
parent
ddcf94a587
commit
6c548416d1
19 changed files with 1939 additions and 35 deletions
85
libs/queue/debug-batch-cleanup.ts
Normal file
85
libs/queue/debug-batch-cleanup.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#!/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, {
|
||||
totalDelayMs: 10000, // 10 seconds total
|
||||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue