Skip to content

Commit

Permalink
Merge pull request vmware-archive#170 from KohliDev/handle-exception
Browse files Browse the repository at this point in the history
Handling Exception
  • Loading branch information
KohliDev authored Nov 1, 2017
2 parents 6eaf290 + c33dfd3 commit 44d7513
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
5 changes: 4 additions & 1 deletion liota/entities/metrics/registered_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def send_data(self):
if not self.values:
# No values measured since last report_data
return True
self.ref_dcc.publish(self)
try:
self.ref_dcc.publish(self)
except Exception:
log.error("Exception while publishing message", exc_info=True)

def __str__(self, *args, **kwargs):
return str(self.ref_entity.name) + ":" + str(self._next_run_time)
Expand Down
12 changes: 5 additions & 7 deletions liota/lib/transports/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,11 @@ def publish(self, topic, message, qos, retain=False):
:param retain: Message to be retained or not
:return:
"""
# TODO: Retry logic to be designed
try:
mess_info = self._paho_client.publish(topic, message, qos, retain)
log.info("Publishing Message ID : {0} with result code : {1} ".format(mess_info.mid, mess_info.rc))
log.debug("Published Topic:{0}, Payload:{1}, QoS:{2}".format(topic, message, qos))
except Exception:
log.exception("MQTT Publish exception traceback..")
mess_info = self._paho_client.publish(topic, message, qos, retain)
if mess_info.rc == 0:
log.debug("Published Message ID:{0} with result code:{1}, Topic:{2}, Payload:{3}, QoS:{4}".format(mess_info.mid, mess_info.rc, topic, message, qos))
else:
raise Exception("MQTT Publish exception Message ID:{0} with result code:{1}, Topic:{2}, Payload:{3}, QoS:{4}".format(mess_info.mid, mess_info.rc, topic, message, qos))

def subscribe(self, topic, qos, callback):
"""
Expand Down
3 changes: 3 additions & 0 deletions packages/user_packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ Kindly include the unregister edge_system call in the clean up method required d

This is a sample user package to publish the basic edge system stats which we believe are required to
monitor the health status of the edge system from Pulse IoT Control Center.
Optional mechanism: If the device raises an intermittent exception during metric collection process it will be required to be handled in the user code
otherwise if an exception is thrown from user code the collection process will be stopped for that metric.
If the None value is returned by User Defined Method(UDM) then metric value for that particular collector instance won't be published.
15 changes: 14 additions & 1 deletion packages/user_packages/iotcc_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PackageClass(LiotaPackage):

def run(self, registry):
import copy
import time
from liota.lib.utilities.identity import Identity
from liota.dccs.iotcc import IotControlCenter
from liota.dcc_comms.mqtt_dcc_comms import MqttDccComms
Expand Down Expand Up @@ -79,7 +80,19 @@ def run(self, registry):
self.iotcc.set_system_properties(self.iotcc_edge_system, registry.get("system_properties"))
# Set the properties for edge system as key:value pair, you can also set the location
# by passing the latitude and longitude as a property in the user package
self.iotcc.set_properties(self.iotcc_edge_system,{"key1": "value1", "key2": "value2"})
# If the set_properties or register call fails due to DCC_Comms Publish exception
# the optional retry mechanism can be implemented in the following way
attempts = 0
while attempts < 3:
try:
# Register edge system (gateway)
self.iotcc.set_properties(self.iotcc_edge_system, {"key1": "value1", "key2": "value2"})
break
except Exception:
attempts += 1
# The sleep time before re-trying depends on the infrastructure requirement of broker to restart
# It can be modified or removed as per the infrastructure requirement
time.sleep(5)
registry.register("iotcc_mqtt", self.iotcc)
# Store the registered edge system object in liota package manager registry after the
# system properties are set for it
Expand Down
10 changes: 9 additions & 1 deletion packages/user_packages/iotcc_mqtt_edge_system_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ def read_cpu_utilization(sample_duration_sec=1):


def read_disk_usage_stats():
return round(disk_stat.disk_reads_writes(disk_name)[0], 2)
# If the device raises an intermittent exception during metric collection process it will be required
# to be handled in the user code otherwise if an exception is thrown from user code
# the collection process will be stopped for that metric.
# If the None value is returned by UDM then metric value for that particular collector instance won't be published.
try:
disk_stat_value = round(disk_stat.disk_reads_writes(disk_name)[0], 2)
except Exception:
return None
return disk_stat_value


def read_network_bytes_received():
Expand Down

0 comments on commit 44d7513

Please sign in to comment.