Skip to content

Commit

Permalink
#786: updates to shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
aschonfeld committed Aug 23, 2023
1 parent 4dbcb89 commit 340c33d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
22 changes: 20 additions & 2 deletions dtale/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,21 @@ def shutdown_server():
logger.info("Executing shutdown...")
func = request.environ.get("werkzeug.server.shutdown")
if func is None:
raise RuntimeError("Not running with the Werkzeug Server")
func()
logger.info(
"Not running with the Werkzeug Server, exiting by searching gc for BaseWSGIServer"
)
import gc
from werkzeug.serving import BaseWSGIServer

for obj in gc.get_objects():
try:
if isinstance(obj, BaseWSGIServer):
obj.shutdown()
break
except Exception as e:
logger.error(e)
else:
func()
global_state.cleanup()
ACTIVE_PORT = None
ACTIVE_HOST = None
Expand Down Expand Up @@ -830,6 +843,11 @@ def _start():
if is_active:
_start()
else:
# import multiprocessing
#
# p = multiprocessing.Process(target=_start, args=())
# p.start()

_thread.start_new_thread(_start, ())

if final_options["notebook"]:
Expand Down
5 changes: 4 additions & 1 deletion dtale/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ def kill(base):
This function fires a request to this instance's 'shutdown' route to kill it
"""
requests.get(build_shutdown_url(base))
try:
requests.get(build_shutdown_url(base))
except BaseException:
logger.info("Shutdown complete")


def is_up(base):
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Flask<2.3; python_version >= '3.7'
Flask-Compress
flask-ngrok; python_version > '3.0'
future>=0.14.0
immutables<=0.19; python_version == '3.6'
itsdangerous<=1.1.0; python_version < '3.7'
itsdangerous; python_version >= '3.7'
# required for loading scikit-learn
Expand All @@ -45,7 +46,8 @@ matplotlib<=3.3.4; python_version == '3.4'
matplotlib<=3.3.4; python_version == '3.5'
matplotlib<=3.3.4; python_version == '3.6'
matplotlib<=3.5.3; python_version == '3.7'
matplotlib; python_version >= '3.8'
matplotlib<=3.7.2; python_version == '3.8'
matplotlib; python_version >= '3.9'
missingno
networkx<=2.2; python_version <= '3.4'
networkx<=2.4; python_version == '3.5'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run_tests(self):
install_requires=read_file("requirements.txt"),
extras_require={
"arctic": ["arctic <= 1.79.4"],
"arcticdb": ["arcticdb"],
"arcticdb": ["arcticdb != 1.6.1"],
"dash-bio": [
"ParmEd==3.4.3; python_version == '3.6'",
"dash-bio; python_version > '3.0'",
Expand Down
29 changes: 22 additions & 7 deletions tests/dtale/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,33 @@ def import_mock(name, *args, **kwargs):

@pytest.mark.unit
def test_shutdown(unittest):
import werkzeug

with app.test_client() as c:
try:
c.get("/shutdown")
with ExitStack() as stack:
from werkzeug.serving import BaseWSGIServer

base_server = mock.Mock(spec=BaseWSGIServer)
base_server.shutdown = mock.Mock()
gc_objects = [base_server]
mock_gc = stack.enter_context(
mock.patch("gc.get_objects", mock.Mock(return_value=gc_objects))
)
resp = c.get("/shutdown").data
assert "Server shutting down..." in str(resp)
mock_gc.assert_called()
base_server.shutdown.assert_called()
unittest.fail()
except: # noqa
pass
mock_shutdown = mock.Mock()
resp = c.get(
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
).data
assert "Server shutting down..." in str(resp)
mock_shutdown.assert_called()
if parse_version(werkzeug.__version__) < parse_version("2.1.0"):
mock_shutdown = mock.Mock()
resp = c.get(
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
).data
assert "Server shutting down..." in str(resp)
mock_shutdown.assert_called()


@pytest.mark.unit
Expand Down

0 comments on commit 340c33d

Please sign in to comment.