bitbot-3.11-fork/modules/signals.py

72 lines
2.1 KiB
Python
Raw Normal View History

import signal, sys
from src import Config, ModuleManager, utils
2016-03-29 11:56:58 +00:00
class Module(ModuleManager.BaseModule):
def on_load(self):
2018-12-03 19:46:15 +00:00
self._exited = False
2016-03-29 11:56:58 +00:00
signal.signal(signal.SIGINT, self.SIGINT)
signal.signal(signal.SIGUSR1, self.SIGUSR1)
2019-03-13 18:27:34 +00:00
signal.signal(signal.SIGHUP, self.SIGHUP)
2016-03-29 11:56:58 +00:00
def SIGINT(self, signum, frame):
print()
self.bot.trigger(lambda: self._kill(signum))
def _kill(self, signum):
2018-12-03 19:46:15 +00:00
if self._exited:
return
self._exited = True
self.events.on("signal.interrupt").call(signum=signum)
written = False
2016-03-29 11:56:58 +00:00
for server in self.bot.servers.values():
if server.connected:
server.socket.clear_send_buffer()
line = utils.irc.protocol.quit("Shutting down")
sent_line = server.send(line, immediate=True)
sent_line.events.on("send").hook(self._make_hook(server))
server.send_enabled = False
written = True
if not written:
sys.exit()
def _make_hook(self, server):
return lambda e: self.bot.disconnect(server)
2016-03-29 11:56:58 +00:00
def SIGUSR1(self, signum, frame):
self.bot.trigger(self._reload_config)
2019-03-13 18:27:34 +00:00
def SIGHUP(self, signum, frame):
self.bot.trigger(self._SIGHUP)
def _SIGHUP(self):
self._reload_config()
self._reload_modules()
def _reload_config(self):
2018-09-02 10:43:52 +00:00
self.bot.log.info("Reloading config file", [])
self.bot.config.load()
2018-09-02 10:43:52 +00:00
self.bot.log.info("Reloaded config file", [])
2019-03-13 18:27:34 +00:00
def _reload_modules(self):
self.bot.log.info("Reloading modules", [])
success = []
fail = []
2019-03-13 18:27:34 +00:00
for name in list(self.bot.modules.modules.keys()):
try:
self.bot.modules.unload_module(name)
2019-03-13 18:27:34 +00:00
except ModuleManager.ModuleWarning:
continue
except Exception as e:
failed.append(name)
continue
load_success, load_fail = self.bot.load_modules(safe=True)
fail.extend(load_fail)
2019-03-13 18:27:34 +00:00
self.bot.log.info("Reloaded %d modules (%d failed)",
[len(load_success), len(fail)])