Skip to content

Commit

Permalink
Restructure calls to retry from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Apr 16, 2024
1 parent adf0259 commit 981227e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 49 deletions.
8 changes: 2 additions & 6 deletions custom_components/dabpumps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def async_logout(self):
self._client = None


async def async_fetch_install_list(self, callback_diag_save=None):
async def async_fetch_install_list(self):
"""Get installation list"""

timestamp = datetime.now()
Expand Down Expand Up @@ -316,11 +316,7 @@ async def async_fetch_install_details(self, install_id):
url = DABPUMPS_API_URL + f"/api/v1/installation/{install_id_org}"

_LOGGER.debug(f"DAB Pumps retrieve installation details via {verb} {url}")
try:
result = await self._async_send_request(context, verb, url)
except:
_LOGGER.warn(f"Could not retrieve data for installation {install_id}. Trying fallback data.")
result = await self.async_fallback_install_details(install_id)
result = await self._async_send_request(context, verb, url)

return result

Expand Down
139 changes: 96 additions & 43 deletions custom_components/dabpumps/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,39 @@ async def _async_detect_install_details(self):
data = await self._api.async_fetch_install_details(self._install_id)
await self._async_process_install_data(data)
await self._async_update_cache(context, data)

ex = None
except Exception as e:
if len(self._device_map) > 0:
# Ignore problems if this is just a periodic refresh
pass
ex = None
else:
# Retry from persisted cache if this is the initial retrieve
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_install_data(data)
# Try next alternative while remembering original exception
ex = e

except Exception:
# Force retry in calling function by raising original exception
raise e
if ex:
# Next, try from persisted cache
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_install_data(data)
ex = None
except Exception:
# Try next alternative while remembering original exception
pass

if ex:
# Finally, try from API history store 'details->installation list'
try:
data = await self._api.async_fallback_install_details(self._install_id)
await self._async_process_install_data(data)
await self._async_update_cache(context, data)
ex = None
except Exception as e:
# Try next alternative while remembering original exception
pass

if ex:
# Force retry in calling function by raising original exception
raise ex

# If we reach this point, then all devices have been fetched/refreshed
self._device_map_ts = datetime.now()
Expand All @@ -428,19 +447,28 @@ async def _async_detect_device_configs(self):
data = await self._api.async_fetch_device_config(device)
await self._async_process_device_config_data(device, data)
await self._async_update_cache(context, data)

ex = None
except Exception as e:
if device.config_id in self._config_map:
# Ignore problems if this is just a refresh
pass
ex = None
else:
# Retry from persisted cache if this is the initial retrieve
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_device_config_data(device, data)
except Exception:
# Force retry in calling function by raising original exception
raise e
# Try next alternative while remembering original exception
ex = e

if ex:
# Next try from persisted cache if this is the initial retrieve
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_device_config_data(device, data)
ex = None
except Exception:
# Try next alternative while remembering original exception
pass

if ex:
# Force retry in calling function by raising original exception
raise ex

# If we reach this point, then all device configs have been fetched/refreshed
self._config_map_ts = datetime.now()
Expand All @@ -462,20 +490,29 @@ async def _async_detect_device_statusses(self):
data = await self._api.async_fetch_device_statusses(device)
await self._async_process_device_status_data(device, data)
await self._async_update_cache(context, data)

ex = None
except Exception as e:
if any(status.serial==device.serial for status in self._status_map.values()):
# Ignore problems if this is just a refresh
pass
ex = None
else:
# Retry from (outdated) persisted cache if this is the initial retrieve.
# However, we will then set all values to unknown.
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_device_status_data(device, data, expired_values=True)
except Exception:
# Force retry in calling function by raising original exception
raise e
# Try next alternative while remembering original exception
ex = e

if ex:
# Next try from (outdated) persisted cache if this is the initial retrieve.
# However, we will then set all values to unknown.
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_device_status_data(device, data, expired_values=True)
ex = None
except Exception:
# Try next alternative while remembering original exception
pass

if ex:
# Force retry in calling function by raising original exception
raise ex

# If we reach this point, then all device statusses have been fetched/refreshed
self._status_map_ts = datetime.now()
Expand All @@ -494,19 +531,28 @@ async def _async_detect_strings(self):
data = await self._api.async_fetch_strings(self.language)
await self._async_process_strings_data(data)
await self._async_update_cache(context, data)

ex = None
except Exception as e:
# Ignore problems if this is just a refresh
if len(self._string_map) > 0:
pass
# Ignore problems if this is just a refresh
ex = None
else:
# Retry from persisted cache if this is the initial retrieve
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_strings_data(data)
except Exception:
# Force retry in calling function by raising original exception
raise e
# Try next alternative while remembering original exception
ex = e

if ex:
# Next, try from persisted cache if this is the initial retrieve
try:
data = await self._async_fetch_from_cache(context)
await self._async_process_strings_data(data)
ex = None
except Exception:
# Try next alternative while remembering original exception
pass

if ex:
# Force retry in calling function by raising original exception
raise ex

# If we reach this point, then all strings have been fetched/refreshed
self._string_map_ts = datetime.now()
Expand All @@ -521,16 +567,23 @@ async def _async_detect_installations(self, ignore_exception=False):
return

# First try to retrieve from API.
# Make sure not to overwrite data in dabpumps.api_history file when an empty list is returned.
context = f"installation list"
try:
data = await self._api.async_fetch_install_list()
await self._async_process_install_list(data, ignore_empty=True)
await self._async_process_install_list(data)
await self._async_update_cache(context, data)

ex = None
except Exception as e:
if not ignore_exception:
raise
if ignore_exception:
# Ignore problems
ex = None
else:
# Try next alternative while remembering original exception
ex = e

if ex:
# Force retry in calling function by raising original exception
raise ex

# If we reach this point, then installation list been fetched/refreshed/ignored
self._install_map_ts = datetime.now()
Expand Down

0 comments on commit 981227e

Please sign in to comment.