Stop requiring super call to activate event handlers in plugin start/stop methods

This commit is contained in:
Tulir Asokan 2019-07-13 15:48:13 +03:00
parent 7e6c51d18f
commit 8b0bd510f9
2 changed files with 12 additions and 4 deletions

View File

@ -173,7 +173,7 @@ class PluginInstance:
database=self.inst_db, webapp=self.inst_webapp, database=self.inst_db, webapp=self.inst_webapp,
webapp_url=self.inst_webapp_url) webapp_url=self.inst_webapp_url)
try: try:
await self.plugin.start() await self.plugin.internal_start()
except Exception: except Exception:
self.log.exception("Failed to start instance") self.log.exception("Failed to start instance")
self.db_instance.enabled = False self.db_instance.enabled = False
@ -190,7 +190,7 @@ class PluginInstance:
self.log.debug("Stopping plugin instance...") self.log.debug("Stopping plugin instance...")
self.started = False self.started = False
try: try:
await self.plugin.stop() await self.plugin.internal_stop()
except Exception: except Exception:
self.log.exception("Failed to stop instance") self.log.exception("Failed to stop instance")
self.plugin = None self.plugin = None

View File

@ -52,7 +52,7 @@ class Plugin(ABC):
self.webapp_url = webapp_url self.webapp_url = webapp_url
self._handlers_at_startup = [] self._handlers_at_startup = []
async def start(self) -> None: async def internal_start(self) -> None:
for key in dir(self): for key in dir(self):
val = getattr(self, key) val = getattr(self, key)
try: try:
@ -67,12 +67,20 @@ class Plugin(ABC):
self.webapp.add_route(method=method, path=path, handler=val, **kwargs) self.webapp.add_route(method=method, path=path, handler=val, **kwargs)
except AttributeError: except AttributeError:
pass pass
await self.start()
async def stop(self) -> None: async def start(self) -> None:
pass
async def internal_stop(self) -> None:
for func, event_type in self._handlers_at_startup: for func, event_type in self._handlers_at_startup:
self.client.remove_event_handler(event_type, func) self.client.remove_event_handler(event_type, func)
if self.webapp is not None: if self.webapp is not None:
self.webapp.clear() self.webapp.clear()
await self.stop()
async def stop(self) -> None:
pass
@classmethod @classmethod
def get_config_class(cls) -> Optional[Type['BaseProxyConfig']]: def get_config_class(cls) -> Optional[Type['BaseProxyConfig']]: