Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
Support liveness endpoint (#161)
Browse files Browse the repository at this point in the history
* support liveness endpoint

* Fix linter
  • Loading branch information
oeway authored May 23, 2021
1 parent 8eebd97 commit 0e09b78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion imjoy/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.11.10"
"version": "0.11.11"
}
23 changes: 23 additions & 0 deletions imjoy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from fastapi import FastAPI
from fastapi.logger import logger
from fastapi.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import JSONResponse

from imjoy import __version__ as VERSION
from imjoy.core import (
Expand Down Expand Up @@ -79,6 +81,11 @@ async def connect(sid, environ):
all_users[uid]._sessions.append(sid)
all_sessions[sid] = all_users[uid]

@sio.event
async def echo(sid, data):
"""Echo service for testing."""
return data

@sio.event
async def register_plugin(sid, config):
user_info = all_sessions[sid]
Expand Down Expand Up @@ -241,6 +248,22 @@ def setup_socketio_server(
app.sio = sio
core_api = CoreInterface()
initialize_socketio(sio, core_api)

@app.get("/liveness")
async def liveness(req: Request) -> JSONResponse:
try:
loop = asyncio.get_event_loop()
fut = loop.create_future()

def done():
fut.set_result()

await sio.emit("liveness", callback=done)
await fut
except Exception: # pylint: disable=broad-except
return JSONResponse({"status": "DOWN"}, status_code=503)
return JSONResponse({"status": "OK"})

return sio


Expand Down
4 changes: 2 additions & 2 deletions tests/test_imjoy_engine_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def socketio_server_fixture():
[sys.executable, "-m", "imjoy.server", f"--port={PORT}"]
) as proc:

timeout = 5
timeout = 10
while timeout > 0:
try:
response = requests.get(f"http://127.0.0.1:{PORT}/")
response = requests.get(f"http://127.0.0.1:{PORT}/liveness")
if response.ok:
break
except RequestException:
Expand Down

0 comments on commit 0e09b78

Please # to comment.