How do I handle timezones in cron jobs?
Specify timezone in cron.schedule options: cron.schedule('0 9 * * *', fn, { timezone: 'Asia/Kolkata' }). For per-user timezones, run hourly and check which users' local time is 9 AM. This ensures users receive notifications at their local time, not the server's time.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Cron Jobs in Production Best Practices for Reliable Scheduled Tasks
Track job execution in a database. Before running, check if the job already ran for this period (e.g., DigestLog.findOne({ date: today })). If it exists, skip. After running, create a record. This prevents duplicate emails or data processing if the job runs twice.
Use a distributed lock with Redis: redis.set('lockKey', 'locked', 'NX', 'EX', 300). If another instance already acquired the lock, skip the job. Release the lock with redis.del('lockKey') after completion. The TTL ensures the lock is released if the instance crashes.
Use Promise.race with the job function and a timeout promise: Promise.race([jobFn(), new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 300000))]). This prevents hung jobs from running indefinitely. Set appropriate timeouts based on the job's expected duration.
Still have questions?
Browse all our FAQs or reach out to our support team
