Skip to content

Add _update_last_activity to the extension app #582

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
14 changes: 14 additions & 0 deletions jupyter_scheduler/extension.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -91,3 +93,15 @@ 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
)
16 changes: 14 additions & 2 deletions jupyter_scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
Loading