improve plug/mod cleanup strategy

This commit is contained in:
Bastian Schroll 2019-10-25 15:36:10 +02:00
parent 9ce4fd7420
commit 512d72e97a
No known key found for this signature in database
GPG key ID: 0AE96912A20E9F5F
5 changed files with 25 additions and 16 deletions

View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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()

View file

@ -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()