|
| 1 | +import { Queue } from '../src' |
| 2 | + |
| 3 | +interface NotificationData { |
| 4 | + title: string |
| 5 | + message: string |
| 6 | + recipients: string[] |
| 7 | + type: 'email' | 'push' | 'sms' |
| 8 | +} |
| 9 | + |
| 10 | +async function main() { |
| 11 | + console.log('🕒 Cron Jobs Example') |
| 12 | + |
| 13 | + // Create a queue for notifications |
| 14 | + const notificationQueue = new Queue<NotificationData>('notifications', { |
| 15 | + verbose: true, |
| 16 | + logLevel: 'info', |
| 17 | + }) |
| 18 | + |
| 19 | + console.log('✅ Queue created') |
| 20 | + |
| 21 | + // Process notifications |
| 22 | + notificationQueue.process(5, async (job) => { |
| 23 | + const { title, message, recipients, type } = job.data |
| 24 | + console.log(`📨 Processing ${type} notification "${title}" to ${recipients.length} recipients`) |
| 25 | + |
| 26 | + // Simulate notification sending |
| 27 | + await new Promise(resolve => setTimeout(resolve, 200)) |
| 28 | + |
| 29 | + return { success: true, sentAt: new Date(), recipientCount: recipients.length } |
| 30 | + }) |
| 31 | + |
| 32 | + console.log('\n📅 Scheduling cron jobs with different expressions:') |
| 33 | + |
| 34 | + // Example 1: Run every minute - useful for testing |
| 35 | + const everyMinuteId = await notificationQueue.scheduleCron({ |
| 36 | + cronExpression: '* * * * *', // Every minute |
| 37 | + data: { |
| 38 | + title: 'Server Status', |
| 39 | + message: 'All systems operational', |
| 40 | + recipients: ['admin@example.com'], |
| 41 | + type: 'email' |
| 42 | + }, |
| 43 | + jobId: 'status-check-minute', |
| 44 | + // Will stop after 5 executions |
| 45 | + limit: 5 |
| 46 | + }) |
| 47 | + console.log(` - Every minute status check scheduled (ID: ${everyMinuteId})`) |
| 48 | + |
| 49 | + // Example 2: Hourly job with timezone |
| 50 | + const hourlyJobId = await notificationQueue.scheduleCron({ |
| 51 | + cronExpression: '0 * * * *', // At minute 0 of every hour |
| 52 | + timezone: 'America/New_York', // Eastern Time |
| 53 | + data: { |
| 54 | + title: 'Hourly Update', |
| 55 | + message: 'This is your hourly system update', |
| 56 | + recipients: ['team@example.com'], |
| 57 | + type: 'push' |
| 58 | + }, |
| 59 | + jobId: 'hourly-update' |
| 60 | + }) |
| 61 | + console.log(` - Hourly update scheduled in Eastern Time (ID: ${hourlyJobId})`) |
| 62 | + |
| 63 | + // Example 3: Daily job at specific time |
| 64 | + const dailyJobId = await notificationQueue.scheduleCron({ |
| 65 | + cronExpression: '30 9 * * *', // Every day at 9:30am |
| 66 | + timezone: 'Europe/London', // London time |
| 67 | + data: { |
| 68 | + title: 'Daily Report', |
| 69 | + message: 'Here is your daily activity report', |
| 70 | + recipients: ['manager@example.com'], |
| 71 | + type: 'email' |
| 72 | + }, |
| 73 | + jobId: 'daily-report' |
| 74 | + }) |
| 75 | + console.log(` - Daily report scheduled for 9:30am London time (ID: ${dailyJobId})`) |
| 76 | + |
| 77 | + // Example 4: Weekday job (Monday through Friday) |
| 78 | + const weekdayJobId = await notificationQueue.scheduleCron({ |
| 79 | + cronExpression: '0 8 * * 1-5', // At 8:00am, Monday through Friday |
| 80 | + data: { |
| 81 | + title: 'Morning Briefing', |
| 82 | + message: 'Your tasks for today', |
| 83 | + recipients: ['staff@example.com'], |
| 84 | + type: 'sms' |
| 85 | + }, |
| 86 | + jobId: 'weekday-briefing' |
| 87 | + }) |
| 88 | + console.log(` - Weekday briefing scheduled for 8:00am Mon-Fri (ID: ${weekdayJobId})`) |
| 89 | + |
| 90 | + // Example 5: Complex schedule (first Monday of the month) |
| 91 | + const monthlyJobId = await notificationQueue.scheduleCron({ |
| 92 | + cronExpression: '0 12 1-7 * 1', // At 12:00pm on Monday in the first week of the month |
| 93 | + data: { |
| 94 | + title: 'Monthly Review', |
| 95 | + message: 'Time for our monthly performance review', |
| 96 | + recipients: ['executives@example.com'], |
| 97 | + type: 'email' |
| 98 | + }, |
| 99 | + jobId: 'monthly-review' |
| 100 | + }) |
| 101 | + console.log(` - Monthly review scheduled for first Monday of each month (ID: ${monthlyJobId})`) |
| 102 | + |
| 103 | + // Show when the next few minutes of jobs will run |
| 104 | + console.log('\n⏰ Demonstrating minute-by-minute execution for a short period:') |
| 105 | + console.log(' (The every-minute job will run several times)') |
| 106 | + |
| 107 | + // Wait for several minutes to see some executions |
| 108 | + await new Promise(resolve => setTimeout(resolve, 180000)) // 3 minutes |
| 109 | + |
| 110 | + // Unschedule one of the jobs to demonstrate cancellation |
| 111 | + console.log('\n❌ Unscheduling the every-minute job') |
| 112 | + const unscheduled = await notificationQueue.unscheduleCron(everyMinuteId) |
| 113 | + console.log(` Job ${everyMinuteId} ${unscheduled ? 'successfully unscheduled' : 'failed to unschedule'}`) |
| 114 | + |
| 115 | + // Show the remaining scheduled jobs |
| 116 | + console.log('\n📝 The following jobs remain scheduled:') |
| 117 | + console.log(` - Hourly update (ID: ${hourlyJobId})`) |
| 118 | + console.log(` - Daily report (ID: ${dailyJobId})`) |
| 119 | + console.log(` - Weekday briefing (ID: ${weekdayJobId})`) |
| 120 | + console.log(` - Monthly review (ID: ${monthlyJobId})`) |
| 121 | + |
| 122 | + console.log('\n👋 Example completed. In a real application, these would continue running.') |
| 123 | + console.log(' Closing the queue for the example.') |
| 124 | + |
| 125 | + // In a real application, you might keep the queue running forever |
| 126 | + await notificationQueue.close() |
| 127 | +} |
| 128 | + |
| 129 | +main().catch(error => { |
| 130 | + console.error('Error in example:', error) |
| 131 | + process.exit(1) |
| 132 | +}) |
0 commit comments