Add graceful exits
This sets up a set of ropes and pulleys that signal the `Bot.keepalive` function to clean things up after a quit signal has been sent. This allows plugins to define an `on_unload` function to save any important datas on intentional exit. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -22,6 +22,7 @@ class Bot:
|
||||
]
|
||||
# TODO - this may not be needed
|
||||
self.__channels: Set[str] = set()
|
||||
self.__quitting = asyncio.Event()
|
||||
|
||||
@property
|
||||
def server_config(self) -> ServerConfig:
|
||||
@@ -31,6 +32,9 @@ class Bot:
|
||||
def plugins(self) -> Sequence[plugin.Plugin]:
|
||||
return self.__plugins
|
||||
|
||||
def quit(self):
|
||||
self.__quitting.set()
|
||||
|
||||
def channel_plugins(self, channel: str) -> Sequence[plugin.Plugin]:
|
||||
return [plugin for plugin in self.plugins if channel in plugin.channels]
|
||||
|
||||
@@ -127,6 +131,9 @@ class Bot:
|
||||
|
||||
async def keepalive(self):
|
||||
# loop while we're connected, check every second
|
||||
log.info("Starting keepalive loop")
|
||||
while self.connection.connected:
|
||||
await asyncio.sleep(1.0)
|
||||
await self.__quitting.wait()
|
||||
log.info("Shutting down gracefully")
|
||||
# TODO: unload modules
|
||||
await asyncio.gather(
|
||||
*[plugin.on_unload(self.connection) for plugin in self.plugins]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user