Skip to content

Commit 5ac4c5c

Browse files
author
Shaheed Haque
committed
Size CrontabSchedule fields for max length values.
1 parent feaf34e commit 5ac4c5c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

django_celery_beat/models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,20 @@ def period_singular(self):
142142
class CrontabSchedule(models.Model):
143143
"""Crontab-like schedule."""
144144

145-
minute = models.CharField(_('minute'), max_length=64, default='*')
146-
hour = models.CharField(_('hour'), max_length=64, default='*')
145+
#
146+
# The worst case scenario for day of month is a list of all 31 day numbers
147+
# '[1, 2, ..., 31]' which has a length of 115. Likewise, minute can be
148+
# 0..59 and hour can be 0..23. Ensure we can accomodate these by allowing
149+
# 4 chars for each value (what we save on 0-9 accomodates the []).
150+
# We leave the other fields at their historical length.
151+
#
152+
minute = models.CharField(_('minute'), max_length=60 * 4, default='*')
153+
hour = models.CharField(_('hour'), max_length=24 * 4, default='*')
147154
day_of_week = models.CharField(
148155
_('day of week'), max_length=64, default='*',
149156
)
150157
day_of_month = models.CharField(
151-
_('day of month'), max_length=64, default='*',
158+
_('day of month'), max_length=31 * 4, default='*',
152159
)
153160
month_of_year = models.CharField(
154161
_('month of year'), max_length=64, default='*',

t/unit/test_schedulers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,25 @@ def test_CrontabSchedule_schedule(self):
436436
assert s.schedule.day_of_month == {1, 16}
437437
assert s.schedule.month_of_year == {1, 7}
438438

439+
def test_CrontabSchedule_long_schedule(self):
440+
s = CrontabSchedule(
441+
minute=str(list(range(60)))[1:-1],
442+
hour=str(list(range(24)))[1:-1],
443+
day_of_week=str(list(range(7)))[1:-1],
444+
day_of_month=str(list(range(1, 32)))[1:-1],
445+
month_of_year=str(list(range(1, 13)))[1:-1]
446+
)
447+
assert s.schedule.minute == set(range(60))
448+
assert s.schedule.hour == set(range(24))
449+
assert s.schedule.day_of_week == set(range(7))
450+
assert s.schedule.day_of_month == set(range(1, 32))
451+
assert s.schedule.month_of_year == set(range(1, 13))
452+
assert len(str(s.schedule.minute)) <= s._meta.get_field('minute').max_length
453+
assert len(str(s.schedule.hour)) <= s._meta.get_field('hour').max_length
454+
assert len(str(s.schedule.day_of_week)) <= s._meta.get_field('day_of_week').max_length
455+
assert len(str(s.schedule.day_of_month)) <= s._meta.get_field('day_of_month').max_length
456+
assert len(str(s.schedule.month_of_year)) <= s._meta.get_field('month_of_year').max_length
457+
439458
def test_SolarSchedule_schedule(self):
440459
s = SolarSchedule(event='solar_noon', latitude=48.06, longitude=12.86)
441460
dt = datetime(day=26, month=7, year=2050, hour=1, minute=0)

0 commit comments

Comments
 (0)