Add IRCBot.panic() - a nicer interface for killing the whole application

This commit is contained in:
jesopo 2019-06-17 11:06:03 +01:00
parent ced59a0f74
commit f8fa529ca5

View file

@ -1,4 +1,4 @@
import enum, queue, os, queue, select, socket, threading, time, traceback import enum, queue, os, queue, select, socket, sys, threading, time, traceback
import typing, uuid import typing, uuid
from src import EventManager, Exports, IRCServer, Logging, ModuleManager from src import EventManager, Exports, IRCServer, Logging, ModuleManager
from src import Socket, utils from src import Socket, utils
@ -100,6 +100,20 @@ class Bot(object):
elif type == TriggerResult.Return: elif type == TriggerResult.Return:
return returned return returned
def panic(self, reason=None):
callback = None
if not reason == None:
self.log.error("panic() called: %s", [reason])
exception = sys.exc_info()[1]
if exception:
def _raise():
raise exception
callback = _raise
self._event_queue.put(TriggerEvent(TriggerEventType.Kill, callback))
def load_modules(self, safe: bool=False def load_modules(self, safe: bool=False
) -> typing.Tuple[typing.List[str], typing.List[str]]: ) -> typing.Tuple[typing.List[str], typing.List[str]]:
db_blacklist = set(self.get_setting("module-blacklist", [])) db_blacklist = set(self.get_setting("module-blacklist", []))
@ -263,6 +277,8 @@ class Bot(object):
raise raise
elif item.type == TriggerEventType.Kill: elif item.type == TriggerEventType.Kill:
self._kill() self._kill()
if not item.callback == None:
item.callback()
def _post_send_factory(self, server, lines): def _post_send_factory(self, server, lines):
return lambda: server._post_send(lines) return lambda: server._post_send(lines)
@ -272,9 +288,8 @@ class Bot(object):
def _loop_catch(self, name: str, loop: typing.Callable[[], None]): def _loop_catch(self, name: str, loop: typing.Callable[[], None]):
try: try:
loop() loop()
except: except Exception as e:
self.log.critical("Exception on '%s' thread", exc_info=True) self.panic("Exception on '%s' thread" % name)
self._event_queue.put(TriggerEvent(TriggerEventType.Kill))
def _write_loop(self): def _write_loop(self):
while self.running: while self.running: