Commit db5196d2cd6e

Vincent Demeester <vincent@sbr.pm>
2026-01-15 17:00:39
fix(xmpp-research-bot): add automatic reconnection on connection loss
The bot would lose connection without attempting to reconnect, requiring manual systemd service restart. Added slixmpp auto-reconnect support and event handlers for connection monitoring to improve reliability. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 16c46fa
Changed files (1)
modules
xmpp-research-bot
modules/xmpp-research-bot/bot.py
@@ -88,6 +88,9 @@ class ResearchBot(slixmpp.ClientXMPP):
         # Register event handlers
         self.add_event_handler("session_start", self.on_session_start)
         self.add_event_handler("message", self.on_message)
+        self.add_event_handler("disconnected", self.on_disconnected)
+        self.add_event_handler("connection_failed", self.on_connection_failed)
+        self.add_event_handler("session_end", self.on_session_end)
 
         log.info(f"Bot initialized: {jid}")
         log.info(f"Owner: {owner_jid}")
@@ -116,6 +119,18 @@ class ResearchBot(slixmpp.ClientXMPP):
         await self.get_roster()
         log.info("Session started, presence sent")
 
+    async def on_session_end(self, event):
+        """Called when XMPP session ends"""
+        log.warning("Session ended")
+
+    async def on_disconnected(self, event):
+        """Called when disconnected from XMPP server"""
+        log.warning(f"Disconnected from XMPP server: {event}")
+
+    async def on_connection_failed(self, event):
+        """Called when connection to XMPP server fails"""
+        log.error(f"Connection failed: {event}")
+
     async def on_message(self, msg):
         """Handle incoming messages"""
         # Only respond to chat messages from owner
@@ -425,12 +440,14 @@ async def main():
     bot = ResearchBot(jid, password, owner_jid, project_id, region, inbox_path, commands_path, gemini_api_key)
 
     log.info("Connecting to XMPP server...")
-    bot.connect()
+    # Enable auto-reconnect with reattempt=True
+    # This will automatically reconnect if connection drops
+    bot.connect(reattempt=True, use_tls=True)
 
-    # Process events
+    # Process events using slixmpp's built-in event loop
+    # This is more robust than manual sleep loop
     try:
-        while True:
-            await asyncio.sleep(1)
+        await bot.process(forever=True)
     except KeyboardInterrupt:
         log.info("Shutting down...")
         bot.disconnect()