Browse Source

feat(am2-check): Add possibility to refresh cache (#57653)

Riccardo Busetti 1 year ago
parent
commit
99d3254e97

+ 21 - 12
src/sentry/api/endpoints/check_am2_compatibility.py

@@ -9,6 +9,7 @@ from sentry.tasks.check_am2_compatibility import (
     CheckStatus,
     get_check_results,
     get_check_status,
+    refresh_check_state,
     run_compatibility_check_async,
 )
 
@@ -23,18 +24,26 @@ class CheckAM2CompatibilityEndpoint(Endpoint):
     def get(self, request: Request) -> Response:
         try:
             org_id = request.GET.get("orgId")
-            check_status = get_check_status(org_id)
-            if check_status == CheckStatus.DONE:
-                results = get_check_results(org_id)
-                # In case the state is done, but we didn't find a valid value in cache, we have a problem.
-                if results is None:
-                    raise Exception("the check status is done in cache but there are no results.")
-
-                return Response({"status": CheckStatus.DONE.value, **results}, status=200)
-            elif check_status == CheckStatus.IN_PROGRESS:
-                return Response({"status": CheckStatus.IN_PROGRESS.value}, status=202)
-            elif check_status == CheckStatus.ERROR:
-                raise Exception("the asynchronous task had an internal error.")
+            refresh = request.GET.get("refresh") == "true"
+
+            if refresh:
+                refresh_check_state(org_id)
+            else:
+                # We check the caches only if we are not refreshing.
+                check_status = get_check_status(org_id)
+                if check_status == CheckStatus.DONE:
+                    results = get_check_results(org_id)
+                    # In case the state is done, but we didn't find a valid value in cache, we have a problem.
+                    if results is None:
+                        raise Exception(
+                            "the check status is done in cache but there are no results."
+                        )
+
+                    return Response({"status": CheckStatus.DONE.value, **results}, status=200)
+                elif check_status == CheckStatus.IN_PROGRESS:
+                    return Response({"status": CheckStatus.IN_PROGRESS.value}, status=202)
+                elif check_status == CheckStatus.ERROR:
+                    raise Exception("the asynchronous task had an internal error.")
 
             # In case we have no status, we will trigger the asynchronous job and return.
             run_compatibility_check_async.delay(org_id)

+ 11 - 2
src/sentry/tasks/check_am2_compatibility.py

@@ -247,6 +247,7 @@ SDKS_SUPPORTING_PERFORMANCE = {
     "sentry.java.android.unity",
 }
 
+CACHING_TTL_IN_SECONDS = 60 * 10  # 10 minutes
 TASK_SOFT_LIMIT_IN_SECONDS = 30 * 60  # 30 minutes
 ONE_MINUTE_TTL = 60  # 1 minute
 
@@ -608,7 +609,7 @@ def generate_cache_key_for_async_result(org_id):
     return f"ds::o:{org_id}:check_am2_compatibility_results"
 
 
-def set_check_status(org_id, status, ttl=TASK_SOFT_LIMIT_IN_SECONDS):
+def set_check_status(org_id, status, ttl=CACHING_TTL_IN_SECONDS):
     redis_client = get_redis_client_for_ds()
     cache_key = generate_cache_key_for_async_progress(org_id)
 
@@ -633,7 +634,7 @@ def set_check_results(org_id, results):
     cache_key = generate_cache_key_for_async_result(org_id)
 
     redis_client.set(cache_key, json.dumps(results))
-    redis_client.expire(cache_key, TASK_SOFT_LIMIT_IN_SECONDS)
+    redis_client.expire(cache_key, CACHING_TTL_IN_SECONDS)
 
 
 def get_check_results(org_id):
@@ -649,6 +650,14 @@ def get_check_results(org_id):
         return None
 
 
+def refresh_check_state(org_id):
+    redis_client = get_redis_client_for_ds()
+    status_cache_key = generate_cache_key_for_async_progress(org_id)
+    results_cache_key = generate_cache_key_for_async_result(org_id)
+
+    redis_client.delete(status_cache_key, results_cache_key)
+
+
 @instrumented_task(
     name="sentry.tasks.check_am2_compatibility",
     queue="dynamicsampling",