Skip to content

Commit aee3565

Browse files
committed
added healthcheck for default web sockets
1 parent a1a2bea commit aee3565

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

template/e2b.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
team_id = "460355b3-4f64-48f9-9a16-4442817f79f5"
1414
memory_mb = 1_024
1515
start_cmd = "/root/.jupyter/start-up.sh"
16+
ready_cmd = "curl -s -o /dev/null -w '200' http://localhost:49999/health | grep -q '200'"
1617
dockerfile = "e2b.Dockerfile"
1718
template_name = "code-interpreter-v1"
1819
template_id = "nlhz8vlwyupq845jsdg9"

template/server/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ async def lifespan(app: FastAPI):
6565

6666
@app.get("/health")
6767
async def get_health():
68-
return "OK"
68+
websockets_ready = True
69+
for ws in default_websockets.values():
70+
if not ws in websockets or not websockets[ws].is_connected():
71+
websockets_ready = False
72+
print(f"Context {ws} is not connected")
73+
break
74+
75+
if not websockets_ready:
76+
return PlainTextResponse("Not ready", status_code=503)
77+
78+
return PlainTextResponse("OK", status_code=200)
6979

7080

7181
@app.post("/execute")

template/server/messaging.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ async def _process_message(self, data: dict):
542542
else:
543543
logger.warning(f"[UNHANDLED MESSAGE TYPE]: {data['msg_type']}")
544544

545+
def is_connected(self) -> bool:
546+
"""Check if the websocket is connected and open"""
547+
return self._ws is not None and self._ws.open
548+
545549
async def close(self):
546550
logger.debug(f"Closing WebSocket {self.context_id}")
547551

0 commit comments

Comments
 (0)