Merge pull request 'Potentially fixed bug to kill web api on restart #301' (#302) from #301 into support

Reviewed-on: sh-edraft.de/kd_discord_bot#302
Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
Closes #301
This commit is contained in:
Sven Heidemann 2023-04-12 20:09:05 +02:00
commit 629556b5fc
3 changed files with 22 additions and 2 deletions

View File

@ -65,6 +65,9 @@ class Application(DiscordBotApplicationABC):
self._is_stopping = True
try:
self._logger.info(__name__, f"Try to stop {DiscordBotService.__name__}")
if self._feature_flags.get_flag(FeatureFlagsEnum.api_module):
self._api.stop()
await self._bot.close()
self._logger.info(__name__, f"Stopped {DiscordBotService.__name__}")
except Exception as e:

View File

@ -1,8 +1,9 @@
import socket
import sys
import textwrap
import uuid
from functools import partial
from typing import Union
from typing import Union, Optional
import eventlet
from cpl_core.dependency_injection import ServiceProviderABC
@ -60,6 +61,8 @@ class Api(Flask):
self._socketio.on_event("connect", self.on_connect)
self._socketio.on_event("disconnect", self.on_disconnect)
self._socket: Optional[socket] = None
self._requests = {}
@staticmethod
@ -152,7 +155,14 @@ class Api(Flask):
# from waitress import serve
# https://docs.pylonsproject.org/projects/waitress/en/stable/arguments.html
# serve(self, host=self._apt_settings.host, port=self._apt_settings.port, threads=10, connection_limit=1000, channel_timeout=10)
wsgi.server(eventlet.listen((self._api_settings.host, self._api_settings.port)), self, log_output=False)
self._socket = eventlet.listen((self._api_settings.host, self._api_settings.port))
wsgi.server(self._socket, self, log_output=False)
def stop(self):
if self._socket is None:
return
self._socket.shutdown(socket.SHUT_RDWR)
self._socket.close()
def on_connect(self):
self._logger.info(__name__, f"Client connected")

View File

@ -17,3 +17,10 @@ class ApiThread(threading.Thread):
self._api.start()
except Exception as e:
self._logger.error(__name__, "Start failed", e)
def stop(self):
try:
self._logger.trace(__name__, f"Try to stop {type(self._api).__name__}")
self._api.stop()
except Exception as e:
self._logger.error(__name__, "Stop failed", e)