oncall-engine/engine/apps/alerts/tasks/wipe.py
Vadim Stepanov 1acb7018d0
Improve alert group deletion API (#3124)
# What this PR does

- Invalidate alert group cache on wipe
- Improve public API docs on alert group deletion
- Add / improve tests

## Which issue(s) this PR fixes

Related to https://github.com/grafana/oncall/issues/3051

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-10-05 14:32:40 +01:00

22 lines
904 B
Python

from django.conf import settings
from common.custom_celery_tasks import shared_dedicated_queue_retry_task
@shared_dedicated_queue_retry_task(
autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else None
)
def wipe(alert_group_pk, user_pk):
from apps.alerts.models import AlertGroup
from apps.api.serializers.alert import AlertFieldsCacheSerializerMixin
from apps.api.serializers.alert_group import AlertGroupFieldsCacheSerializerMixin
from apps.user_management.models import User
alert_group = AlertGroup.objects.filter(pk=alert_group_pk).first()
user = User.objects.filter(pk=user_pk).first()
alert_group.wipe_by_user(user)
# Clear internal API cache
AlertGroupFieldsCacheSerializerMixin.bust_object_caches(alert_group)
for alert in alert_group.alerts.all():
AlertFieldsCacheSerializerMixin.bust_object_caches(alert)