Skip to content

Fix the time calculation problem caused by start_time #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ def from_schedule(cls, schedule):
except MultipleObjectsReturned:
return cls.objects.filter(**spec).first()

def due_start_time(self, initial_start_time, tz):
start_time = initial_start_time.astimezone(tz)
start, ends_in, now = self.schedule.remaining_delta(start_time)
return start + ends_in


class PeriodicTasks(models.Model):
"""Helper table for tracking updates to periodic tasks.
Expand Down Expand Up @@ -663,3 +668,9 @@ def scheduler(self):
@property
def schedule(self):
return self.scheduler.schedule

def due_start_time(self, tz):
if self.crontab:
return self.crontab.due_start_time(self.start_time, tz)
else:
return self.start_time
8 changes: 5 additions & 3 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ def is_due(self):
if now < self.model.start_time:
# The datetime is before the start date - don't run.
# send a delay to retry on start_time
delay = math.ceil(
(self.model.start_time - now).total_seconds()
)
current_tz = now.tzinfo
start_time = self.model.due_start_time(current_tz)
time_remaining = start_time - now
delay = math.ceil(time_remaining.total_seconds())

return schedules.schedstate(False, delay)

# EXPIRED TASK: Disable task when expired
Expand Down
12 changes: 9 additions & 3 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pytest-django>=4.5.2,<5.0
pytest>=6.2.5,<9.0
pytest-timeout
# Base dependencies (common for all Python versions)
ephem
pytest-timeout

# Conditional dependencies
pytest>=6.2.5,<8.0; python_version < '3.9' # Python 3.8 only
pytest>=6.2.5,<9.0; python_version >= '3.9' # Python 3.9+ only
pytest-django>=4.5.2,<4.6.0; python_version < '3.9' # Python 3.8 only
pytest-django>=4.5.2,<5.0; python_version >= '3.9' # Python 3.9+ only
backports.zoneinfo; python_version < '3.9' # Python 3.8 only
122 changes: 122 additions & 0 deletions t/unit/test_schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from itertools import count
from time import monotonic

try:
from zoneinfo import ZoneInfo # Python 3.9+
except ImportError:
from backports.zoneinfo import ZoneInfo # Python 3.8

import pytest
from celery.schedules import crontab, schedule, solar
from django.contrib.admin.sites import AdminSite
Expand Down Expand Up @@ -777,6 +782,123 @@ def test_starttime_trigger(self, monkeypatch):
assert s._heap[0]
assert s._heap[0][2].name == m1.name

def test_crontab_with_start_time_between_now_and_crontab(self, app):
now = app.now()
delay_minutes = 2

test_start_time = now + timedelta(minutes=delay_minutes)

crontab_time = test_start_time + timedelta(minutes=delay_minutes)

task = self.create_model_crontab(
crontab(minute=f'{crontab_time.minute}'),
start_time=test_start_time)

entry = EntryTrackSave(task, app=app)

is_due, next_check = entry.is_due()

expected_delay = 2 * delay_minutes * 60

assert not is_due
assert next_check == pytest.approx(expected_delay, abs=60)

def test_crontab_with_start_time_after_crontab(self, app):
now = app.now()

delay_minutes = 2

crontab_time = now + timedelta(minutes=delay_minutes)

test_start_time = crontab_time + timedelta(minutes=delay_minutes)

task = self.create_model_crontab(
crontab(minute=f'{crontab_time.minute}'),
start_time=test_start_time)

entry = EntryTrackSave(task, app=app)

is_due, next_check = entry.is_due()

expected_delay = delay_minutes * 60 + 3600

assert not is_due
assert next_check == pytest.approx(expected_delay, abs=60)

def test_crontab_with_start_time_different_time_zone(self, app):
now = app.now()

delay_minutes = 2

test_start_time = now + timedelta(minutes=delay_minutes)

crontab_time = test_start_time + timedelta(minutes=delay_minutes)

tz = ZoneInfo('Asia/Shanghai')
test_start_time = test_start_time.astimezone(tz)

task = self.create_model_crontab(
crontab(minute=f'{crontab_time.minute}'),
start_time=test_start_time)

entry = EntryTrackSave(task, app=app)

is_due, next_check = entry.is_due()

expected_delay = 2 * delay_minutes * 60

assert not is_due
assert next_check == pytest.approx(expected_delay, abs=60)

now = app.now()

crontab_time = now + timedelta(minutes=delay_minutes)

test_start_time = crontab_time + timedelta(minutes=delay_minutes)

tz = ZoneInfo('Asia/Shanghai')
test_start_time = test_start_time.astimezone(tz)

task = self.create_model_crontab(
crontab(minute=f'{crontab_time.minute}'),
start_time=test_start_time)

entry = EntryTrackSave(task, app=app)

is_due, next_check = entry.is_due()

expected_delay = delay_minutes * 60 + 3600

assert not is_due
assert next_check == pytest.approx(expected_delay, abs=60)

def test_crontab_with_start_time_tick(self, app):
PeriodicTask.objects.all().delete()
s = self.Scheduler(app=self.app)
assert not s._heap

m1 = self.create_model_interval(schedule(timedelta(seconds=3)))
m1.save()

now = timezone.now()
start_time = now + timedelta(minutes=1)
crontab_trigger_time = now + timedelta(minutes=2)

m2 = self.create_model_crontab(
crontab(minute=f'{crontab_trigger_time.minute}'),
start_time=start_time)
m2.save()

e2 = EntryTrackSave(m2, app=self.app)
is_due, _ = e2.is_due()

max_iterations = 1000
iterations = 0
while (not is_due and iterations < max_iterations):
s.tick()
assert s._heap[0][2].name != m2.name
is_due, _ = e2.is_due()


@pytest.mark.django_db
class test_models(SchedulerCase):
Expand Down