handle MCRconException

This commit is contained in:
Arkrissym 2022-01-23 20:01:04 +01:00
parent aac5f59e9f
commit 1a3599363d
2 changed files with 22 additions and 7 deletions

View file

@ -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"]

View file

@ -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()
self.rcon_connected = False
self.rcon_disconnect()
else:
print("RCON command failed")
except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError):
print("Lost RCON Connection")
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:
try:
time.sleep(1)
schedule.run_pending()
except MCRconException:
# RCON timeout
collector.rcon_disconnect()