Spaces:
Running
Running
added upstash
Browse files- redis_keep_alive_service.py +15 -15
redis_keep_alive_service.py
CHANGED
|
@@ -10,8 +10,8 @@ from pydantic import BaseModel
|
|
| 10 |
# Logging (console only)
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.INFO,
|
| 13 |
-
format=
|
| 14 |
-
handlers=[logging.StreamHandler()]
|
| 15 |
)
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
|
@@ -29,7 +29,6 @@ REDIS_SERVICES = {
|
|
| 29 |
"port": os.getenv("REDIS_DEVELOPMENT_PORT"),
|
| 30 |
"password": os.getenv("REDIS_DEVELOPMENT_PASSWORD"),
|
| 31 |
},
|
| 32 |
-
|
| 33 |
# Upstash Redis (REST API)
|
| 34 |
"upstash_dev": {
|
| 35 |
"rest_url": os.getenv("UPSTASH_REDIS_DEV_REST_URL"),
|
|
@@ -42,7 +41,7 @@ REDIS_SERVICES = {
|
|
| 42 |
}
|
| 43 |
|
| 44 |
|
| 45 |
-
# ---------- Pydantic Response ----------
|
| 46 |
class RedisPingResponse(BaseModel):
|
| 47 |
service_name: str
|
| 48 |
success: bool
|
|
@@ -55,6 +54,10 @@ class RedisPingResponse(BaseModel):
|
|
| 55 |
value: str | None = None
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# ---------- Self-managed Redis ----------
|
| 59 |
def ping_redis(service_name: str, redis_config: dict) -> RedisPingResponse:
|
| 60 |
now = datetime.utcnow().isoformat()
|
|
@@ -75,15 +78,17 @@ def ping_redis(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 75 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 76 |
r.set(key, "ok")
|
| 77 |
got = r.get(key)
|
|
|
|
| 78 |
|
| 79 |
-
logger.info(f"{service_name} PING OK, latency {latency:.2f}ms, value={
|
| 80 |
return RedisPingResponse(
|
| 81 |
service_name=service_name,
|
| 82 |
success=True,
|
| 83 |
error=None,
|
| 84 |
time=now,
|
| 85 |
latency_ms=latency,
|
| 86 |
-
type="self-managed"
|
|
|
|
| 87 |
)
|
| 88 |
except Exception as e:
|
| 89 |
error_msg = str(e)
|
|
@@ -94,7 +99,7 @@ def ping_redis(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 94 |
error=error_msg,
|
| 95 |
time=now,
|
| 96 |
latency_ms=None,
|
| 97 |
-
type="self-managed"
|
| 98 |
)
|
| 99 |
|
| 100 |
|
|
@@ -120,7 +125,6 @@ def ping_upstash(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 120 |
type="upstash",
|
| 121 |
)
|
| 122 |
|
| 123 |
-
result = None
|
| 124 |
try:
|
| 125 |
result = resp.json().get("result")
|
| 126 |
except Exception:
|
|
@@ -136,7 +140,7 @@ def ping_upstash(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 136 |
type="upstash",
|
| 137 |
)
|
| 138 |
|
| 139 |
-
# ✅ SET/GET
|
| 140 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 141 |
value = "ok"
|
| 142 |
|
|
@@ -163,7 +167,6 @@ def ping_upstash(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 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,
|
|
@@ -176,7 +179,7 @@ def ping_upstash(service_name: str, redis_config: dict) -> RedisPingResponse:
|
|
| 176 |
|
| 177 |
|
| 178 |
# ---------- Run All ----------
|
| 179 |
-
def ping_all_redis_projects() ->
|
| 180 |
logger.info("Starting Redis service health check")
|
| 181 |
results: list[RedisPingResponse] = []
|
| 182 |
|
|
@@ -189,7 +192,4 @@ def ping_all_redis_projects() -> list[RedisPingResponse]:
|
|
| 189 |
results.append(ping_redis(service_name, config))
|
| 190 |
|
| 191 |
logger.info("Completed all Redis service checks")
|
| 192 |
-
return results
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
| 10 |
# Logging (console only)
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.INFO,
|
| 13 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 14 |
+
handlers=[logging.StreamHandler()],
|
| 15 |
)
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
|
|
|
| 29 |
"port": os.getenv("REDIS_DEVELOPMENT_PORT"),
|
| 30 |
"password": os.getenv("REDIS_DEVELOPMENT_PASSWORD"),
|
| 31 |
},
|
|
|
|
| 32 |
# Upstash Redis (REST API)
|
| 33 |
"upstash_dev": {
|
| 34 |
"rest_url": os.getenv("UPSTASH_REDIS_DEV_REST_URL"),
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
|
| 44 |
+
# ---------- Pydantic Response Models ----------
|
| 45 |
class RedisPingResponse(BaseModel):
|
| 46 |
service_name: str
|
| 47 |
success: bool
|
|
|
|
| 54 |
value: str | None = None
|
| 55 |
|
| 56 |
|
| 57 |
+
class RedisPingAllResponse(BaseModel):
|
| 58 |
+
services: list[RedisPingResponse]
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# ---------- Self-managed Redis ----------
|
| 62 |
def ping_redis(service_name: str, redis_config: dict) -> RedisPingResponse:
|
| 63 |
now = datetime.utcnow().isoformat()
|
|
|
|
| 78 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 79 |
r.set(key, "ok")
|
| 80 |
got = r.get(key)
|
| 81 |
+
got_value = got.decode() if got else None # ✅ ensure only str, not bytes or Redis object
|
| 82 |
|
| 83 |
+
logger.info(f"{service_name} PING OK, latency {latency:.2f}ms, value={got_value}")
|
| 84 |
return RedisPingResponse(
|
| 85 |
service_name=service_name,
|
| 86 |
success=True,
|
| 87 |
error=None,
|
| 88 |
time=now,
|
| 89 |
latency_ms=latency,
|
| 90 |
+
type="self-managed",
|
| 91 |
+
value=got_value,
|
| 92 |
)
|
| 93 |
except Exception as e:
|
| 94 |
error_msg = str(e)
|
|
|
|
| 99 |
error=error_msg,
|
| 100 |
time=now,
|
| 101 |
latency_ms=None,
|
| 102 |
+
type="self-managed",
|
| 103 |
)
|
| 104 |
|
| 105 |
|
|
|
|
| 125 |
type="upstash",
|
| 126 |
)
|
| 127 |
|
|
|
|
| 128 |
try:
|
| 129 |
result = resp.json().get("result")
|
| 130 |
except Exception:
|
|
|
|
| 140 |
type="upstash",
|
| 141 |
)
|
| 142 |
|
| 143 |
+
# ✅ SET/GET test
|
| 144 |
key = f"healthcheck:{uuid.uuid4()}"
|
| 145 |
value = "ok"
|
| 146 |
|
|
|
|
| 167 |
get_latency_ms=get_latency,
|
| 168 |
value=got_value,
|
| 169 |
)
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
return RedisPingResponse(
|
| 172 |
service_name=service_name,
|
|
|
|
| 179 |
|
| 180 |
|
| 181 |
# ---------- Run All ----------
|
| 182 |
+
def ping_all_redis_projects() -> RedisPingAllResponse:
|
| 183 |
logger.info("Starting Redis service health check")
|
| 184 |
results: list[RedisPingResponse] = []
|
| 185 |
|
|
|
|
| 192 |
results.append(ping_redis(service_name, config))
|
| 193 |
|
| 194 |
logger.info("Completed all Redis service checks")
|
| 195 |
+
return RedisPingAllResponse(services=results)
|
|
|
|
|
|
|
|
|