Skip to content

Commit

Permalink
bump black; fix cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Dec 18, 2024
1 parent 2f6e752 commit 0237b8f
Show file tree
Hide file tree
Showing 49 changed files with 118 additions and 43 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.20.44"
"version": "0.20.44.post1"
}
1 change: 1 addition & 0 deletions hypha/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Main module for hypha."""

import json
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions hypha/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Main module for hypha."""

from hypha.server import get_argparser, start_server


Expand Down
23 changes: 16 additions & 7 deletions hypha/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,24 @@ async def install(
f"User {user_info.id} does not have permission"
f" to install apps in workspace {workspace_info.id}"
)

if config:
config["entry_point"] = config.get("entry_point", "index.html")
template = config.get("type") + "." + config["entry_point"]
else:
template = "hypha"

if source.startswith("http"):
if not (source.startswith("https://") or source.startswith("http://localhost") or source.startswith("http://127.0.0.1")):
if not (
source.startswith("https://")
or source.startswith("http://localhost")
or source.startswith("http://127.0.0.1")
):
raise Exception("Only secured https urls are allowed: " + source)
if source.startswith("https://") and (source.split("?")[0].endswith(".imjoy.html") or source.split("?")[0].endswith(".hypha.html")):
if source.startswith("https://") and (
source.split("?")[0].endswith(".imjoy.html")
or source.split("?")[0].endswith(".hypha.html")
):
# download source with httpx
async with httpx.AsyncClient() as client:
response = await client.get(source)
Expand Down Expand Up @@ -248,7 +255,7 @@ async def install(
version="stage",
context=context,
)

