|
| 1 | +--- |
| 2 | +title: 'Scheduled Jobs' |
| 3 | +source: 'https://github.com/NeaByteLab/DEEP-NOTES' |
| 4 | +description: 'Periodic task execution with schedulers, resource efficiency, and operational best practices.' |
| 5 | +tags: |
| 6 | + - 'job management' |
| 7 | + - 'background processing' |
| 8 | + - 'system design' |
| 9 | + - 'automation' |
| 10 | +--- |
| 11 | + |
| 12 | +# Scheduled Jobs |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +This note covers **scheduled jobs**: tasks that execute at specific times or intervals, managed by a scheduler (cron, job queue, cloud scheduler). They provide resource efficiency and predictable execution patterns. **Goal:** understand when scheduled jobs are optimal and how to implement them reliably. |
| 17 | + |
| 18 | +## Definition |
| 19 | + |
| 20 | +**Scheduled Job**: A task that runs at predetermined times or intervals, managed by an external scheduler rather than running continuously. |
| 21 | + |
| 22 | +- Process starts, executes, then terminates. |
| 23 | +- Resource consumption only during execution. |
| 24 | +- Execution timing is predictable and configurable. |
| 25 | + |
| 26 | +## The Analogy |
| 27 | + |
| 28 | +**On-call security specialist.** They only show up for specific patrols or scheduled checks. No continuous presence, no constant resource consumption, but reliable and predictable coverage when needed. |
| 29 | + |
| 30 | +## When You See It |
| 31 | + |
| 32 | +**Use scheduled jobs for:** |
| 33 | + |
| 34 | +- Batch processing (data ETL, report generation) |
| 35 | +- Periodic maintenance (cleanup, backups, health checks) |
| 36 | +- Resource-intensive tasks that can be deferred |
| 37 | +- API polling or external data sync |
| 38 | +- Anything with predictable timing requirements |
| 39 | +- Cost-sensitive operations where continuous resource use is wasteful |
| 40 | + |
| 41 | +## Examples |
| 42 | + |
| 43 | +**Good: Daily data sync** |
| 44 | + |
| 45 | +```typescript |
| 46 | +// NOTE: Market data sync runs once daily at 2 AM. |
| 47 | +// No need for continuous polling. |
| 48 | +const dailySync = new CronJob('0 2 * * *', async () => { |
| 49 | + await syncMarketData() |
| 50 | + await updateAnalytics() |
| 51 | + console.log('Daily sync completed') |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +**Good: Weekly cleanup** |
| 56 | + |
| 57 | +```typescript |
| 58 | +// NOTE: Cleanup old logs weekly on Sunday at 3 AM. |
| 59 | +// Resource-intensive but not time-sensitive. |
| 60 | +const cleanupJob = new CronJob('0 3 * * 0', async () => { |
| 61 | + await cleanupOldLogs() |
| 62 | + await archiveReports() |
| 63 | + await freeDiskSpace() |
| 64 | +}) |
| 65 | +``` |
| 66 | + |
| 67 | +**Good: Hourly API polling** |
| 68 | + |
| 69 | +```typescript |
| 70 | +// NOTE: External API updates hourly. |
| 71 | +// Predictable interval, no need for continuous polling. |
| 72 | +const apiPolling = new CronJob('0 * * * *', async () => { |
| 73 | + const data = await fetchExternalAPI() |
| 74 | + await processData(data) |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +**Bad: Real-time message processing** |
| 79 | + |
| 80 | +```typescript |
| 81 | +// NOTE: Delay unacceptable for real-time chat. |
| 82 | +// Messages would wait up to 1 minute. |
| 83 | +const messageProcessing = new CronJob('* * * * *', processMessages) |
| 84 | +``` |
| 85 | + |
| 86 | +## Important Points |
| 87 | + |
| 88 | +- **Resource efficiency**: Excellent - resources used only during execution. |
| 89 | +- **Monitoring**: Clear success/failure metrics per execution; easy to track. |
| 90 | +- **Scalability**: Multiple instances can run safely; scheduler handles coordination. |
| 91 | +- **Error isolation**: Failed execution doesn't affect next scheduled run. |
| 92 | +- **Testing**: Can be triggered manually for testing and debugging. |
| 93 | +- **Reliability**: Built-in retry mechanisms and failure notifications. |
| 94 | +- **Cost**: Lower operational costs due to intermittent resource usage. |
| 95 | + |
| 96 | +## Summary |
| 97 | + |
| 98 | +- **Scheduled jobs**: batch processing, periodic tasks, resource efficiency preferred. |
| 99 | +- **Analogy**: On-call security specialist - appears when needed, no constant presence. |
| 100 | +- **Use when**: timing is predictable and immediate response isn't required. |
| 101 | + |
| 102 | +_Scheduled jobs trade immediate response for resource efficiency. Use them when predictability matters more than latency._ |
0 commit comments