Mercurial > code > home > repos > mqtt_metrics
diff mqtt_metrics.py @ 3:41e36f98f3b8
die on mqtt connection errors
author | drewp@bigasterisk.com |
---|---|
date | Fri, 09 Aug 2024 18:36:39 -0700 |
parents | 579df3a4e62d |
children | cd1b8d7bda78 |
line wrap: on
line diff
--- a/mqtt_metrics.py Fri Aug 09 17:37:00 2024 -0700 +++ b/mqtt_metrics.py Fri Aug 09 18:36:39 2024 -0700 @@ -1,6 +1,7 @@ import asyncio import json import logging +import os import re import time from typing import cast @@ -56,12 +57,19 @@ async def mqttTask(): - client = aiomqtt.Client('mqtt2.bigasterisk.com', identifier="mqtt-exporter") - async with client: - await client.subscribe('#') - async for mqttMessage in client.messages: - onMqttMessage(mqttMessage) + try: + client = aiomqtt.Client('mqtt2.bigasterisk.com', identifier="mqtt-exporter") + async with client: + await client.subscribe('#') + async for mqttMessage in client.messages: + try: + onMqttMessage(mqttMessage) + except Exception as e: + log.warning("mqtt message (topic %r) failed: %r", mqttMessage.topic.value, e) + except Exception: + log.error("mqtt task failed", exc_info=True) + os.abort() def onMqttMessage(mqttMessage): message = simplifyMqttMessage(mqttMessage)