class DbusHook(Hook, ExitStack): def __init__(self, config, client): super().__init__() self.config = config bus = SessionBus() self.bot = RetryProxy(bus, "se.raek.PladderBot") self.connector = PladderConnector(bus, config, client) self.enter_context(dbus_loop()) def on_trigger(self, timestamp, channel, sender, text): return self.bot.RunCommand(timestamp, self.config.network, channel, sender, text, on_error=self._handle_bot_error) def _handle_bot_error(self, e): if "org.freedesktop.DBus.Error.ServiceUnknown" in str(e): return { "text": "Internal error: could not reach pladder-bot. " + "Please check the log: \"journalctl --user-unit pladder-bot.service -e\"", "command": "error", } else: logger.error(str(e)) return { "text": "Internal error: " + str(e), "command": "error", }
class DbusHook(Hook): def __init__(self, config, client): super().__init__() self.config = config bus = SessionBus() self.bot = RetryProxy(bus, "se.raek.PladderBot") self.connector = PladderConnector(bus, config, client) self.running = False self.exe = None self.loop = None self.loop_future = None def __enter__(self): assert not self.running self.exe = ThreadPoolExecutor(max_workers=1).__enter__() self.loop = GLib.MainLoop() self.loop_future = self.exe.submit(self.loop.run) self.running = True logger.info("Dbus thread started") return self def __exit__(self, exc_type, exc_value, traceback): assert self.running # Signal loop to stop self.loop.quit() self.loop = None # Wait for loop task to finish self.loop_future.result() self.loop_future = None # Wait for executor to shut down self.exe.__exit__(None, None, None) self.exe = None # Everything is torn down self.running = False logger.info("Dbus thread stopped") return None def on_trigger(self, timestamp, channel, sender, text): return self.bot.RunCommand(timestamp, self.config.network, channel, sender, text, on_error=self._handle_bot_error) def _handle_bot_error(self, e): if "org.freedesktop.DBus.Error.ServiceUnknown" in str(e): return { "text": "Internal error: could not reach pladder-bot. " + "Please check the log: \"journalctl --user-unit pladder-bot.service -e\"", "command": "error", } else: logger.error(str(e)) return { "text": "Internal error: " + str(e), "command": "error", }