add on_pause() and on_resume() for module - use in rest_api.py

This commit is contained in:
jesopo 2019-11-04 10:52:41 +00:00
parent 754269576a
commit bcdffacab5
2 changed files with 26 additions and 6 deletions

View file

@ -175,17 +175,27 @@ class Module(ModuleManager.BaseModule):
self.httpd = None self.httpd = None
if self.bot.get_setting("rest-api", False): if self.bot.get_setting("rest-api", False):
self._start_httpd()
def _start_httpd(self):
port = int(self.bot.config.get("api-port", str(DEFAULT_PORT))) port = int(self.bot.config.get("api-port", str(DEFAULT_PORT)))
self.httpd = BitBotIPv6HTTPd(("::1", port), Handler) self.httpd = BitBotIPv6HTTPd(("::1", port), Handler)
self.thread = threading.Thread(target=self.httpd.serve_forever) self.thread = threading.Thread(target=self.httpd.serve_forever)
self.thread.daemon = True self.thread.daemon = True
self.thread.start() self.thread.start()
def _stop_httpd(self):
def unload(self):
if self.httpd: if self.httpd:
self.httpd.shutdown() 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.hook("received.command.apikey")
@utils.kwarg("private_only", True) @utils.kwarg("private_only", True)
@utils.kwarg("min_args", 1) @utils.kwarg("min_args", 1)

View file

@ -60,6 +60,11 @@ class BaseModule(object):
def unload(self): def unload(self):
pass pass
def on_pause(self):
pass
def on_resume(self):
pass
def command_line(self, args: str): def command_line(self, args: str):
pass pass
@ -381,6 +386,9 @@ class ModuleManager(object):
old_modules = self.modules old_modules = self.modules
self.modules = {} self.modules = {}
for module in old_modules.values():
module.module.on_pause()
failed = None failed = None
for definition in loadable: for definition in loadable:
try: try:
@ -393,6 +401,8 @@ class ModuleManager(object):
for module in self.modules.values(): for module in self.modules.values():
self._unload_module(module) self._unload_module(module)
self.modules = old_modules self.modules = old_modules
for module in old_modules.values():
module.module.on_resume()
definition, exception = failed definition, exception = failed
return TryReloadResult(False, return TryReloadResult(False,