Skip to content

Commit

Permalink
increase timeout and handle timeout error (jupyter-server#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Chitneni authored and GitHub Enterprise committed Feb 24, 2022
1 parent b72ebda commit 1a141ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def datastudio_env(monkeypatch, project_id, notebook_id, app_base_url, ds_api_to
monkeypatch.setenv(constants.DS_NOTEBOOK_ID, notebook_id)
monkeypatch.setenv(constants.DS_API_URL, app_base_url)
monkeypatch.setenv(constants.DS_URL, app_base_url)
monkeypatch.setenv(constants.DS_API_REQUEST_TIMEOUT, "20")
monkeypatch.setenv("API_TOKEN", ds_api_token)


Expand Down
7 changes: 7 additions & 0 deletions data_studio_jupyter_extensions/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class DataStudioJupyterExtensions(ExtensionAppJinjaMixin, ExtensionApp):
help="The Notebook Server ID from the ACS Data Platform.",
).tag(config=True)

datastudio_api_request_timeout = IntFromEnv(
name=constants.DS_API_REQUEST_TIMEOUT,
default_value=120,
help="Request timeout talking to DATASTUDIO API",
).tag(config=True)

# List configurables here.
nbservice_client_class = Type(
default_value=NotebookServiceClient,
Expand Down Expand Up @@ -256,6 +262,7 @@ def initialize_configurables(self):
ssl_cert_file=self.ssl_cert_file,
project_id=self.project_id,
notebook_id=self.notebook_id,
request_timeout=self.datastudio_api_request_timeout,
)
handlers = []
if self.serverapp.log_level <= 10:
Expand Down
44 changes: 31 additions & 13 deletions data_studio_jupyter_extensions/configurables/notebook_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from data_studio_jupyter_extensions import constants
from data_studio_jupyter_extensions.configurables.hubble import hubble
from data_studio_jupyter_extensions.traits import IntFromEnv
from data_studio_jupyter_extensions.traits import UnicodeFromEnv
from data_studio_jupyter_extensions.utils import get_ssl_cert

Expand Down Expand Up @@ -47,6 +48,10 @@ class NotebookServiceClient(SingletonConfigurable):
)
request_token = Unicode(allow_none=True)

request_timeout = IntFromEnv(
name=constants.DS_API_REQUEST_TIMEOUT, allow_none=True
).tag(config=True)

http_client = Instance(AsyncHTTPClient).tag(config=True)

@default("http_client")
Expand Down Expand Up @@ -119,6 +124,7 @@ def _get_request(self, url, method, data):
body=json.dumps(data),
allow_nonstandard_methods=True,
ca_certs=self.ssl_cert_file,
request_timeout=float(self.request_timeout),
)

async def fetch(self, *parts, method="GET", data=None):
Expand Down Expand Up @@ -151,19 +157,31 @@ async def fetch(self, *parts, method="GET", data=None):
else:
# Get response from notebook service
response = err.response
error_message = (
"JupyterLab received the following response from Data Studio's Notebook Service.\n\n"
f"Code: {response.code}\n"
f"Reason: {response.reason}"
)
if response.body:
error_message += f"\nBody: {response.body.decode()}"
# Raise an internal server error, but bubble the
# error as a "message" string to the client.
# JupyterLab will show the log_message as a message
# in the Dialog UI.
# See here: https://github.com/jupyterlab/jupyterlab/blob/114159269a76c4a8d4c1e725e882d7c8a7791869/packages/apputils/src/sessioncontext.tsx#L968-L977
raise HTTPError(500, log_message=error_message)
code = err.code
if code != 599:
# Error code 599 is used when no HTTP response was received, e.g. for a timeout.
error_message = (
"JupyterLab received the following response from Data Studio's Notebook Service.\n\n"
f"Code: {response.code}\n"
f"Reason: {response.reason}"
)
if response.body:
error_message += f"\nBody: {response.body.decode()}"
# Raise an internal server error, but bubble the
# error as a "message" string to the client.
# JupyterLab will show the log_message as a message
# in the Dialog UI.
# See here: https://github.com/jupyterlab/jupyterlab/blob/114159269a76c4a8d4c1e725e882d7c8a7791869/packages/apputils/src/sessioncontext.tsx#L968-L977
raise HTTPError(500, log_message=error_message)
else:
error_message = (
"Request error communicating with backend services.\n\n"
f"Error: {err}\n"
)
raise HTTPError(
500,
log_message=error_message,
)
return response

@hubble("notebook_service_kernelspecs")
Expand Down
1 change: 1 addition & 0 deletions data_studio_jupyter_extensions/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
DS_PROCESS_ID = "DS_PROCESS_ID"
DS_SPEC_ID = "DS_SPEC_ID"
DS_NAMESPACE = "DS_NAMESPACE"
DS_API_REQUEST_TIMEOUT = "DATASTUDIO_API_REQUEST_TIMEOUT"
NB_PREFIX = "NB_PREFIX"
HBPORT = "HBPORT"
SHELLPORT = "SHELLPORT"
Expand Down

0 comments on commit 1a141ec

Please sign in to comment.