Skip to content

Commit

Permalink
Working on resilience...
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianTremblay committed Sep 5, 2021
1 parent 37f24e6 commit 2223094
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
10 changes: 10 additions & 0 deletions BAC0/core/devices/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@ def update_description(self, value):
)
self.properties.description = self.read_property("description")

def ping(self):
try:
if self.read_property("objectName"):
return True
else:
return False
except Exception as e:
self._log_error("Error in ping")
return False

def __repr__(self):
return "{} / Connected".format(self.properties.name)

Expand Down
4 changes: 1 addition & 3 deletions BAC0/db/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, params):
setattr(self, k, v)
if self.bucket is None:
raise ValueError("Missing bucket name, please provide one in db_params")
self.connect_to_db()
self.connect_to_db()

self.write_api = self.client.write_api(
write_options=WriteOptions(
Expand Down Expand Up @@ -68,12 +68,10 @@ def connect_to_db(self):
except:
raise ConnectionError("Error connecting to InfluxDB")


@property
def health(self):
return self.client.health()


def clean_value(self, object_type, val, units_state):
if "analog" in object_type:
_string_value = "{:.3f} {}".format(val, units_state)
Expand Down
10 changes: 8 additions & 2 deletions BAC0/scripts/Lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ def __init__(
if db_params["name"].lower() == "influxdb"
else None
)
self._log.info("Connection made to InfluxDB bucket : {}".format(self.database.bucket))
self._log.info(
"Connection made to InfluxDB bucket : {}".format(
self.database.bucket
)
)
except ConnectionError:
self._log.error("Unable to connect to InfluxDB. Please validate parameters")
self._log.error(
"Unable to connect to InfluxDB. Please validate parameters"
)

@property
def known_network_numbers(self):
Expand Down
33 changes: 28 additions & 5 deletions BAC0/tasks/Poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from ..core.utils.notes import note_and_log

# ------------------------------------------------------------------------------
class MultiplePollingFailures(Exception):
pass


@note_and_log
Expand Down Expand Up @@ -70,6 +72,8 @@ def __init__(self, device, delay=10, name="", prefix="basic_poll"):
:returns: Nothing
"""
self.failures = 0
self.MAX_FAILURES = 3
self._device = weakref.ref(device)
Task.__init__(self, name="{}_{}".format(prefix, name), delay=delay)
self._counter = 0
Expand All @@ -80,6 +84,10 @@ def device(self):

def task(self):
try:
if self.failures >= self.MAX_FAILURES:
raise MultiplePollingFailures(
"Polling failed numerous times in a row... let see what we can do"
)
self.device.read_multiple(
list(self.device.pollable_points_name), points_per_request=25
)
Expand All @@ -89,17 +97,32 @@ def task(self):
if self.device.properties.clear_history_on_save:
self.device.clear_histories()
self._counter = 0
except AttributeError:
self.failures = 0
except AttributeError as e:
# This error can be seen when defining a controller on a busy network...
# When creation fail, polling is created and fail the first time...
# So kill the task
self.stop()
self.device._log.error(
"Something is wrong while creating the polling task."
"Error: {}".format(e)
)
# self.stop()
self.failures += 1
except ValueError as e:
self.failures += 1
self.device._log.error(
"Something is wrong with polling...stopping. Try setting off "
"segmentation. Error: {}".format(e)
"Something is wrong with polling...stopping. Will pass this time"
"Error: {}".format(e)
)
pass

except MultiplePollingFailures as e:
self.device._log.warning(
"Trying to ping device then we'll reset the number of failures and get back with polling"
"Error: {}".format(e)
)
self.stop()
if self.device.ping():
self.failures = 0


@note_and_log
Expand Down

0 comments on commit 2223094

Please sign in to comment.