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
87
libs/queue/examples/basic-usage.ts
Normal file
87
libs/queue/examples/basic-usage.ts
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { initializeBatchCache, processItems, QueueManager } from '@stock-bot/queue';
|
||||
|
||||
async function basicUsageExample() {
|
||||
console.log('=== Basic Queue Usage Example ===');
|
||||
|
||||
// 1. Initialize queue manager
|
||||
const queueManager = new QueueManager({
|
||||
queueName: 'example-queue',
|
||||
workers: 3,
|
||||
concurrency: 10,
|
||||
redis: {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
},
|
||||
});
|
||||
|
||||
// 2. Register providers
|
||||
queueManager.registerProvider('market-data', {
|
||||
'fetch-price': async payload => {
|
||||
// payload is now the raw symbol string
|
||||
console.log(`Fetching price for ${payload}`);
|
||||
// Simulate API call
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
return {
|
||||
symbol: payload,
|
||||
price: Math.random() * 1000,
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
},
|
||||
|
||||
'update-cache': async payload => {
|
||||
// payload is now the raw symbol string
|
||||
console.log(`Updating cache for ${payload}`);
|
||||
// Simulate cache update
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
return { success: true, symbol: payload };
|
||||
},
|
||||
});
|
||||
|
||||
// 3. Initialize
|
||||
await queueManager.initialize();
|
||||
await initializeBatchCache(queueManager);
|
||||
|
||||
// 4. Add individual jobs
|
||||
console.log('Adding individual jobs...');
|
||||
await queueManager.add('fetch-price', {
|
||||
provider: 'market-data',
|
||||
operation: 'fetch-price',
|
||||
payload: 'AAPL', // Direct symbol instead of wrapped object
|
||||
});
|
||||
|
||||
// 5. Process items in batch
|
||||
console.log('Processing items in batch...');
|
||||
const symbols = ['GOOGL', 'MSFT', 'TSLA', 'AMZN'];
|
||||
|
||||
const result = await processItems(symbols, queueManager, {
|
||||
totalDelayMs: 30000, // 30 seconds total
|
||||
useBatching: true,
|
||||
batchSize: 2,
|
||||
priority: 1,
|
||||
provider: 'market-data',
|
||||
operation: 'fetch-price',
|
||||
});
|
||||
|
||||
console.log('Batch processing result:', result);
|
||||
|
||||
// 6. Get queue statistics
|
||||
const stats = await queueManager.getStats();
|
||||
console.log('Queue stats:', stats);
|
||||
|
||||
// 7. Clean up old jobs
|
||||
await queueManager.clean(60000); // Clean jobs older than 1 minute
|
||||
|
||||
// 8. Shutdown gracefully
|
||||
setTimeout(async () => {
|
||||
console.log('Shutting down...');
|
||||
await queueManager.shutdown();
|
||||
console.log('Shutdown complete');
|
||||
}, 35000);
|
||||
}
|
||||
|
||||
// Run the example
|
||||
if (require.main === module) {
|
||||
basicUsageExample().catch(console.error);
|
||||
}
|
||||
|
||||
export { basicUsageExample };
|
||||
Loading…
Add table
Add a link
Reference in a new issue