Skip to content

Commit

Permalink
Handle error when updating ical cache from deleted web schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb authored and iskhakov committed Dec 31, 2022
1 parent e7ddea1 commit 85dfc55
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
13 changes: 11 additions & 2 deletions engine/apps/schedules/models/on_call_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.conf import settings
from django.core.validators import MinLengthValidator
from django.db import models
from django.db.utils import DatabaseError
from django.utils import timezone
from django.utils.functional import cached_property
from icalendar.cal import Calendar
Expand Down Expand Up @@ -637,7 +638,11 @@ def _ical_file_primary(self):
"""Return cached ical file with iCal events from custom on-call shifts."""
if self.cached_ical_file_primary is None:
self.cached_ical_file_primary = self._generate_ical_file_primary()
self.save(update_fields=["cached_ical_file_primary"])
try:
self.save(update_fields=["cached_ical_file_primary"])
except DatabaseError:
# schedule may have been deleted from db
return
return self.cached_ical_file_primary

def _refresh_primary_ical_file(self):
Expand All @@ -650,7 +655,11 @@ def _ical_file_overrides(self):
"""Return cached ical file with iCal events from custom on-call overrides shifts."""
if self.cached_ical_file_overrides is None:
self.cached_ical_file_overrides = self._generate_ical_file_overrides()
self.save(update_fields=["cached_ical_file_overrides"])
try:
self.save(update_fields=["cached_ical_file_overrides"])
except DatabaseError:
# schedule may have been deleted from db
return
return self.cached_ical_file_overrides

def _refresh_overrides_ical_file(self):
Expand Down
34 changes: 34 additions & 0 deletions engine/apps/schedules/tests/test_on_call_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,3 +924,37 @@ def test_schedule_related_users(make_organization, make_user_for_organization, m
schedule.refresh_from_db()
users = schedule.related_users()
assert users == set(u.public_primary_key for u in [user_a, user_d, user_e])


@pytest.mark.django_db(transaction=True)
def test_filter_events_none_cache_unchanged(
make_organization, make_user_for_organization, make_schedule, make_on_call_shift
):
organization = make_organization()
user = make_user_for_organization(organization)
schedule = make_schedule(
organization,
schedule_class=OnCallScheduleWeb,
name="test_web_schedule",
)
start_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)

# add shift
data = {
"start": start_date + timezone.timedelta(hours=36),
"rotation_start": start_date + timezone.timedelta(hours=36),
"duration": timezone.timedelta(hours=2),
"frequency": CustomOnCallShift.FREQUENCY_DAILY,
"schedule": schedule,
}
on_call_shift = make_on_call_shift(
organization=organization, shift_type=CustomOnCallShift.TYPE_ROLLING_USERS_EVENT, **data
)
on_call_shift.add_rolling_users([[user]])

# schedule is removed from db
schedule.delete()

events = schedule.filter_events("UTC", start_date, days=5, filter_by=OnCallSchedule.TYPE_ICAL_PRIMARY)
expected = []
assert events == expected

0 comments on commit 85dfc55

Please sign in to comment.