Retry mqtt connection on unreachable network
This commit is contained in:
parent
1e41bdd2dc
commit
f030b460c8
1 changed files with 19 additions and 5 deletions
|
@ -130,11 +130,25 @@ class MqttConsumer(BaseConsumer):
|
|||
self.client.will_set(
|
||||
f"{self.topic_prefix}/available", payload="offline", retain=True
|
||||
)
|
||||
self.client.connect(
|
||||
settings["client"]["host"],
|
||||
settings["client"]["port"],
|
||||
settings["client"]["keepalive"],
|
||||
)
|
||||
while True:
|
||||
try:
|
||||
self.client.connect(
|
||||
settings["client"]["host"],
|
||||
settings["client"]["port"],
|
||||
settings["client"]["keepalive"],
|
||||
)
|
||||
break
|
||||
except OSError as err:
|
||||
# Network is unreachable
|
||||
if err.errno == 101:
|
||||
pass
|
||||
# Temporary failure in name resolution
|
||||
elif err.errno == -3:
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
print(err)
|
||||
sleep(0.1)
|
||||
|
||||
def config(self, settings: Dict[str, Any]):
|
||||
super().config(settings)
|
||||
|
|
Loading…
Reference in a new issue