Skip to content

Commit 50a4f28

Browse files
committed
feat: make command optional, only proxy on good faith
1 parent 7a0e3b9 commit 50a4f28

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

jupyter_server_proxy/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class _Proxy(SuperviseAndProxyHandler):
2626
def __init__(self, *args, **kwargs):
2727
super().__init__(*args, **kwargs)
2828
self.name = name
29+
self.command = command
2930
self.proxy_base = name
3031
self.absolute_url = absolute_url
3132
self.requested_port = port
@@ -62,7 +63,7 @@ def _realize_rendered_template(self, attribute):
6263
return self._render_template(attribute)
6364

6465
def get_cmd(self):
65-
return self._realize_rendered_template(command)
66+
return self._realize_rendered_template(self.command)
6667

6768
def get_env(self):
6869
return self._realize_rendered_template(environment)
@@ -121,7 +122,7 @@ def make_server_process(name, server_process_config, serverproxy_config):
121122
le = server_process_config.get('launcher_entry', {})
122123
return ServerProcess(
123124
name=name,
124-
command=server_process_config['command'],
125+
command=server_process_config.get('command', list()),
125126
environment=server_process_config.get('environment', {}),
126127
timeout=server_process_config.get('timeout', 5),
127128
absolute_url=server_process_config.get('absolute_url', False),
@@ -152,12 +153,16 @@ class ServerProxy(Configurable):
152153
153154
Value should be a dictionary with the following keys:
154155
command
155-
A list of strings that should be the full command to be executed.
156+
An optional list of strings that should be the full command to be executed.
156157
The optional template arguments {{port}} and {{base_url}} will be substituted with the
157158
port the process should listen on and the base-url of the notebook.
158159
159160
Could also be a callable. It should return a list.
160161
162+
If the command is not specified or is an empty list, it is assumed the process is already
163+
running, therefore the port checking is skipped and the proxy is set up on the specified
164+
port.
165+
161166
environment
162167
A dictionary of environment variable mappings. As with the command
163168
traitlet, {{port}} and {{base_url}} will be substituted.

jupyter_server_proxy/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ class SuperviseAndProxyHandler(LocalProxyHandler):
558558
def __init__(self, *args, **kwargs):
559559
self.requested_port = 0
560560
self.mappath = {}
561+
self.command = list()
561562
super().__init__(*args, **kwargs)
562563

563564
def initialize(self, state):
@@ -573,11 +574,14 @@ def port(self):
573574
Allocate either the requested port or a random empty port for use by
574575
application
575576
"""
576-
if 'port' not in self.state:
577+
if 'port' not in self.state and self.command:
577578
sock = socket.socket()
578579
sock.bind(('', self.requested_port))
579580
self.state['port'] = sock.getsockname()[1]
580581
sock.close()
582+
elif 'port' not in self.state:
583+
self.state['port'] = self.requested_port
584+
581585
return self.state['port']
582586

583587
def get_cwd(self):

0 commit comments

Comments
 (0)