Permalink
Browse files

Exit when we're told to, and try to reconnect in the event of a failure

  • Loading branch information...
1 parent e2479d6 commit f9c022558a81f19080dfa62d76292ab9932eb111 @kylegordon committed Dec 6, 2012
Showing with 6 additions and 1 deletion.
  1. +6 −1 mqtt-republisher.py
View
@@ -50,12 +50,17 @@ def cleanup(signum, frame):
mqttc.publish("/status/" + socket.getfqdn(), "Offline")
mqttc.disconnect()
logging.info("Exiting on signal %d", signum)
+ sys.exit(signum)
def connect():
"""
Connect to the broker, define the callbacks, and subscribe
"""
- mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True)
+ result = mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True)
+ if result != 0:
+ logging.info("Connection failed with error code %s. Retrying", result)
+ time.sleep(10)
+ connect()
#define the callbacks
mqttc.on_message = on_message

0 comments on commit f9c0225

Please sign in to comment.