Skip to content

Commit

Permalink
Refactor fps_yjs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 23, 2023
1 parent 095c375 commit 8ffbbf1
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 225 deletions.
6 changes: 6 additions & 0 deletions jupyverse_api/jupyverse_api/contents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ async def get_path(self, file_id: str) -> str:
async def get_id(self, file_path: str) -> str:
...

def watch(self, path: str):
...

def unwatch(self, path: str, watcher):
...


class Contents(Router, ABC):
def __init__(self, app: App, auth: Auth):
Expand Down
24 changes: 7 additions & 17 deletions jupyverse_api/jupyverse_api/yjs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ def __init__(self, app: App, auth: Auth):

router = APIRouter()

@router.websocket("/api/yjs/{path:path}")
async def yjs_websocket(
path,
websocket_permissions=Depends(
auth.websocket_auth(permissions={"yjs": ["read", "write"]})
),
):
return await self.yjs_websocket(path, websocket_permissions)

@router.websocket("/api/collaboration/room/{path:path}")
async def collaboration_room_websocket(
path,
Expand All @@ -45,14 +36,6 @@ async def create_roomid(

self.include_router(router)

@abstractmethod
async def yjs_websocket(
self,
path,
websocket_permissions,
):
...

@abstractmethod
async def collaboration_room_websocket(
self,
Expand All @@ -70,3 +53,10 @@ async def create_roomid(
user: User,
):
...

@abstractmethod
def get_document(
self,
document_id: str,
):
...
3 changes: 3 additions & 0 deletions plugins/auth/fps_auth/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def get_db(auth_config: _AuthConfig) -> Res:
userdb_path.unlink()
if secret_path.is_file():
secret_path.unlink()
if auth_config.mode == "token":
if secret_path.is_file():
secret_path.unlink()

if not secret_path.is_file():
secret_path.write_text(secrets.token_hex(32))
Expand Down
20 changes: 11 additions & 9 deletions plugins/contents/fps_contents/fileid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Set
from uuid import uuid4

import aiosqlite
Expand Down Expand Up @@ -101,8 +101,8 @@ async def watch_files(self):
async for changes in awatch(".", stop_event=self.stop_watching_files):
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
deleted_paths = []
added_paths = []
deleted_paths = set()
added_paths = set()
for change, changed_path in changes:
# get relative path
changed_path = Path(changed_path).relative_to(await Path().absolute())
Expand Down Expand Up @@ -147,16 +147,18 @@ async def watch_files(self):
(mtime, changed_path_str),
)

for path in deleted_paths + added_paths:
for path in deleted_paths - added_paths:
logger.debug("Unindexing file %s ", path)
await db.execute("DELETE FROM fileids WHERE path = ?", (path,))
await db.commit()

for change in changes:
changed_path = change[1]
# get relative path
changed_path = str(Path(changed_path).relative_to(await Path().absolute()))
for watcher in self.watchers.get(changed_path, []):
watcher.notify(change)
relative_changed_path = str(Path(changed_path).relative_to(await Path().absolute()))
relative_change = (change[0], relative_changed_path)
for watcher in self.watchers.get(relative_changed_path, []):
watcher.notify(relative_change)

self.stopped_watching_files.set()

Expand Down Expand Up @@ -184,7 +186,7 @@ async def get_mtime(path, db) -> Optional[float]:


async def maybe_rename(
db, changed_path: str, changed_paths: List[str], other_paths: List[str], is_added_path
db, changed_path: str, changed_paths: Set[str], other_paths: Set[str], is_added_path: bool
) -> None:
# check if the same file was added/deleted, this would be a rename
db_or_fs1, db_or_fs2 = db, None
Expand All @@ -204,4 +206,4 @@ async def maybe_rename(
await db.execute("UPDATE fileids SET path = ? WHERE path = ?", (path2, path1))
other_paths.remove(other_path)
return
changed_paths.append(changed_path)
changed_paths.add(changed_path)
4 changes: 0 additions & 4 deletions plugins/kernels/fps_kernels/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations
import asyncio
import logging
from collections.abc import AsyncGenerator
from pathlib import Path
from typing import Optional
Expand All @@ -16,9 +15,6 @@
from .routes import _Kernels


logger = logging.getLogger("kernels")


class KernelsComponent(Component):
def __init__(self, **kwargs):
self.kernels_config = KernelsConfig(**kwargs)
Expand Down
6 changes: 5 additions & 1 deletion plugins/kernels/fps_kernels/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import uuid
from http import HTTPStatus
from pathlib import Path
Expand All @@ -24,6 +25,9 @@
)


logger = logging.getLogger("kernels")


class _Kernels(Kernels):
def __init__(
self,
Expand Down Expand Up @@ -231,7 +235,7 @@ async def execute_cell(
r = await request.json()
execution = Execution(**r)
if kernel_id in kernels:
ynotebook = self.yjs.websocket_server.get_room(execution.document_id).document
ynotebook = self.yjs.get_document(execution.document_id)
cell = ynotebook.get_cell(execution.cell_idx)
cell["outputs"] = []

Expand Down
2 changes: 2 additions & 0 deletions plugins/yjs/fps_yjs/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations

from collections.abc import AsyncGenerator
from typing import Optional

Expand Down Expand Up @@ -29,5 +30,6 @@ async def start(

yield

yjs.room_manager.stop()
contents.file_id_manager.stop_watching_files.set()
await contents.file_id_manager.stopped_watching_files.wait()
Loading

0 comments on commit 8ffbbf1

Please sign in to comment.