mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2025-12-06 07:12:04 +01:00
improve plug/mod cleanup strategy
This commit is contained in:
parent
9ce4fd7420
commit
512d72e97a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue