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 Route:
"""!Class for single routing points""" """!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 """!Create a instance of an route point
@param name: name of the route point @param name: name of the route point
@param callback: instance of the callback function @param callback: instance of the callback function
@param statsCallback: instance of the callback to get statistics (None) @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.name = name
self.callback = callback self.callback = callback
self.statistics = statsCallback self.statistics = statsCallback
self.cleanup = cleanupCallback

View file

@ -33,13 +33,6 @@ class RouterManager:
self._routerDict = {} self._routerDict = {}
self._startTime = int(time.time()) 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) # if there is an error, router list would be empty (see tmp variable)
def buildRouter(self, config): def buildRouter(self, config):
"""!Initialize Routers from given config file """!Initialize Routers from given config file
@ -79,12 +72,18 @@ class RouterManager:
if routeType == "plugin": if routeType == "plugin":
importedFile = importlib.import_module(routeType + "." + routeRes) importedFile = importlib.import_module(routeType + "." + routeRes)
loadedClass = importedFile.BoswatchPlugin(routeConfig) 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": elif routeType == "module":
importedFile = importlib.import_module(routeType + "." + routeRes) importedFile = importlib.import_module(routeType + "." + routeRes)
loadedClass = importedFile.BoswatchModule(routeConfig) 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": elif routeType == "router":
routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter)) routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter))
@ -119,6 +118,14 @@ class RouterManager:
self._saveStats() # write stats to stats file 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): def _showRouterRoute(self):
"""!Show the routes of all routers""" """!Show the routes of all routers"""
for name, routerObject in self._routerDict.items(): for name, routerObject in self._routerDict.items():
@ -133,7 +140,7 @@ class RouterManager:
lines = [] lines = []
for name, routerObject in self._routerDict.items(): for name, routerObject in self._routerDict.items():
lines.append("[" + name + "]") 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: for routePoint in routerObject.routeList:
lines.append("[+] " + routePoint.name) lines.append("[+] " + routePoint.name)
if routePoint.statistics: if routePoint.statistics:

View file

@ -112,7 +112,7 @@ except: # pragma: no cover
finally: finally:
logging.debug("Starting shutdown routine") logging.debug("Starting shutdown routine")
if bwRoutMan: if bwRoutMan:
del bwRoutMan bwRoutMan.cleanup()
if bwServer: if bwServer:
bwServer.stop() bwServer.stop()
if bcServer: if bcServer:

View file

@ -42,8 +42,8 @@ class Module:
logging.debug("[%s] onLoad()", moduleName) logging.debug("[%s] onLoad()", moduleName)
self.onLoad() self.onLoad()
def __del__(self): def _cleanup(self):
"""!Destructor calls onUnload() directly""" """!Cleanup routine calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._moduleName) logging.debug("[%s] onUnload()", self._moduleName)
self._modulesActive.remove(self) self._modulesActive.remove(self)
self.onUnload() self.onUnload()

View file

@ -52,8 +52,8 @@ class Plugin:
logging.debug("[%s] onLoad()", pluginName) logging.debug("[%s] onLoad()", pluginName)
self.onLoad() self.onLoad()
def __del__(self): def _cleanup(self):
"""!Destructor calls onUnload() directly""" """!Cleanup routine calls onUnload() directly"""
logging.debug("[%s] onUnload()", self._pluginName) logging.debug("[%s] onUnload()", self._pluginName)
self._pluginsActive.remove(self) self._pluginsActive.remove(self)
self.onUnload() self.onUnload()