Skip to content

Commit

Permalink
database: catch various exceptions that occurred if db was temporaril…
Browse files Browse the repository at this point in the history
…y not reachable.
  • Loading branch information
aschwith committed Sep 18, 2023
1 parent c1cc244 commit e181c4a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
57 changes: 40 additions & 17 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Database(SmartPlugin):
"""

ALLOW_MULTIINSTANCE = True
PLUGIN_VERSION = '1.6.10'
PLUGIN_VERSION = '1.6.11'

# SQL queries: {item} = item table name, {log} = log table name
# time, item_id, val_str, val_num, val_bool, changed
Expand Down Expand Up @@ -786,7 +786,11 @@ def readOldestLog(self, id, cur=None):
:return: Time of oldest log record for the database ID
"""
params = {'id': id}
return self._fetchall("SELECT min(time) FROM {log} WHERE item_id = :id;", params, cur=cur)[0][0]
db_values = self._fetchall("SELECT min(time) FROM {log} WHERE item_id = :id;", params, cur=cur)
if db_values is None:
return None
else:
return db_values[0][0]


def readLatestLog(self, id, time=None, cur=None):
Expand All @@ -803,11 +807,18 @@ def readLatestLog(self, id, time=None, cur=None):
"""
if time is None:
params = {'id': id}
return self._fetchall("SELECT max(time) FROM {log} WHERE item_id = :id;", params, cur=cur)[0][0]
db_values = self._fetchall("SELECT max(time) FROM {log} WHERE item_id = :id;", params, cur=cur)
if db_values is None:
return None
else:
return db_values[0][0]
else:
params = {'id': id, 'time': time}
return self._fetchall("SELECT max(time) FROM {log} WHERE item_id = :id AND time <= :time", params, cur=cur)[0][0]

db_values = self._fetchall("SELECT max(time) FROM {log} WHERE item_id = :id AND time <= :time", params, cur=cur)
if db_values is None:
return None
else:
return db_values[0][0]

def readTotalLogCount(self, id=None, time_start=None, time_end=None, cur=None):
"""
Expand All @@ -826,7 +837,6 @@ def readTotalLogCount(self, id=None, time_start=None, time_end=None, cur=None):
return 0
return result[0][0]


def readLogCount(self, id, time_start=None, time_end=None, cur=None):
"""
Read database log count for given database ID
Expand Down Expand Up @@ -907,17 +917,30 @@ def build_orphanlist(self, log_activity=False):
self.orphanlist = []

items = [item.id() for item in self._buffer]
cur = self._db_maint.cursor()
try:
for item in self.readItems(cur=cur):
if item[COL_ITEM_NAME] not in items:
if log_activity:
self.logger.info(f"- Found data for item w/o database attribute: {item[COL_ITEM_NAME]}")
self.orphanitemlist.append(item)
self.orphanlist.append(item[COL_ITEM_NAME])
try:
cur = self._db_maint.cursor()
except Exception as e:
self.logger.error("Database build_orphan_list failed: {}".format(e))
cur.close()
self.logger.error("Database build_orphan_list failed obtaining cursor: {}".format(e))
else:

try:
return_list = self.readItems(cur=cur)
if return_list:
for item in return_list:
if item[COL_ITEM_NAME] not in items:
if log_activity:
self.logger.info(f"- Found data for item w/o database attribute: {item[COL_ITEM_NAME]}")
self.orphanitemlist.append(item)
self.orphanlist.append(item[COL_ITEM_NAME])
except Exception as e:
self.logger.error("Database build_orphan_list failed: {}".format(e))

try:
if cur:
cur.close()
except Exception as e:
self.logger.error("Database build_orphan_list failed closing cursor: {}".format(e))

self._count_orphanlogentries()
if log_activity:
self.logger.info("build_orphan_list: Finished")
Expand Down Expand Up @@ -1649,7 +1672,7 @@ def _initialize_db(self):
self.last_connect_time = time.time()
self._db.connect()
else:
self.logger.error("Database reconnect supressed: Delta time: {0}".format(time_delta_last_connect))
self.logger.info("Database reconnect supressed: Delta time: {0}".format(time_delta_last_connect))
return False

if not self._db_initialized:
Expand Down
2 changes: 1 addition & 1 deletion database/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugin:
keywords: database
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/1021844-neues-database-plugin

version: 1.6.10 # Plugin version
version: 1.6.11 # Plugin version
sh_minversion: 1.9.3.2 # minimum shNG version to use this plugin
# sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
multi_instance: True # plugin supports multi instance
Expand Down

0 comments on commit e181c4a

Please sign in to comment.