Skip to content

New behavior exclude specific crontab hours - It uses django's timezone rather than serverΒ #885

@iwalucas

Description

@iwalucas

There's an issue in django-celery-beat's DatabaseScheduler class where timezone calculations lead to incorrect task scheduling when the Celery timezone differs from the server timezone.

In our project, we have:

Server running in UTC
CELERY_TIMEZONE = "EST" in settings
Tasks scheduled using crontab hour values

Problem
The _get_timezone_offset method in schedulers.py gets the wrong server timezone information. It uses timezone.get_current_timezone() which returns a timezone-aware datetime, but then incorrectly interprets the timezone from it.

This leads to the scheduler making incorrect decisions about which tasks to run because the timezone offset calculation is wrong. Tasks scheduled in different timezones don't run at the expected times.

Expected Behavior
The scheduler should correctly calculate the timezone offset between the server timezone and each task's timezone, accounting for the Celery timezone setting.

Steps to Reproduce
Set CELERY_TIMEZONE = "EST" in Django settings
Run the server on a machine with UTC timezone
Schedule a task with a crontab for a specific hour
Observe that the task wont run at the defined time

Suggested Fix
The _get_timezone_offset method should account for the server timezone. This is how I fixed:

        server_time = aware_now()
        server_tz = ZoneInfo(str(server_time.tzinfo))

Environment
django-celery-beat version: 2.8.0
Django version: 5.2
Python version: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions