Move cleanup to after thread wait; Figured out why the webserver was exiting early

This commit is contained in:
Raoul Snyman 2018-01-04 14:03:15 -07:00
parent db7ff02abb
commit 0601cf1543
3 changed files with 29 additions and 15 deletions

View File

@ -27,7 +27,7 @@ import logging
import time
from PyQt5 import QtCore, QtWidgets
from waitress import serve
from waitress.server import create_server
from openlp.core.api.deploy import download_and_check, download_sha256
from openlp.core.api.endpoint.controller import controller_endpoint, api_controller_endpoint
@ -61,11 +61,19 @@ class HttpWorker(ThreadWorker):
port = Settings().value('api/port')
Registry().execute('get_website_version')
try:
serve(application, host=address, port=port)
self.server = create_server(application, host=address, port=port)
self.server.run()
except OSError:
log.exception('An error occurred when serving the application.')
self.quit.emit()
def stop(self):
"""
A method to stop the worker
"""
if hasattr(self, 'server'):
self.server.close()
class HttpServer(RegistryBase, RegistryProperties, LogMixin):
"""

View File

@ -29,7 +29,6 @@ import logging
import time
import websockets
from PyQt5 import QtCore
from openlp.core.common.mixins import LogMixin, RegistryProperties
from openlp.core.common.registry import Registry
@ -59,12 +58,11 @@ class WebSocketWorker(ThreadWorker):
self.ws_server.start_server()
self.quit.emit()
@QtCore.pyqtSlot()
def stop(self):
"""
Stop the websocket server
"""
self.ws_server.stop = True
self.ws_server.stop_server()
class WebSocketServer(RegistryProperties, LogMixin):
@ -97,6 +95,15 @@ class WebSocketServer(RegistryProperties, LogMixin):
else:
log.debug('Failed to start ws server on port {port}'.format(port=port))
def stop_server(self):
"""
Stop the websocket server
"""
if hasattr(self.ws_server, 'ws_server'):
self.ws_server.ws_server.close()
elif hasattr(self.ws_server, 'server'):
self.ws_server.server.close()
def start_websocket_instance(self, address, port):
"""
Start the server

View File

@ -499,8 +499,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties):
Settings().set_up_default_values()
self.about_form = AboutForm(self)
MediaController()
websockets.WebSocketServer()
server.HttpServer()
self.ws_server = websockets.WebSocketServer()
self.http_server = server.HttpServer(self)
SettingsForm(self)
self.formatting_tag_form = FormattingTagForm(self)
self.shortcut_form = ShortcutListForm(self)
@ -560,7 +560,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties):
for thread_name in self.threads.keys():
self.application.processEvents()
thread = self.threads[thread_name]['thread']
worker = self.threads[thread_name]['worker']
try:
if worker and hasattr(worker, 'stop'):
# If the worker has a stop method, run it
worker.stop()
if thread and thread.isRunning():
# If the thread is running, let's wait 5 seconds for it
retry = 0
@ -1028,20 +1032,14 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties):
if not self.application.is_event_loop_active:
event.ignore()
return
# If we just did a settings import, close without saving changes.
if self.settings_imported:
self.clean_up(False)
event.accept()
if self.service_manager_contents.is_modified():
ret = self.service_manager_contents.save_modified_service()
if ret == QtWidgets.QMessageBox.Save:
if self.service_manager_contents.decide_save_method():
self.clean_up()
event.accept()
else:
event.ignore()
elif ret == QtWidgets.QMessageBox.Discard:
self.clean_up()
event.accept()
else:
event.ignore()
@ -1057,15 +1055,16 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties):
close_button.setText(translate('OpenLP.MainWindow', '&Exit OpenLP'))
msg_box.setDefaultButton(QtWidgets.QMessageBox.Close)
if msg_box.exec() == QtWidgets.QMessageBox.Close:
self.clean_up()
event.accept()
else:
event.ignore()
else:
self.clean_up()
event.accept()
if event.isAccepted():
# Wait for all the threads to complete
self._wait_for_threads()
# If we just did a settings import, close without saving changes.
self.clean_up(save_settings=not self.settings_imported)
def clean_up(self, save_settings=True):
"""