Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start Y Websocket Server earlier and make it resilient to crashes #294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions projects/jupyter-server-ydoc/jupyter_server_ydoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from jupyter_ydoc.ybasedoc import YBaseDoc
from pycrdt import Doc
from pycrdt_websocket.ystore import BaseYStore
from traitlets import Bool, Float, Type
from pycrdt_websocket.websocket_server import exception_logger
from traitlets import Bool, Float, Type, Set, Instance

from .handlers import DocSessionHandler, YDocWebSocketHandler
from .loaders import FileLoaderMapping
Expand All @@ -24,7 +25,6 @@
)
from .websocketserver import JupyterWebsocketServer, RoomNotFound


class YDocExtension(ExtensionApp):
name = "jupyter_server_ydoc"
app_name = "Collaboration"
Expand Down Expand Up @@ -75,6 +75,10 @@ class YDocExtension(ExtensionApp):
model.""",
)

_running_ywebsocket_server = Instance(asyncio.Task, allow_none=True)
ywebsocket_server = Instance(JupyterWebsocketServer, allow_none=True)


def initialize(self):
super().initialize()
self.serverapp.event_logger.register_event_schema(EVENTS_SCHEMA_PATH)
Expand All @@ -90,6 +94,14 @@ def initialize_settings(self):
}
)

self.ywebsocket_server = JupyterWebsocketServer(
rooms_ready=False,
auto_clean_rooms=False,
ystore_class=self.ystore_class,
log=self.log,
exception_logger=exception_logger
)

def initialize_handlers(self):
self.serverapp.web_app.settings.setdefault(
"page_config_data",
Expand All @@ -103,13 +115,6 @@ def initialize_handlers(self):
for k, v in self.config.get(self.ystore_class.__name__, {}).items():
setattr(self.ystore_class, k, v)

self.ywebsocket_server = JupyterWebsocketServer(
rooms_ready=False,
auto_clean_rooms=False,
ystore_class=self.ystore_class,
log=self.log,
)

# self.settings is local to the ExtensionApp but here we need
# the global app settings in which the file id manager will register
# itself maybe at a later time.
Expand All @@ -134,6 +139,14 @@ def initialize_handlers(self):
]
)

async def start_extension(self):
"""Start the y-websocket server.
"""
self.log.info("Starting the Collaborative Document Server.")

# Start the websocket server
self._running_ywebsocket_server = asyncio.create_task(self.ywebsocket_server.start())

async def get_document(
self: YDocExtension,
*,
Expand Down
4 changes: 1 addition & 3 deletions projects/jupyter-server-ydoc/jupyter_server_ydoc/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def create_task(self, aw):
task.add_done_callback(self._background_tasks.discard)

async def prepare(self):
if not self._websocket_server.started.is_set():
self.create_task(self._websocket_server.start())
await self._websocket_server.started.wait()
await self._websocket_server.started.wait()

# Get room
self._room_id: str = room_id_from_encoded_path(self.request.path)
Expand Down
Loading