stock-bot/libs/queue/examples/basic-usage.ts

87 lines
2.3 KiB
TypeScript

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, {
totalDelayHours: 0.0083, // 30 seconds
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 };