if template:
# Upload the main source file
put_url = await self.artifact_manager.put_file(
Expand Down Expand Up @@ -428,7 +435,7 @@ async def start(
server_url = self.local_base_url
local_url = (
f"{entry_point}?"
+ f'server_url={server_url}&client_id={client_id}&workspace={workspace}'
+ f"server_url={server_url}&client_id={client_id}&workspace={workspace}"
+ f"&app_id={app_id}"
+ f"&server_url={server_url}"
+ (f"&token={token}" if token else "")
Expand Down Expand Up @@ -519,7 +526,9 @@ def service_added(info: dict):

# save the services
manifest.name = manifest.name or app_info.get("name", "Untitled App")
manifest.description = manifest.description or app_info.get("description", "")
manifest.description = manifest.description or app_info.get(
"description", ""
)
manifest.services = collected_services
manifest = ApplicationManifest.model_validate(
manifest.model_dump(mode="json")
Expand Down
46 changes: 27 additions & 19 deletions hypha/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,17 @@ async def get_zip_file_content(
# Top-level file
directory_contents.append(
{
"type": "file"
if not zip_info.is_dir()
else "directory",
"type": (
"file"
if not zip_info.is_dir()
else "directory"
),
"name": relative_path,
"size": zip_info.file_size
if not zip_info.is_dir()
else None,
"size": (
zip_info.file_size
if not zip_info.is_dir()
else None
),
"last_modified": datetime(
*zip_info.date_time
).timestamp(),
Expand Down Expand Up @@ -897,9 +901,9 @@ async def _get_artifact_with_parent(self, session, artifact_id):
def _generate_artifact_data(self, artifact, parent_artifact=None):
artifact_data = model_to_dict(artifact)
if parent_artifact:
artifact_data[
"parent_id"
] = f"{parent_artifact.workspace}/{parent_artifact.alias}"
artifact_data["parent_id"] = (
f"{parent_artifact.workspace}/{parent_artifact.alias}"
)
artifact_data["id"] = f"{artifact.workspace}/{artifact.alias}"
artifact_data["_id"] = artifact.id
# Exclude 'secrets' from artifact_data to prevent exposure
Expand Down Expand Up @@ -1777,10 +1781,10 @@ async def read(
artifact_data["config"]["child_count"] = child_count
elif artifact.type == "vector-collection":
artifact_data["config"] = artifact_data.get("config", {})
artifact_data["config"][
"vector_count"
] = await self._vector_engine.count(
f"{artifact.workspace}/{artifact.alias}"
artifact_data["config"]["vector_count"] = (
await self._vector_engine.count(
f"{artifact.workspace}/{artifact.alias}"
)
)
if not silent:
await session.commit()
Expand Down Expand Up @@ -2559,12 +2563,16 @@ async def list_children(
model_field = range_fields[key]
if isinstance(value, list):
condition = and_(
model_field >= value[0]
if value[0] is not None
else True,
model_field <= value[1]
if value[1] is not None
else True,
(
model_field >= value[0]
if value[0] is not None
else True
),
(
model_field <= value[1]
if value[1] is not None
else True
),
)
else:
condition = model_field >= value
Expand Down
13 changes: 9 additions & 4 deletions hypha/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide the ImJoy core API interface."""

import asyncio
import inspect
import io
Expand Down Expand Up @@ -515,9 +516,11 @@ async def emit_message(self, data: Union[dict, bytes]):

message.update(
{
"ws": target_id.split("/")[0]
if self._workspace == "*"
else self._workspace,
"ws": (
target_id.split("/")[0]
if self._workspace == "*"
else self._workspace
),
"to": target_id,
"from": source_id,
"user": self._user_info,
Expand Down Expand Up @@ -688,7 +691,9 @@ async def stop(self):
async def _subscribe_redis(self):
cpu_count = os.cpu_count() or 1
concurrent_tasks = cpu_count * 10
logger.info(f"Starting Redis event bus with {concurrent_tasks} concurrent task processing")
logger.info(
f"Starting Redis event bus with {concurrent_tasks} concurrent task processing"
)
pubsub = self._redis.pubsub()
self._stop = False
semaphore = asyncio.Semaphore(concurrent_tasks) # Limit concurrent tasks
Expand Down
5 changes: 2 additions & 3 deletions hypha/core/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide authentication."""

import asyncio
import json
import logging
Expand Down Expand Up @@ -370,9 +371,7 @@ async def start_login(workspace: str = None, expires_in: int = None):
+ (
f"&workspace={workspace}"
if workspace
else "" + f"&expires_in={expires_in}"
if expires_in
else ""
else "" + f"&expires_in={expires_in}" if expires_in else ""
),
"key": key,
"report_url": f"{login_service_url}/report",
Expand Down
1 change: 1 addition & 0 deletions hypha/core/store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A scalable state store based on Redis."""

import asyncio
import json
import logging
Expand Down
28 changes: 20 additions & 8 deletions hypha/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,13 @@ async def delete_workspace(
if not user_info.check_permission(ws, UserPermission.admin):
raise PermissionError(f"Permission denied for workspace {ws}")
workspace_info = await self.load_workspace_info(workspace)
await self._redis.hdel("workspaces", workspace)
# delete all the associated keys
keys = await self._redis.keys(f"{ws}:*")
for key in keys:
await self._redis.delete(key)
if self._s3_controller:
await self._s3_controller.cleanup_workspace(workspace_info, force=True)
await self._redis.hdel("workspaces", workspace)
await self._event_bus.emit("workspace_deleted", workspace_info.model_dump())
# remove the workspace from the user's bookmarks
user_workspace = await self.load_workspace_info(user_info.get_workspace())
Expand Down Expand Up @@ -1160,7 +1164,11 @@ async def register_service(
return
# Check if the service already exists
service_exists = await self._redis.keys(f"services:*|*:{service.id}@*")
visibility = service.config.visibility.value if isinstance(service.config.visibility, VisibilityEnum) else service.config.visibility
visibility = (
service.config.visibility.value
if isinstance(service.config.visibility, VisibilityEnum)
else service.config.visibility
)
key = f"services:{visibility}|{service.type}:{service.id}@{service.app_id}"

if service_exists:
Expand Down Expand Up @@ -1317,7 +1325,11 @@ async def unregister_service(
assert ":" in service.id, "Service id info must contain ':'"
service.app_id = service.app_id or "*"
service.type = service.type or "*"
visibility = service.config.visibility.value if isinstance(service.config.visibility, VisibilityEnum) else service.config.visibility
visibility = (
service.config.visibility.value
if isinstance(service.config.visibility, VisibilityEnum)
else service.config.visibility
)
key = f"services:{visibility}|{service.type}:{service.id}@{service.app_id}"

# Check if the service exists before removal
Expand Down Expand Up @@ -1763,9 +1775,7 @@ async def unload(self, context=None):
winfo = await self.load_workspace_info(ws)
# Mark the workspace as not ready
winfo.status = None
await self._redis.hset(
"workspaces", winfo.id, winfo.model_dump_json()
)
await self._redis.hset("workspaces", winfo.id, winfo.model_dump_json())
# list all the clients in the workspace and send a meesage to delete them
client_keys = await self._list_client_keys(winfo.id)
if len(client_keys) > 0:
Expand All @@ -1779,8 +1789,6 @@ async def unload(self, context=None):
)
await self._event_bus.emit(f"unload:{ws}", "Unloading workspace: " + ws)



if not winfo.persistent:
# delete all the items in redis starting with `workspaces_name:`
# Including the queue and other associated resources
Expand Down Expand Up @@ -1878,9 +1886,13 @@ async def cleanup(
raise PermissionError(
f"Permission denied for workspace {workspace}, user: {user_info.id}"
)
logger.info(f"Cleaning up workspace {workspace_info.id}...")
# list all the clients and ping them
# If they are not responding, delete them
client_keys = await self._list_client_keys(workspace)
if len(client_keys) <= 0:
await self.delete_workspace(workspace_info.id, context=context)
return {"removed_workspace": 1, "removed_clients": 0}
removed = []
summary = {}
if not workspace_info.persistent:
Expand Down
1 change: 1 addition & 0 deletions hypha/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide the http proxy."""

import inspect
import json
import traceback
Expand Down
1 change: 1 addition & 0 deletions hypha/migrations/versions/9096f050eb04_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2024-11-15 05:58:12.181737
"""

from typing import Sequence, Union

from alembic import op
Expand Down
1 change: 1 addition & 0 deletions hypha/minio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A module for minio client operations."""

import asyncio
import json
import logging
Expand Down
1 change: 1 addition & 0 deletions hypha/plugin_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""imjoy plugin parser module."""

import json
import uuid

Expand Down
1 change: 1 addition & 0 deletions hypha/runner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide main entrypoint."""

import asyncio
import inspect
import json
Expand Down
1 change: 1 addition & 0 deletions hypha/runner/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Plugin runner."""

from . import start_runner

if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions hypha/runner/browser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide a browser runner."""

import uuid
import asyncio
import logging
Expand Down
1 change: 1 addition & 0 deletions hypha/s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide an s3 interface."""

import asyncio
import json
import logging
Expand Down
1 change: 1 addition & 0 deletions hypha/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide the server."""

import argparse
import logging
import sys
Expand Down
1 change: 1 addition & 0 deletions hypha/taskiq_utils/redis_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
We need to patch it so we can use fakeredis when redis is not available.
The library was created by Pavel Kirilin, released under the MIT license.
"""

from fakeredis import aioredis
from typing import (
Dict,
Expand Down
1 change: 1 addition & 0 deletions hypha/taskiq_utils/redis_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
We need to patch it so we can use fakeredis when redis is not available.
The library was created by Pavel Kirilin, released under the MIT license.
"""

from fakeredis import aioredis
from logging import getLogger
from typing import AsyncGenerator, Callable, Optional, TypeVar
Expand Down
1 change: 1 addition & 0 deletions hypha/taskiq_utils/schedule_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
We need to patch it so we can use fakeredis when redis is not available.
The library was created by Pavel Kirilin, released under the MIT license.
"""

from fakeredis import aioredis
from typing import List, Optional

Expand Down
1 change: 1 addition & 0 deletions hypha/triton.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide the triton proxy."""

import random
from typing import Any, List

Expand Down
1 change: 1 addition & 0 deletions hypha/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide utilities that should not be aware of hypha."""

import copy
import asyncio
import gzip
Expand Down
2 changes: 1 addition & 1 deletion requirements_lint.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black==23.7.0
black==24.10.0
flake8==7.1.1
flake8-docstrings==1.7.0
pylint==3.2.6
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Set up the hypha package."""

import json
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the hypha module."""

import uuid

SIO_PORT = 38283
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provide common pytest fixtures."""

import asyncio
import os
import shutil
Expand Down
Loading

0 comments on commit 0237b8f

Please sign in to comment.