implement a shutdown routine instead of just waiting for all plugins to stop on their own

This commit is contained in:
AAGaming
2024-07-03 20:08:30 -04:00
parent b32fa7948f
commit 2c4edeb810
8 changed files with 121 additions and 22 deletions
@@ -1,5 +1,5 @@
from os import path, environ
from signal import SIGINT, signal
from signal import SIG_IGN, SIGINT, SIGTERM, signal
from importlib.util import module_from_spec, spec_from_file_location
from json import dumps, loads
from logging import getLogger
@@ -39,13 +39,14 @@ class SandboxedPlugin:
self.author = author
self.api_version = api_version
self.log = getLogger("plugin")
self.log = getLogger("sandboxed_plugin")
def initialize(self, socket: LocalSocket):
self._socket = socket
try:
signal(SIGINT, lambda s, f: exit(0))
signal(SIGINT, SIG_IGN)
signal(SIGTERM, SIG_IGN)
set_event_loop(new_event_loop())
if self.passive:
@@ -112,10 +113,10 @@ class SandboxedPlugin:
else:
get_event_loop().create_task(self.Plugin._main(self.Plugin))
get_event_loop().create_task(socket.setup_server())
get_event_loop().run_forever()
except:
self.log.error("Failed to start " + self.name + "!\n" + format_exc())
exit(0)
get_event_loop().run_forever()
async def _unload(self):
try:
@@ -130,7 +131,7 @@ class SandboxedPlugin:
self.log.info("Could not find \"_unload\" in " + self.name + "'s main.py" + "\n")
except:
self.log.error("Failed to unload " + self.name + "!\n" + format_exc())
exit(0)
pass
async def _uninstall(self):
try:
@@ -145,13 +146,13 @@ class SandboxedPlugin:
self.log.info("Could not find \"_uninstall\" in " + self.name + "'s main.py" + "\n")
except:
self.log.error("Failed to uninstall " + self.name + "!\n" + format_exc())
exit(0)
pass
async def on_new_message(self, message : str) -> str|None:
data = loads(message)
if "stop" in data:
self.log.info("Calling Loader unload function.")
self.log.info(f"Calling Loader unload function for {self.name}.")
await self._unload()
if data.get('uninstall'):
@@ -160,9 +161,9 @@ class SandboxedPlugin:
get_event_loop().stop()
while get_event_loop().is_running():
await sleep(0)
await sleep(0.1)
get_event_loop().close()
raise Exception("Closing message listener")
exit(0)
d: SocketResponseDict = {"type": SocketMessageType.RESPONSE, "res": None, "success": True, "id": data["id"]}
try: