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

View File

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

View File

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