django-q icon indicating copy to clipboard operation
django-q copied to clipboard

Schedules with 'schedule_type=DAILY' do not account for DST changes

Open SebastianRehfeldt opened this issue 3 years ago • 0 comments

Scenario

We want to schedule a periodic task which runs every day at 12:00 in Europe/Berlin timezone.

Configuration & Problem Description

We have configured the TIME_ZONE to Europe/Berlin in our settings. The next_run correctly shows 10 UTC in the database in summer (Berlin offset is 2 hours in summer). When changing from dst to winter time the offset changes to 1 hour. However, the scheduled task is still timed at 10 UTC. Therefore the cluster did not account for the dst change when updating the next_run after task execution. From the change on, the tasks were timed an hour too early.

Potential bug in cluster.py::scheduler

According to my current understandingm the scheduler function in cluster.py is responsible for setting the next_run after task execution. It gets the current next_run of the scheduled task, uses arrow to compute the next timestamp for execution and later stores the updated schedule.

AFAIK the next_run property here will not be localized to the timezone defined in the settings but will still contain UTC as timezone information. Given that there is no offset in UTC timezone, arrow simply adds 24 hours and therefore does not account for the dst change.

Potential solution

A potential solution would be to localize the next_run according to the timezone in the settings. arrow will then correctly increment for the next_run based on the specified timezone.

from django.utils.timezone import localtime

localtime(schedule.next_run)

SebastianRehfeldt avatar Nov 25 '21 13:11 SebastianRehfeldt