Try reloading failed instance dependencies when starting instance

This commit is contained in:
Tulir Asokan 2018-12-12 15:21:17 +02:00
parent 07fe46e7f9
commit 1552964d3f

View File

@ -73,21 +73,24 @@ class PluginInstance:
"config": self.db_instance.config, "config": self.db_instance.config,
} }
def load(self) -> None: def load(self) -> bool:
try: if not self.loader:
self.loader = PluginLoader.find(self.type) try:
except KeyError: self.loader = PluginLoader.find(self.type)
self.log.error(f"Failed to find loader for type {self.type}") except KeyError:
self.db_instance.enabled = False self.log.error(f"Failed to find loader for type {self.type}")
return self.db_instance.enabled = False
self.client = Client.get(self.primary_user) return False
if not self.client: if not self.client:
self.log.error(f"Failed to get client for user {self.primary_user}") self.client = Client.get(self.primary_user)
self.db_instance.enabled = False if not self.client:
return self.log.error(f"Failed to get client for user {self.primary_user}")
self.db_instance.enabled = False
return False
self.log.debug("Plugin instance dependencies loaded") self.log.debug("Plugin instance dependencies loaded")
self.loader.references.add(self) self.loader.references.add(self)
self.client.references.add(self) self.client.references.add(self)
return True
def delete(self) -> None: def delete(self) -> None:
if self.loader is not None: if self.loader is not None:
@ -117,6 +120,10 @@ class PluginInstance:
elif not self.enabled: elif not self.enabled:
self.log.warning("Plugin disabled, not starting.") self.log.warning("Plugin disabled, not starting.")
return return
if not self.client or not self.loader:
self.log.warning("Missing plugin instance dependencies, attempting to load...")
if not self.load():
return
cls = await self.loader.load() cls = await self.loader.load()
config_class = cls.get_config_class() config_class = cls.get_config_class()
if config_class: if config_class:
@ -184,7 +191,8 @@ class PluginInstance:
return False return False
await self.stop() await self.stop()
self.db_instance.primary_user = client.id self.db_instance.primary_user = client.id
self.client.references.remove(self) if self.client:
self.client.references.remove(self)
self.client = client self.client = client
self.client.references.add(self) self.client.references.add(self)
await self.start() await self.start()
@ -200,7 +208,8 @@ class PluginInstance:
return False return False
await self.stop() await self.stop()
self.db_instance.type = loader.meta.id self.db_instance.type = loader.meta.id
self.loader.references.remove(self) if self.loader:
self.loader.references.remove(self)
self.loader = loader self.loader = loader
self.loader.references.add(self) self.loader.references.add(self)
await self.start() await self.start()