Skip to content

Commit

Permalink
TaskChunkSeries rule: do not schedule instances before start date
Browse files Browse the repository at this point in the history
Otherwise, an infinite loop scheduling new instances on the same day
could occur.
  • Loading branch information
wichmannpas committed Sep 10, 2018
1 parent 6c1fa44 commit 46c1ab1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions task/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ def apply_rule(self, last: Optional[date] = None) -> Optional[date]:
"""
if last and last < self.start:
# if the start date is modified after chunks are scheduled
# already, prevent from scheduling any more chunks in the
# past
# already, prevent from scheduling any more chunks before the
# start date
last = None

apply = getattr(self, '_apply_{}'.format(self.rule), None)
Expand All @@ -315,7 +315,10 @@ def _apply_monthly(self, last: Optional[date], day: Optional[int] = None) -> Opt
day = self.monthly_day

if not last:
return self._replace_day(self.start, day)
next = self._replace_day(self.start, day)
if next < self.start:
next = self._add_months(next, 1)
return next

next = self._add_months(last, self.monthly_months)
return self._replace_day(next, day)
Expand Down
27 changes: 27 additions & 0 deletions task/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3085,6 +3085,33 @@ def test_apply_rule_monthlyweekday_last(self):
series.apply_rule(date(2010, 5, 16)),
date(2010, 6, 26))

@freeze_time('2010-02-24')
def test_apply_rule_monthlyweekday_start_before_first_potential_schedule(self):
"""
Test applying the monthlyweekday rule if the instance of
the start month would be before the actual start date.
"""
series = TaskChunkSeries.objects.create(
task=self.task,
start=date(2010, 1, 28),
rule='monthlyweekday',
monthly_months=1,
monthlyweekday_weekday=1,
monthlyweekday_nth=1)

# interval without previous occurrence schedules for month of start
self.assertEqual(
series.apply_rule(),
date(2010, 2, 2))

self.assertEqual(
series.apply_rule(series.start),
date(2010, 2, 2))

self.assertEqual(
series.apply_rule(date(2010, 2, 5)),
date(2010, 3, 2))

def test_schedule_limit_count(self):
series = TaskChunkSeries.objects.create(
task=self.task,
Expand Down

0 comments on commit 46c1ab1

Please sign in to comment.