|
| 1 | +import { BatchProcessor, Queue, QueueGroup, QueueObservable } from '../src' |
| 2 | + |
| 3 | +async function main() { |
| 4 | + console.log('🚀 Advanced Features Example - Groups, Observables, and Batches') |
| 5 | + |
| 6 | + // Create a few queues |
| 7 | + const emailQueue = new Queue<{ to: string, subject: string, body: string }>('email', { |
| 8 | + redis: { url: 'redis://localhost:6379' }, |
| 9 | + }) |
| 10 | + |
| 11 | + const notificationQueue = new Queue<{ userId: string, message: string }>('notification', { |
| 12 | + redis: { url: 'redis://localhost:6379' }, |
| 13 | + }) |
| 14 | + |
| 15 | + const imageProcessingQueue = new Queue<{ imageUrl: string, filters: string[] }>('image-processing', { |
| 16 | + redis: { url: 'redis://localhost:6379' }, |
| 17 | + }) |
| 18 | + |
| 19 | + console.log('✅ Queues created') |
| 20 | + |
| 21 | + // ======================== |
| 22 | + // Example 1: Batch Processing |
| 23 | + // ======================== |
| 24 | + console.log('\n📦 Example 1: Batch Processing') |
| 25 | + |
| 26 | + // Create a batch processor for the email queue |
| 27 | + const batchProcessor = new BatchProcessor(emailQueue) |
| 28 | + |
| 29 | + // Create a batch of email jobs |
| 30 | + const emailJobs = [ |
| 31 | + { to: 'user1@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' }, |
| 32 | + { to: 'user2@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' }, |
| 33 | + { to: 'user3@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' }, |
| 34 | + ] |
| 35 | + |
| 36 | + const batch = await batchProcessor.createBatch(emailJobs, { |
| 37 | + attempts: 3, |
| 38 | + backoff: { |
| 39 | + type: 'exponential', |
| 40 | + delay: 1000, |
| 41 | + }, |
| 42 | + }) |
| 43 | + |
| 44 | + console.log(`📨 Created batch ${batch.id} with ${batch.jobs.length} email jobs`) |
| 45 | + |
| 46 | + // Process the batch |
| 47 | + await batchProcessor.processBatch(batch.id, async (jobs) => { |
| 48 | + console.log(`🔄 Processing batch of ${jobs.length} emails together`) |
| 49 | + |
| 50 | + // Update progress |
| 51 | + await batchProcessor.setBatchProgress(batch.id, 50) |
| 52 | + |
| 53 | + // Simulate batch processing (in a real scenario, you might use a bulk email API) |
| 54 | + return jobs.map((job) => { |
| 55 | + console.log(`📧 Would send email to ${job.data.to} with subject "${job.data.subject}"`) |
| 56 | + return { sent: true, to: job.data.to } |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + console.log('✅ Batch processing complete') |
| 61 | + |
| 62 | + // ======================== |
| 63 | + // Example 2: Queue Groups |
| 64 | + // ======================== |
| 65 | + console.log('\n👥 Example 2: Queue Groups') |
| 66 | + |
| 67 | + // Create a queue group |
| 68 | + const queueGroup = new QueueGroup('app', emailQueue.redisClient) |
| 69 | + |
| 70 | + // Add queues to the group |
| 71 | + await queueGroup.addQueue(emailQueue, { name: 'notifications', maxConcurrency: 5 }) |
| 72 | + await queueGroup.addQueue(notificationQueue, { name: 'notifications', maxConcurrency: 5 }) |
| 73 | + |
| 74 | + // Add job to all queues in the group |
| 75 | + const groupJobs = await queueGroup.addJobToGroup('notifications', { |
| 76 | + to: 'all-users@example.com', |
| 77 | + subject: 'System Maintenance', |
| 78 | + body: 'Our system will be down for maintenance tonight.', |
| 79 | + } as any) |
| 80 | + |
| 81 | + console.log(`📝 Added job to all queues in the 'notifications' group, created ${groupJobs.length} jobs`) |
| 82 | + |
| 83 | + // Process jobs in the group (using imported Job type from src) |
| 84 | + await queueGroup.processGroup<any>('notifications', async (job) => { |
| 85 | + if (job.name === 'email') { |
| 86 | + console.log(`📧 Processing email job: ${job.data.subject} to ${job.data.to}`) |
| 87 | + return { sent: true } |
| 88 | + } |
| 89 | + else if (job.name === 'notification') { |
| 90 | + console.log(`🔔 Processing notification job: ${job.data.message} to ${job.data.userId}`) |
| 91 | + return { delivered: true } |
| 92 | + } |
| 93 | + return null |
| 94 | + }) |
| 95 | + |
| 96 | + // ======================== |
| 97 | + // Example 3: Observables |
| 98 | + // ======================== |
| 99 | + console.log('\n👁️ Example 3: Observables') |
| 100 | + |
| 101 | + // Create an observable for all queues |
| 102 | + const observable = new QueueObservable('app', emailQueue.redisClient) |
| 103 | + |
| 104 | + // Create an observable that monitors all queues |
| 105 | + const queueObservable = await observable.createObservable( |
| 106 | + [emailQueue, notificationQueue, imageProcessingQueue], |
| 107 | + { interval: 2000, autoStart: true }, |
| 108 | + ) |
| 109 | + |
| 110 | + console.log(`👀 Created observable ${queueObservable.id} monitoring ${queueObservable.queues.length} queues`) |
| 111 | + |
| 112 | + // Wait a moment for the first stats to be collected |
| 113 | + await new Promise(resolve => setTimeout(resolve, 3000)) |
| 114 | + |
| 115 | + // Get stats |
| 116 | + const stats = await observable.getObservableStats(queueObservable.id) |
| 117 | + console.log('📊 Queue stats:', stats) |
| 118 | + |
| 119 | + // Stop the observable |
| 120 | + await observable.stopObservable(queueObservable.id) |
| 121 | + console.log(`⏹️ Stopped observable ${queueObservable.id}`) |
| 122 | + |
| 123 | + // Close everything |
| 124 | + console.log('\n🧹 Cleaning up...') |
| 125 | + await emailQueue.close() |
| 126 | + await notificationQueue.close() |
| 127 | + await imageProcessingQueue.close() |
| 128 | + await queueGroup.closeAll() |
| 129 | + await observable.closeAll() |
| 130 | + |
| 131 | + console.log('✅ Done!') |
| 132 | +} |
| 133 | + |
| 134 | +main().catch(console.error) |
0 commit comments