Background Jobs (Cron)
The Background Jobs component provides a standardized way to schedule and manage recurring tasks in your Servercn application using node-cron.
Installation Guide
Install the component using the servercn CLI:
npx servercn-cli add cron-jobImplementation
1. Define a Job
Create individual job files for different tasks to keep your code modular and readable.
2. Initialize Jobs
Export a central initialization function to start all your jobs at once.
3. Usage in Server
Import and call the initJobs function in your main entry file (usually server.ts).
Cron Syntax Refresher
Real-World Examples
Here are some common scenarios where background jobs are used in production-grade Express applications.
1. Expired Refresh Tokens Cleanup
2. Daily Database Cleanup
Remove unverified users who haven't verified their email within 24 hours.
3. System Health Heartbeat
Send a heartbeat or log system performance metrics every 15 minutes.
4. Syncing Exchange Rates
Fetch and cache currency exchange rates from an external API every hour.