From 61f3fc948a38616170d57e853ad31a912486b322 Mon Sep 17 00:00:00 2001 From: Mimi Wang Date: Sun, 13 Apr 2025 11:59:21 -0700 Subject: [PATCH 1/3] Add `_update_last_activity` to the extension app Added `_update_last_activity` to the extension app to update the `last_activity_times` look up in the web application. Pass this method down to the scheduler so that it can be used to update the `last_acitivity` whenever a job is created. May want to add it elsewhere in the future, but this is a good start. --- jupyter_scheduler/extension.py | 12 ++++++++++++ jupyter_scheduler/scheduler.py | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/jupyter_scheduler/extension.py b/jupyter_scheduler/extension.py index 1a4ba3736..097763fce 100644 --- a/jupyter_scheduler/extension.py +++ b/jupyter_scheduler/extension.py @@ -1,4 +1,5 @@ import asyncio +from datetime import datetime, timezone from jupyter_core.paths import jupyter_data_dir from jupyter_server.extension.application import ExtensionApp @@ -78,6 +79,7 @@ def initialize_settings(self): environments_manager=environments_manager, db_url=self.db_url, config=self.config, + update_last_activity=self._update_last_activity, ) job_files_manager = self.job_files_manager_class(scheduler=scheduler) @@ -91,3 +93,13 @@ def initialize_settings(self): if scheduler.task_runner: loop = asyncio.get_event_loop() loop.create_task(scheduler.task_runner.start()) + + self._update_last_activity() + + def _update_last_activity(self): + """Update last activity in the web application + + Updates the `last_acitivity_times` dict in the web application. When running with jupyterhub, this prevents + the culler from thinking the server is idle. + """ + self.serverapp.web_app.settings["last_activity_times"]["jupyter_scheduler"] = datetime.now(timezone.utc) diff --git a/jupyter_scheduler/scheduler.py b/jupyter_scheduler/scheduler.py index 867034c60..c6c604fca 100644 --- a/jupyter_scheduler/scheduler.py +++ b/jupyter_scheduler/scheduler.py @@ -96,11 +96,17 @@ def _default_staging_path(self): ) def __init__( - self, root_dir: str, environments_manager: Type[EnvironmentManager], config=None, **kwargs + self, + root_dir: str, + environments_manager: Type[EnvironmentManager], + config=None, + update_last_activity=None, + **kwargs ): super().__init__(config=config, **kwargs) self.root_dir = root_dir self.environments_manager = environments_manager + self.update_last_activity = update_last_activity def create_job(self, model: CreateJob) -> str: """Creates a new job record, may trigger execution of the job. @@ -405,10 +411,15 @@ def __init__( environments_manager: Type[EnvironmentManager], db_url: str, config=None, + update_last_activity=None, **kwargs, ): super().__init__( - root_dir=root_dir, environments_manager=environments_manager, config=config, **kwargs + root_dir=root_dir, + environments_manager=environments_manager, + config=config, + update_last_activity=update_last_activity, + **kwargs ) self.db_url = db_url if self.task_runner_class: @@ -438,6 +449,7 @@ def copy_input_folder(self, input_uri: str, nb_copy_to_path: str) -> List[str]: ) def create_job(self, model: CreateJob) -> str: + self.update_last_activity() if not model.job_definition_id and not self.file_exists(model.input_uri): raise InputUriError(model.input_uri) From 89f58897684e7a3256b8809dcbd68c2b402bce15 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Apr 2025 19:09:55 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyter_scheduler/extension.py | 6 ++++-- jupyter_scheduler/scheduler.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/jupyter_scheduler/extension.py b/jupyter_scheduler/extension.py index 097763fce..4a36f7925 100644 --- a/jupyter_scheduler/extension.py +++ b/jupyter_scheduler/extension.py @@ -93,7 +93,7 @@ def initialize_settings(self): if scheduler.task_runner: loop = asyncio.get_event_loop() loop.create_task(scheduler.task_runner.start()) - + self._update_last_activity() def _update_last_activity(self): @@ -102,4 +102,6 @@ def _update_last_activity(self): Updates the `last_acitivity_times` dict in the web application. When running with jupyterhub, this prevents the culler from thinking the server is idle. """ - self.serverapp.web_app.settings["last_activity_times"]["jupyter_scheduler"] = datetime.now(timezone.utc) + self.serverapp.web_app.settings["last_activity_times"]["jupyter_scheduler"] = datetime.now( + timezone.utc + ) diff --git a/jupyter_scheduler/scheduler.py b/jupyter_scheduler/scheduler.py index c6c604fca..6b90f29d6 100644 --- a/jupyter_scheduler/scheduler.py +++ b/jupyter_scheduler/scheduler.py @@ -101,7 +101,7 @@ def __init__( environments_manager: Type[EnvironmentManager], config=None, update_last_activity=None, - **kwargs + **kwargs, ): super().__init__(config=config, **kwargs) self.root_dir = root_dir @@ -419,7 +419,7 @@ def __init__( environments_manager=environments_manager, config=config, update_last_activity=update_last_activity, - **kwargs + **kwargs, ) self.db_url = db_url if self.task_runner_class: From ef747e2debccbce00473db20115dd1efa0f2e92f Mon Sep 17 00:00:00 2001 From: Mimi Wang Date: Mon, 14 Apr 2025 06:14:17 -0700 Subject: [PATCH 3/3] fix test --- conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conftest.py b/conftest.py index 5c5849235..8de56f211 100644 --- a/conftest.py +++ b/conftest.py @@ -59,6 +59,7 @@ def jp_scheduler(jp_scheduler_db_url, jp_scheduler_root_dir, jp_scheduler_db): db_url=jp_scheduler_db_url, root_dir=str(jp_scheduler_root_dir), environments_manager=MockEnvironmentManager(), + update_last_activity=lambda: True, )