diff --git a/Dockerfile b/Dockerfile index 1d80e9c..8f7738f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,4 @@ RUN pip install --no-cache-dir -r requirements.txt EXPOSE 8000 -ENTRYPOINT ["python","minecraft_exporter.py"] +ENTRYPOINT ["python","-u","minecraft_exporter.py"] diff --git a/minecraft_exporter.py b/minecraft_exporter.py index 99f30f2..2c992b7 100644 --- a/minecraft_exporter.py +++ b/minecraft_exporter.py @@ -62,13 +62,23 @@ class MinecraftCollector(object): print(e) return False + def rcon_disconnect(self): + self.rcon.disconnect() + self.rcon_connected = False + def rcon_command(self, command): try: response = self.rcon.command(command) - except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError, MCRconException): + except MCRconException as e: + response = None + if e == "Connection timeout error": + print("Lost RCON Connection") + self.rcon_disconnect() + else: + print("RCON command failed") + except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError): print("Lost RCON Connection") - self.rcon.disconnect() - self.rcon_connected = False + self.rcon_disconnect() response = None return response @@ -359,9 +369,14 @@ class MinecraftCollector(object): if __name__ == '__main__': + collector = MinecraftCollector() start_http_server(8000) - REGISTRY.register(MinecraftCollector()) + REGISTRY.register(collector) print("Exporter started on Port 8000") while True: - time.sleep(1) - schedule.run_pending() + try: + time.sleep(1) + schedule.run_pending() + except MCRconException: + # RCON timeout + collector.rcon_disconnect()