Skip to content

Commit

Permalink
Upgrade lifespan api (#599)
Browse files Browse the repository at this point in the history
* Upgrade lifespan api

* bump imjoy-rpc 0.5.48

* post1

* Add requirements
  • Loading branch information
oeway authored Mar 3, 2024
1 parent f42ac6e commit 4441984
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion hypha/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.15.46"
"version": "0.15.47"
}
2 changes: 1 addition & 1 deletion hypha/built-in/echo-service.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>ImJoy Plugin Template</title>
<meta name="description" content="Template for ImJoy plugin">
<meta name="author" content="ImJoy-Team">
<script src="https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion hypha/built-in/imjoy-plugin-parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>ImJoy Plugin Template</title>
<meta name="description" content="Template for ImJoy plugin">
<meta name="author" content="ImJoy-Team">
<script src="https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src/pluginParser.js"></script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion hypha/built-in/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="description" content="A playground for developing ImJoy plugins">
<meta name="author" content="ImJoy-Team">
<link rel="stylesheet" data-name="vs/editor/editor.main" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src/pluginParser.js"></script>
<script>
window.default_plugin = `
Expand Down
2 changes: 1 addition & 1 deletion hypha/built-in/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<title>ImJoy Plugin Template</title>
<meta name="description" content="Template for ImJoy plugin">
<meta name="author" content="ImJoy-Team">
<script src="https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js"></script>
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions hypha/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ServiceInfo(BaseModel):
class Config:
"""Set the config for pydantic."""

extra='allow'
extra = "allow"

def is_singleton(self):
"""Check if the service is singleton."""
Expand Down Expand Up @@ -137,7 +137,7 @@ class RDF(BaseModel):
class Config:
"""Set the config for pydantic."""

extra='allow'
extra = "allow"


class ApplicationInfo(RDF):
Expand Down
23 changes: 17 additions & 6 deletions hypha/core/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ async def init(self, reset_redis, startup_functions=None):
await self.cleanup_disconnected_clients()
for service in self._public_services:
try:
await self._public_workspace_interface.register_service(service.model_dump())
await self._public_workspace_interface.register_service(
service.model_dump()
)
except Exception: # pylint: disable=broad-except
logger.exception("Failed to register public service: %s", service)
raise
Expand Down Expand Up @@ -187,7 +189,9 @@ async def add_disconnected_client(self, client_info: ClientInfo):
await self._redis.hset(
"clients:disconnected",
f"{client_info.workspace}/{client_info.id}",
json.dumps({"client": client_info.model_dump_json(), "timestamp": time.time()}),
json.dumps(
{"client": client_info.model_dump_json(), "timestamp": time.time()}
),
)

async def remove_disconnected_client(
Expand Down Expand Up @@ -239,14 +243,17 @@ async def get_user_workspace(self, user_id: str):
workspace_info = await self._redis.hget("workspaces", user_id)
if workspace_info is None:
return None
workspace_info = WorkspaceInfo.model_validate(json.loads(workspace_info.decode()))
workspace_info = WorkspaceInfo.model_validate(
json.loads(workspace_info.decode())
)
return workspace_info

async def get_all_users(self):
"""Get all users."""
users = await self._redis.hgetall("users")
return [
UserInfo.model_validate(json.loads(user.decode())) for user in users.values()
UserInfo.model_validate(json.loads(user.decode()))
for user in users.values()
]

async def get_all_workspace(self):
Expand Down Expand Up @@ -278,14 +285,18 @@ async def register_workspace(self, workspace: dict, overwrite=False):
raise KeyError(
f"Client does not exist: {workspace.name}/{client_id}"
)
client_info = ClientInfo.model_validate(json.loads(client_info.decode()))
client_info = ClientInfo.model_validate(
json.loads(client_info.decode())
)
await self._redis.srem(
f"user:{client_info.user_info.id}:clients", client_info.id
)
# assert ret >= 1, f"Client not found in user({client_info.user_info.id})'s clients list: {client_info.id}"
await self._redis.hdel(f"{workspace.name}:clients", client_id)
await self._redis.delete(f"{workspace}:clients")
await self._redis.hset("workspaces", workspace.name, workspace.model_dump_json())
await self._redis.hset(
"workspaces", workspace.name, workspace.model_dump_json()
)
await self.get_workspace_manager(workspace.name, setup=True)

self._event_bus.emit("workspace_registered", workspace.model_dump())
Expand Down
15 changes: 11 additions & 4 deletions hypha/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ async def create_workspace(

if not overwrite and await self._redis.hexists("workspaces", workspace.name):
raise Exception(f"Workspace {workspace.name} already exists.")
await self._redis.hset("workspaces", workspace.name, workspace.model_dump_json())
await self._redis.hset(
"workspaces", workspace.name, workspace.model_dump_json()
)
# Clear the workspace
await self.remove_clients(workspace.name)
workspace_info = await self.get_workspace_info(workspace.name)
Expand Down Expand Up @@ -630,7 +632,9 @@ async def get_workspace_info(self, workspace: str = None) -> WorkspaceInfo:
workspace_info = await self._redis.hget("workspaces", workspace)
if workspace_info is None:
raise KeyError(f"Workspace not found: {workspace}")
workspace_info = WorkspaceInfo.model_validate(json.loads(workspace_info.decode()))
workspace_info = WorkspaceInfo.model_validate(
json.loads(workspace_info.decode())
)
return workspace_info

async def _get_workspace_info_dict(
Expand Down Expand Up @@ -819,7 +823,8 @@ async def _get_all_workspace(self):
"""Get all workspaces."""
workspaces = await self._redis.hgetall("workspaces")
return [
WorkspaceInfo.model_validate(json.loads(v.decode())) for v in workspaces.values()
WorkspaceInfo.model_validate(json.loads(v.decode()))
for v in workspaces.values()
]

async def check_permission(
Expand Down Expand Up @@ -938,7 +943,9 @@ async def _update_workspace(self, config: dict, context=None):
if _id not in workspace.owners:
workspace.owners.append(_id)
workspace.owners = [o.strip() for o in workspace.owners if o.strip()]
await self._redis.hset("workspaces", workspace.name, workspace.model_dump_json())
await self._redis.hset(
"workspaces", workspace.name, workspace.model_dump_json()
)
self._event_bus.emit("workspace_changed", workspace.model_dump())

async def delete_if_empty(self):
Expand Down
23 changes: 12 additions & 11 deletions hypha/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from hypha.triton import TritonProxy
from hypha.utils import GZipMiddleware, GzipRoute, PatchedCORSMiddleware
from hypha.websocket import WebsocketServer
from contextlib import asynccontextmanager

try:
# For pyodide, we need to patch http
Expand Down Expand Up @@ -152,17 +153,6 @@ async def liveness(req: Request) -> JSONResponse:

return JSONResponse({"status": "DOWN"}, status_code=503)

@app.on_event("startup")
async def startup_event():
# Here we can register all the startup functions
args.startup_functions = args.startup_functions or []
args.startup_functions.append("hypha.core.auth:register_login_service")
await store.init(args.reset_redis, startup_functions=args.startup_functions)

@app.on_event("shutdown")
def shutdown_event():
store.get_event_bus().emit("shutdown", target="local")


def mount_static_files(app, new_route, directory, name="static"):
# Get top level route paths
Expand Down Expand Up @@ -200,8 +190,19 @@ def create_application(args):
else:
args.allow_origins = env.get("ALLOW_ORIGINS", "*").split(",")

@asynccontextmanager
async def lifespan(app: FastAPI):
# Here we can register all the startup functions
args.startup_functions = args.startup_functions or []
args.startup_functions.append("hypha.core.auth:register_login_service")
await store.init(args.reset_redis, startup_functions=args.startup_functions)
yield
# Emit the shutdown event
store.get_event_bus().emit("shutdown", target="local")

application = FastAPI(
title="Hypha",
lifespan=lifespan,
docs_url="/api-docs",
redoc_url="/api-redoc",
description=(
Expand Down
2 changes: 1 addition & 1 deletion hypha/templates/web-python-plugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
async def run():
try:
await micropip.install(["imjoy-rpc==0.5.44", {% for req in requirements %}"{{req}}", {% endfor %}])
await micropip.install(["imjoy-rpc==0.5.48.post1", {% for req in requirements %}"{{req}}", {% endfor %}])
js.__resolve()
except Exception as e:
js.__reject(traceback.format_exc())
Expand Down
2 changes: 1 addition & 1 deletion hypha/templates/web-worker-plugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
self.onmessage = function(e) {
const config = e.data

importScripts("https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js")
importScripts("https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js")

hyphaWebsocketClient.connectToServer(config).then(async (api)=>{
await hyphaWebsocketClient.loadRequirements([{% for req in requirements %}"{{req}}", {% endfor %}])
Expand Down
2 changes: 1 addition & 1 deletion hypha/templates/window-plugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>ImJoy Plugin (window)</title>
<meta name="description" content="Template for ImJoy plugin">
<meta name="author" content="ImJoy-Team">
<script src="https://cdn.jsdelivr.net/npm/[email protected].44/dist/hypha-rpc-websocket.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].48/dist/hypha-rpc-websocket.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ aioboto3==11.2.0
aiofiles==23.2.1
base58==2.1.1
fastapi==0.104.1
imjoy-rpc==0.5.44
imjoy-rpc==0.5.48.post1
jinja2==3.1.2
lxml==4.9.3
msgpack==1.0.5
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
REQUIREMENTS = [
"aiofiles",
"fastapi>=0.70.0",
"imjoy-rpc>=0.5.44",
"imjoy-rpc>=0.5.48.post1",
"msgpack>=1.0.2",
"numpy",
"pydantic[email]>=2.6.1",
Expand Down

0 comments on commit 4441984

Please sign in to comment.