Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Your Nightly Job Ran Twice on the DST Switch: Making Scheduled Jobs Timezone-Safe

说明与用途

在 America/New_York 时区,计划在 01:30 运行的作业在夏令时结束时执行两次,导致重复发送邮件。解决方案是将触发时间(UTC 时刻)与业务日期分离,先转换为 UTC 再去重,并在执行前声明日期。

问题

Scheduled jobs that use local wall‑clock times fire twice during the fall‑back DST transition and never during the spring‑forward transition, causing duplicate or missing work.

结果

The job now runs exactly once per business day, eliminating duplicate billing emails and ensuring no missed runs during DST transitions.

工作流

  • 1. Configure scheduler to use UTC or a timezone‑aware cron (e.g., CRON_TZ=America/New_York).
  • 2. In the job code, compute the logical date for the current run using a helper that resolves wall‑clock times to a single UTC instant (first occurrence for ambiguous times, the jump instant for nonexistent times).
  • 3. Before performing work, check if the logical date has already been claimed (e.g., via a database flag or a unique constraint).
  • 4. If not claimed, claim the date and proceed; otherwise skip the run.
  • 5. Store all timestamps in UTC for comparison and logging.

证据

Python’s zoneinfo makes the ambiguity visible through PEP 495’s fold flag… Convert to UTC before comparing and the check works.

来源

Scheduler-native timezone support is real and worth using, but it only decides when to fire… none of these platforms promise exactly‑once.

来源

  • 记录日期: 2026-09-15T21:15:57.838Z