Spaces:
Running
Running
added upstash
Browse files- redis_keep_alive_service.py +53 -55
redis_keep_alive_service.py
CHANGED
|
@@ -50,6 +50,9 @@ class RedisPingResponse(BaseModel):
|
|
| 50 |
time: str
|
| 51 |
latency_ms: float | None = None
|
| 52 |
type: str # "self-managed" or "upstash"
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
# ---------- Self-managed Redis ----------
|
|
@@ -96,8 +99,8 @@ def ping_redis(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 96 |
|
| 97 |
|
| 98 |
# ---------- Upstash Redis ----------
|
| 99 |
-
def ping_upstash(service_name, redis_config):
|
| 100 |
-
|
| 101 |
try:
|
| 102 |
headers = {"Authorization": f"Bearer {redis_config['rest_token']}"}
|
| 103 |
|
|
@@ -107,38 +110,33 @@ def ping_upstash(service_name, redis_config):
|
|
| 107 |
resp = requests.get(url, headers=headers)
|
| 108 |
latency = (datetime.now() - start_time).total_seconds() * 1000
|
| 109 |
|
| 110 |
-
if resp.status_code =
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
"
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
"latency_ms": None,
|
| 138 |
-
"type": "upstash",
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
# ✅ If ping is OK, also test SET/GET
|
| 142 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 143 |
value = "ok"
|
| 144 |
|
|
@@ -154,28 +152,27 @@ def ping_upstash(service_name, redis_config):
|
|
| 154 |
if get_resp.status_code == 200:
|
| 155 |
got_value = get_resp.json().get("result")
|
| 156 |
|
| 157 |
-
return
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
except Exception as e:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
"
|
| 177 |
-
|
| 178 |
-
}
|
| 179 |
|
| 180 |
|
| 181 |
# ---------- Run All ----------
|
|
@@ -185,7 +182,7 @@ def ping_all_redis_projects() -> list[RedisPingResponse]:
|
|
| 185 |
|
| 186 |
for service_name, config in REDIS_SERVICES.items():
|
| 187 |
if not config or not any(config.values()):
|
| 188 |
-
continue
|
| 189 |
if "rest_url" in config: # Upstash
|
| 190 |
results.append(ping_upstash(service_name, config))
|
| 191 |
else: # Self-managed
|
|
@@ -195,3 +192,4 @@ def ping_all_redis_projects() -> list[RedisPingResponse]:
|
|
| 195 |
return results
|
| 196 |
|
| 197 |
|
|
|
|
|
|
| 50 |
time: str
|
| 51 |
latency_ms: float | None = None
|
| 52 |
type: str # "self-managed" or "upstash"
|
| 53 |
+
set_latency_ms: float | None = None
|
| 54 |
+
get_latency_ms: float | None = None
|
| 55 |
+
value: str | None = None
|
| 56 |
|
| 57 |
|
| 58 |
# ---------- Self-managed Redis ----------
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
# ---------- Upstash Redis ----------
|
| 102 |
+
def ping_upstash(service_name: str, redis_config: dict) -> RedisPingResponse:
|
| 103 |
+
now = datetime.utcnow().isoformat()
|
| 104 |
try:
|
| 105 |
headers = {"Authorization": f"Bearer {redis_config['rest_token']}"}
|
| 106 |
|
|
|
|
| 110 |
resp = requests.get(url, headers=headers)
|
| 111 |
latency = (datetime.now() - start_time).total_seconds() * 1000
|
| 112 |
|
| 113 |
+
if resp.status_code != 200:
|
| 114 |
+
return RedisPingResponse(
|
| 115 |
+
service_name=service_name,
|
| 116 |
+
success=False,
|
| 117 |
+
error=f"HTTP {resp.status_code}: {resp.text}",
|
| 118 |
+
time=now,
|
| 119 |
+
latency_ms=None,
|
| 120 |
+
type="upstash",
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
result = None
|
| 124 |
+
try:
|
| 125 |
+
result = resp.json().get("result")
|
| 126 |
+
except Exception:
|
| 127 |
+
result = resp.text.strip()
|
| 128 |
+
|
| 129 |
+
if not result or result.upper() != "PONG":
|
| 130 |
+
return RedisPingResponse(
|
| 131 |
+
service_name=service_name,
|
| 132 |
+
success=False,
|
| 133 |
+
error=f"Unexpected response: {resp.status_code} - {resp.text}",
|
| 134 |
+
time=now,
|
| 135 |
+
latency_ms=None,
|
| 136 |
+
type="upstash",
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# ✅ SET/GET
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 141 |
value = "ok"
|
| 142 |
|
|
|
|
| 152 |
if get_resp.status_code == 200:
|
| 153 |
got_value = get_resp.json().get("result")
|
| 154 |
|
| 155 |
+
return RedisPingResponse(
|
| 156 |
+
service_name=service_name,
|
| 157 |
+
success=True,
|
| 158 |
+
error=None,
|
| 159 |
+
time=now,
|
| 160 |
+
latency_ms=latency,
|
| 161 |
+
type="upstash",
|
| 162 |
+
set_latency_ms=set_latency,
|
| 163 |
+
get_latency_ms=get_latency,
|
| 164 |
+
value=got_value,
|
| 165 |
+
)
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
+
return RedisPingResponse(
|
| 169 |
+
service_name=service_name,
|
| 170 |
+
success=False,
|
| 171 |
+
error=str(e),
|
| 172 |
+
time=now,
|
| 173 |
+
latency_ms=None,
|
| 174 |
+
type="upstash",
|
| 175 |
+
)
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
# ---------- Run All ----------
|
|
|
|
| 182 |
|
| 183 |
for service_name, config in REDIS_SERVICES.items():
|
| 184 |
if not config or not any(config.values()):
|
| 185 |
+
continue
|
| 186 |
if "rest_url" in config: # Upstash
|
| 187 |
results.append(ping_upstash(service_name, config))
|
| 188 |
else: # Self-managed
|
|
|
|
| 192 |
return results
|
| 193 |
|
| 194 |
|
| 195 |
+
|