fix incompatibility of flask reload and port searching (#793)

* WIP

* add --developer; fix incompatibility of --port and --debug

* put REST tests on separate ports

* PR review
This commit is contained in:
Bruce Martin
2019-05-29 16:38:57 -07:00
committed by GitHub
parent 862d8feb5e
commit 4b96b3a635
4 changed files with 26 additions and 12 deletions
+10 -3
View File
@@ -11,7 +11,7 @@ import click
from server.app.app import Server from server.app.app import Server
from server.app.util.errors import ScanpyFileError from server.app.util.errors import ScanpyFileError
from server.app.util.utils import custom_format_warning from server.app.util.utils import custom_format_warning
from server.utils.utils import find_available_port from server.utils.utils import find_available_port, is_port_available
# anything bigger than this will generate a special message # anything bigger than this will generate a special message
@@ -141,7 +141,14 @@ security risk by including the --scripts flag. Make sure you trust the scripts t
file_parts = splitext(basename(data)) file_parts = splitext(basename(data))
title = file_parts[0] title = file_parts[0]
if not port: if port:
if debug:
raise click.ClickException("--port and --debug may not be used together (try --verbose for error logging).")
if not is_port_available(host, int(port)):
raise click.ClickException(
f"The port selected {port} is in use, please specify an open port using the --port flag."
)
else:
port = find_available_port(host) port = find_available_port(host)
# Setup app # Setup app
@@ -199,7 +206,7 @@ security risk by including the --scripts flag. Make sure you trust the scripts t
sys.stdout = f sys.stdout = f
try: try:
server.app.run(host=host, debug=debug, port=port, threaded=True) server.app.run(host=host, debug=debug, port=port, threaded=True, use_debugger=False)
except OSError as e: except OSError as e:
if e.errno == errno.EADDRINUSE: if e.errno == errno.EADDRINUSE:
raise click.ClickException("Port is in use, please specify an open port using the --port flag.") from e raise click.ClickException("Port is in use, please specify an open port using the --port flag.") from e
+1 -1
View File
@@ -19,7 +19,7 @@ class EndPoints(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.ps = Popen(["cellxgene", "launch", "example-dataset/pbmc3k.h5ad", "--debug", "--port", "5005"]) cls.ps = Popen(["cellxgene", "launch", "example-dataset/pbmc3k.h5ad", "--verbose", "--port", "5005"])
session = requests.Session() session = requests.Session()
for i in range(90): for i in range(90):
try: try:
+2 -2
View File
@@ -8,7 +8,7 @@ import decode_fbs
import requests import requests
LOCAL_URL = "http://127.0.0.1:5005/" LOCAL_URL = "http://127.0.0.1:5006/"
VERSION = "v0.2" VERSION = "v0.2"
URL_BASE = f"{LOCAL_URL}api/{VERSION}/" URL_BASE = f"{LOCAL_URL}api/{VERSION}/"
@@ -21,7 +21,7 @@ class WithNaNs(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.ps = Popen( cls.ps = Popen(
["cellxgene", "launch", "server/test/test_datasets/nan.h5ad", "--debug", "--port", "5005"] ["cellxgene", "launch", "server/test/test_datasets/nan.h5ad", "--verbose", "--port", "5006"]
) )
session = requests.Session() session = requests.Session()
for i in range(90): for i in range(90):
+13 -6
View File
@@ -10,10 +10,17 @@ def find_available_port(host, port=5005):
# Takes approx 2 seconds to do a scan of 5000 ports on my laptop # Takes approx 2 seconds to do a scan of 5000 ports on my laptop
num_ports_to_try = 5000 num_ports_to_try = 5000
for port_to_try in range(port, port + num_ports_to_try): for port_to_try in range(port, port + num_ports_to_try):
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: if is_port_available(host, port_to_try):
try: return port_to_try
s.bind((host, port_to_try))
return port_to_try
except socket.error:
pass
raise socket.error(errno.EADDRINUSE, f"No port in range {port} - {port + num_ports_to_try - 1} available.") raise socket.error(errno.EADDRINUSE, f"No port in range {port} - {port + num_ports_to_try - 1} available.")
def is_port_available(host, port):
is_available = False
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
try:
s.bind((host, port))
is_available = True
except socket.error:
pass
return is_available