Skip to content

Commit 8ea02ef

Browse files
committed
do not reconnect on disconnect
1 parent 2ef6b67 commit 8ea02ef

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

klimalogger/store/queue.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import logging
33
import secrets
4-
import time
54
from typing import List
65

76
from injector import inject
@@ -31,24 +30,9 @@ def on_connect(client, userdata, flags, reason_code, properties):
3130
def on_disconnect(client, userdata, flags, reason_code, properties):
3231
log.warning(f"Disconnected from MQTT Broker: {reason_code}")
3332

34-
delay = 5
35-
max_delay = 60
36-
37-
while True:
38-
try:
39-
if not client.reconnect():
40-
log.info("Successful reconnect to MQTT Broker")
41-
break
42-
except ConnectionRefusedError:
43-
log.info("Reconnect to MQTT Broker failed, retry in %d seconds", delay)
44-
45-
if delay < max_delay:
46-
delay *= 2 + secrets.randbelow(5)
47-
48-
time.sleep(delay)
49-
5033
try:
51-
self.client = mqtt_client.Client(client_id=client_id, clean_session=False, callback_api_version = CallbackAPIVersion.VERSION2)
34+
self.client = mqtt_client.Client(client_id=client_id, clean_session=False,
35+
callback_api_version=CallbackAPIVersion.VERSION2)
5236
# client.username_pw_set(username, password)
5337
self.client.on_connect = on_connect
5438
self.client.on_disconnect = on_disconnect
@@ -68,7 +52,10 @@ def store(self, data: List[dict]):
6852

6953
if not self.client.is_connected():
7054
log.warning("client not connected, try to reconnect")
71-
self.client.reconnect()
55+
try:
56+
self.client.reconnect()
57+
except Exception:
58+
log.warning("reconnect failed")
7259

7360
for entry in data:
7461
topic, json_message = self.map_entry(entry)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "klimalogger"
3-
version = "0.7.27"
3+
version = "0.7.28"
44
description = "Simple data logging client for InfluxDB/MQTT target"
55
authors = ["Andreas Würl <andi@tryb.de>"]
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)