Simplify integrations cache on startupprobe (#3274)

Revert logic to detect integrations server on startupprobe and simplify
logic (and rework failing test having issues with urlpatterns cache and
reset), since cache is the same for engine and integrations, and it is
populated once.
This commit is contained in:
Matias Bordese 2023-11-06 15:03:46 -03:00 committed by GitHub
parent cc9dc66437
commit 20f949df8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 35 deletions

View file

@ -1,36 +1,12 @@
import sys
from importlib import import_module, reload
from unittest.mock import patch
import pytest
from django.conf import settings
from django.urls import clear_url_caches
from rest_framework import status
from rest_framework.test import APIClient
def reload_urlconf():
clear_url_caches()
if settings.ROOT_URLCONF in sys.modules:
reload(sys.modules[settings.ROOT_URLCONF])
return import_module(settings.ROOT_URLCONF)
@pytest.mark.skip(reason="TODO: This test is currently failing in oncall-private, skipping to unblock release")
@pytest.mark.parametrize(
"detached_integrations,urlconf,is_cache_updated",
[
(False, None, True),
(True, None, False),
(True, "engine.integrations_urls", True),
],
)
def test_startupprobe_populates_integrations_cache(settings, detached_integrations, urlconf, is_cache_updated):
settings.DETACHED_INTEGRATIONS_SERVER = detached_integrations
if urlconf:
settings.ROOT_URLCONF = urlconf
reload_urlconf()
@pytest.mark.urls("engine.integrations_urls")
def test_detached_integrations_startupprobe_populates_integrations_cache():
client = APIClient()
with patch(
@ -39,4 +15,16 @@ def test_startupprobe_populates_integrations_cache(settings, detached_integratio
response = client.get("/startupprobe/")
assert response.status_code == status.HTTP_200_OK
assert mock_update_cache.called == is_cache_updated
mock_update_cache.assert_called_once
def test_startupprobe_populates_integrations_cache():
client = APIClient()
with patch(
"apps.integrations.mixins.AlertChannelDefiningMixin.update_alert_receive_channel_cache"
) as mock_update_cache:
response = client.get("/startupprobe/")
assert response.status_code == status.HTTP_200_OK
mock_update_cache.assert_called_once

View file

@ -1,4 +1,3 @@
from django import urls
from django.conf import settings
from django.core.cache import cache
from django.http import HttpResponse, JsonResponse
@ -44,13 +43,7 @@ class StartupProbeView(View):
dangerously_bypass_middlewares = True
def get(self, request):
# enable integrations cache if current engine instance is serving them
integrations_enabled = True
if settings.DETACHED_INTEGRATIONS_SERVER:
url_resolver = urls.get_resolver(urls.get_urlconf())
integrations_enabled = url_resolver.namespace_dict.get("integrations")
if integrations_enabled and cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None:
if cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None:
AlertChannelDefiningMixin().update_alert_receive_channel_cache()
cache.set("healthcheck", "healthcheck", 30) # Checking cache connectivity