From 512d72e97a212065156bf133c9e6607e9d9dbda1 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 25 Oct 2019 15:36:10 +0200 Subject: [PATCH] improve plug/mod cleanup strategy --- boswatch/router/route.py | 4 +++- boswatch/router/routerManager.py | 27 +++++++++++++++++---------- bw_server.py | 2 +- module/module.py | 4 ++-- plugin/plugin.py | 4 ++-- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/boswatch/router/route.py b/boswatch/router/route.py index 1161e0e..4077076 100644 --- a/boswatch/router/route.py +++ b/boswatch/router/route.py @@ -18,13 +18,15 @@ class Route: """!Class for single routing points""" - def __init__(self, name, callback, statsCallback=None): + def __init__(self, name, callback, statsCallback=None, cleanupCallback=None): """!Create a instance of an route point @param name: name of the route point @param callback: instance of the callback function @param statsCallback: instance of the callback to get statistics (None) + @param cleanupCallback: instance of the callback to run a cleanup method (None) """ self.name = name self.callback = callback self.statistics = statsCallback + self.cleanup = cleanupCallback diff --git a/boswatch/router/routerManager.py b/boswatch/router/routerManager.py index bf515d1..60a0b13 100644 --- a/boswatch/router/routerManager.py +++ b/boswatch/router/routerManager.py @@ -33,13 +33,6 @@ class RouterManager: self._routerDict = {} self._startTime = int(time.time()) - def __del__(self): - """!Destroy the internal routerDict - All routers and route point instances will be destroyed too - Also destroys all instances from modules or plugins""" - # destroy all routers (also destroys all instances of modules/plugins) - del self._routerDict - # if there is an error, router list would be empty (see tmp variable) def buildRouter(self, config): """!Initialize Routers from given config file @@ -79,12 +72,18 @@ class RouterManager: if routeType == "plugin": importedFile = importlib.import_module(routeType + "." + routeRes) loadedClass = importedFile.BoswatchPlugin(routeConfig) - routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run, loadedClass._getStatistics)) + routerDict_tmp[routerName].addRoute(Route(routeName, + loadedClass._run, + loadedClass._getStatistics, + loadedClass._cleanup)) elif routeType == "module": importedFile = importlib.import_module(routeType + "." + routeRes) loadedClass = importedFile.BoswatchModule(routeConfig) - routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run, loadedClass._getStatistics)) + routerDict_tmp[routerName].addRoute(Route(routeName, + loadedClass._run, + loadedClass._getStatistics, + loadedClass._cleanup)) elif routeType == "router": routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter)) @@ -119,6 +118,14 @@ class RouterManager: self._saveStats() # write stats to stats file + def cleanup(self): + """!Run cleanup routines for all loaded route points""" + for name, routerObject in self._routerDict.items(): + logging.debug("Start cleanup for %s", name) + for routePoint in routerObject.routeList: + if routePoint.cleanup: + routePoint.cleanup() + def _showRouterRoute(self): """!Show the routes of all routers""" for name, routerObject in self._routerDict.items(): @@ -133,7 +140,7 @@ class RouterManager: lines = [] for name, routerObject in self._routerDict.items(): lines.append("[" + name + "]") - lines.append("loaded route points: " + str(len(routerObject.routeList))) + lines.append(" - Route points: " + str(len(routerObject.routeList))) for routePoint in routerObject.routeList: lines.append("[+] " + routePoint.name) if routePoint.statistics: diff --git a/bw_server.py b/bw_server.py index e9db7ee..4c62bed 100644 --- a/bw_server.py +++ b/bw_server.py @@ -112,7 +112,7 @@ except: # pragma: no cover finally: logging.debug("Starting shutdown routine") if bwRoutMan: - del bwRoutMan + bwRoutMan.cleanup() if bwServer: bwServer.stop() if bcServer: diff --git a/module/module.py b/module/module.py index ad0ede2..05fd317 100644 --- a/module/module.py +++ b/module/module.py @@ -42,8 +42,8 @@ class Module: logging.debug("[%s] onLoad()", moduleName) self.onLoad() - def __del__(self): - """!Destructor calls onUnload() directly""" + def _cleanup(self): + """!Cleanup routine calls onUnload() directly""" logging.debug("[%s] onUnload()", self._moduleName) self._modulesActive.remove(self) self.onUnload() diff --git a/plugin/plugin.py b/plugin/plugin.py index 9101a96..bf60771 100644 --- a/plugin/plugin.py +++ b/plugin/plugin.py @@ -52,8 +52,8 @@ class Plugin: logging.debug("[%s] onLoad()", pluginName) self.onLoad() - def __del__(self): - """!Destructor calls onUnload() directly""" + def _cleanup(self): + """!Cleanup routine calls onUnload() directly""" logging.debug("[%s] onUnload()", self._pluginName) self._pluginsActive.remove(self) self.onUnload()