diff --git a/modules/rest_api.py b/modules/rest_api.py index 7542a607..0f3a1d2e 100644 --- a/modules/rest_api.py +++ b/modules/rest_api.py @@ -175,17 +175,27 @@ class Module(ModuleManager.BaseModule): self.httpd = None if self.bot.get_setting("rest-api", False): - port = int(self.bot.config.get("api-port", str(DEFAULT_PORT))) - self.httpd = BitBotIPv6HTTPd(("::1", port), Handler) + self._start_httpd() - self.thread = threading.Thread(target=self.httpd.serve_forever) - self.thread.daemon = True - self.thread.start() + def _start_httpd(self): + port = int(self.bot.config.get("api-port", str(DEFAULT_PORT))) + self.httpd = BitBotIPv6HTTPd(("::1", port), Handler) - def unload(self): + self.thread = threading.Thread(target=self.httpd.serve_forever) + self.thread.daemon = True + self.thread.start() + def _stop_httpd(self): if self.httpd: self.httpd.shutdown() + def on_resume(self): + self._start_httpd() + + def unload(self): + self._stop_httpd() + def on_pause(self): + self._stop_httpd() + @utils.hook("received.command.apikey") @utils.kwarg("private_only", True) @utils.kwarg("min_args", 1) diff --git a/src/ModuleManager.py b/src/ModuleManager.py index 6497129b..65f7c11e 100644 --- a/src/ModuleManager.py +++ b/src/ModuleManager.py @@ -60,6 +60,11 @@ class BaseModule(object): def unload(self): pass + def on_pause(self): + pass + def on_resume(self): + pass + def command_line(self, args: str): pass @@ -381,6 +386,9 @@ class ModuleManager(object): old_modules = self.modules self.modules = {} + for module in old_modules.values(): + module.module.on_pause() + failed = None for definition in loadable: try: @@ -393,6 +401,8 @@ class ModuleManager(object): for module in self.modules.values(): self._unload_module(module) self.modules = old_modules + for module in old_modules.values(): + module.module.on_resume() definition, exception = failed return TryReloadResult(False,