Skip to content

Commit 9d85ea5

Browse files
author
Joel Collins
committed
Fixed server app context
1 parent fde6181 commit 9d85ea5

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/labthings/wsgi.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def __init__(
3030
):
3131
self.app = app
3232
# Find LabThing attached to app
33-
self.labthing = current_labthing(app)
33+
with app.app_context():
34+
self.labthing = current_labthing(app)
3435

3536
# Server properties
3637
self.host = host
@@ -39,13 +40,13 @@ def __init__(
3940
self.zeroconf = zeroconf
4041

4142
# Servers
42-
self.wsgi_server = None
4343
self.zeroconf_server = None
4444
self.service_info = None
4545
self.service_infos = []
4646

4747
def _register_zeroconf(self):
4848
if self.labthing:
49+
print(f"Registering zeroconf {self.labthing.safe_title}._labthing._tcp.local.")
4950
# Get list of host addresses
5051
mdns_addresses = {
5152
socket.inet_aton(i)
@@ -82,22 +83,16 @@ def start(self):
8283
print(f"Running on http://{friendlyhost}:{self.port} (Press CTRL+C to quit)")
8384

8485
# Create WSGIServer
85-
run_simple(self.host, self.port, self.app, use_debugger=self.debug, threaded=True, processes=1)
86-
87-
# When server stops
88-
if self.zeroconf_server:
89-
logging.info("Unregistering zeroconf services")
90-
for service in self.service_infos:
91-
self.zeroconf_server.unregister_service(service)
92-
self.zeroconf_server.close()
93-
# Stop WSGI server with timeout
94-
if self.wsgi_server:
95-
logging.info("Shutting down WSGI server")
96-
self.wsgi_server.stop(timeout=5)
97-
# Clear started event
98-
if self.started.is_set():
99-
self.started.clear()
100-
logging.info("Done")
86+
try:
87+
run_simple(self.host, self.port, self.app, use_debugger=self.debug, threaded=True, processes=1)
88+
finally:
89+
# When server stops
90+
if self.zeroconf_server:
91+
print("Unregistering zeroconf services...")
92+
for service in self.service_infos:
93+
self.zeroconf_server.unregister_service(service)
94+
self.zeroconf_server.close()
95+
print("Server stopped")
10196

10297
def run(self, host=None, port=None, debug=None, zeroconf=None, **kwargs):
10398
"""Starts the server allowing for runtime parameters. Designed to immitate

0 commit comments

Comments
 (0)