Skip to content

Commit

Permalink
Attempt to disconnect MQTT cleanly if error occurs during upload
Browse files Browse the repository at this point in the history
Have monitored different errors from MQTT during upload after connection.  In the event of an OSError or IndexError try to disconnect MQTT to see if it prevents hangs.
  • Loading branch information
dave-ct committed Dec 11, 2022
1 parent c902806 commit d3f63d3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions enviro/destinations/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def upload_reading(reading):
mqtt_client.disconnect()
return UPLOAD_SUCCESS

# Try disconneting to see if it prevents hangs on this typew of errors recevied so far
except (OSError, IndexError) as exc:
try:
buf = io.StringIO()
sys.print_exception(exc, buf)
logging.debug(f" - an exception occurred when uploading.", buf.getvalue())
mqtt_client.disconnect()
except Exception as exc:
import sys, io
buf = io.StringIO()
sys.print_exception(exc, buf)
logging.debug(f" - an exception occurred when disconnecting mqtt client.", buf.getvalue())

except Exception as exc:
import sys, io
buf = io.StringIO()
Expand Down

0 comments on commit d3f63d3

Please sign in to comment.