Skip to content

Commit 303c7d9

Browse files
committed
#786: updates to shutdown
1 parent 57f3721 commit 303c7d9

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

dtale/app.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,21 @@ def shutdown_server():
423423
logger.info("Executing shutdown...")
424424
func = request.environ.get("werkzeug.server.shutdown")
425425
if func is None:
426-
raise RuntimeError("Not running with the Werkzeug Server")
427-
func()
426+
logger.info(
427+
"Not running with the Werkzeug Server, exiting by searching gc for BaseWSGIServer"
428+
)
429+
import gc
430+
from werkzeug.serving import BaseWSGIServer
431+
432+
for obj in gc.get_objects():
433+
try:
434+
if isinstance(obj, BaseWSGIServer):
435+
obj.shutdown()
436+
break
437+
except Exception as e:
438+
logger.error(e)
439+
else:
440+
func()
428441
global_state.cleanup()
429442
ACTIVE_PORT = None
430443
ACTIVE_HOST = None
@@ -830,6 +843,11 @@ def _start():
830843
if is_active:
831844
_start()
832845
else:
846+
# import multiprocessing
847+
#
848+
# p = multiprocessing.Process(target=_start, args=())
849+
# p.start()
850+
833851
_thread.start_new_thread(_start, ())
834852

835853
if final_options["notebook"]:

dtale/views.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ def kill(base):
192192
This function fires a request to this instance's 'shutdown' route to kill it
193193
194194
"""
195-
requests.get(build_shutdown_url(base))
195+
try:
196+
requests.get(build_shutdown_url(base))
197+
except BaseException:
198+
logger.info("Shutdown complete")
196199

197200

198201
def is_up(base):

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Flask<2.3; python_version >= '3.7'
2323
Flask-Compress
2424
flask-ngrok; python_version > '3.0'
2525
future>=0.14.0
26+
immutables<=0.19; python_version == '3.6'
2627
itsdangerous<=1.1.0; python_version < '3.7'
2728
itsdangerous; python_version >= '3.7'
2829
# required for loading scikit-learn
@@ -45,7 +46,8 @@ matplotlib<=3.3.4; python_version == '3.4'
4546
matplotlib<=3.3.4; python_version == '3.5'
4647
matplotlib<=3.3.4; python_version == '3.6'
4748
matplotlib<=3.5.3; python_version == '3.7'
48-
matplotlib; python_version >= '3.8'
49+
matplotlib<=3.7.2; python_version == '3.8'
50+
matplotlib; python_version >= '3.9'
4951
missingno
5052
networkx<=2.2; python_version <= '3.4'
5153
networkx<=2.4; python_version == '3.5'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def run_tests(self):
8383
install_requires=read_file("requirements.txt"),
8484
extras_require={
8585
"arctic": ["arctic <= 1.79.4"],
86-
"arcticdb": ["arcticdb"],
86+
"arcticdb": ["arcticdb != 1.6.1"],
8787
"dash-bio": [
8888
"ParmEd==3.4.3; python_version == '3.6'",
8989
"dash-bio; python_version > '3.0'",

tests/dtale/test_views.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,33 @@ def import_mock(name, *args, **kwargs):
435435

436436
@pytest.mark.unit
437437
def test_shutdown(unittest):
438+
import werkzeug
439+
438440
with app.test_client() as c:
439441
try:
440-
c.get("/shutdown")
442+
with ExitStack() as stack:
443+
from werkzeug.serving import BaseWSGIServer
444+
445+
base_server = mock.Mock(spec=BaseWSGIServer)
446+
base_server.shutdown = mock.Mock()
447+
gc_objects = [base_server]
448+
mock_gc = stack.enter_context(
449+
mock.patch("gc.get_objects", mock.Mock(return_value=gc_objects))
450+
)
451+
resp = c.get("/shutdown").data
452+
assert "Server shutting down..." in str(resp)
453+
mock_gc.assert_called()
454+
base_server.shutdown.assert_called()
441455
unittest.fail()
442456
except: # noqa
443457
pass
444-
mock_shutdown = mock.Mock()
445-
resp = c.get(
446-
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
447-
).data
448-
assert "Server shutting down..." in str(resp)
449-
mock_shutdown.assert_called()
458+
if parse_version(werkzeug.__version__) < parse_version("2.1.0"):
459+
mock_shutdown = mock.Mock()
460+
resp = c.get(
461+
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
462+
).data
463+
assert "Server shutting down..." in str(resp)
464+
mock_shutdown.assert_called()
450465

451466

452467
@pytest.mark.unit

0 commit comments

Comments
 (0)