Skip to content

Commit

Permalink
Trying to give a little resilience to BAC0 when devices get disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianTremblay committed Aug 25, 2021
1 parent dc646e4 commit 37f24e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
23 changes: 17 additions & 6 deletions BAC0/db/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
try:
import pandas as pd
from pandas.io import sql
from pandas.core.base import DataError

try:
from pandas import Timestamp
Expand All @@ -28,7 +29,7 @@
except ImportError:
_PANDAS = False

from ..core.io.IOExceptions import RemovedPointException
from ..core.io.IOExceptions import RemovedPointException, NoResponseFromController

# --- this application's modules ---

Expand Down Expand Up @@ -141,7 +142,7 @@ def extract_value_and_string(val):
except Exception as error:
self._log.error(
"Error in resampling {} | {} (probably not enough points)".format(
point, error
point.properties.name, error
)
)
if (
Expand Down Expand Up @@ -199,21 +200,31 @@ def save(self, filename=None, resampling=None):
resampling = self.properties.save_resampling

# Does file exist? If so, append data

def _df_to_backup():
try:
return self.backup_histories_df(resampling=resampling)
except (DataError, NoResponseFromController):
self._log.error("Impossible to save right now, error in data")
return None

if os.path.isfile("{}.db".format(self.properties.db_name)):
his = self._read_from_sql(
'select * from "{}"'.format("history"), self.properties.db_name
)
his.index = his["index"].apply(Timestamp)
try:
last = his.index[-1]
df_to_backup = self.backup_histories_df(resampling=resampling)[last:]
except IndexError:
df_to_backup = self.backup_histories_df(resampling=resampling)
df_to_backup = _df_to_backup()[last:]
except (IndexError, TypeError):
df_to_backup = _df_to_backup()

else:
self._log.debug("Creating a new backup database")
df_to_backup = self.backup_histories_df(resampling=resampling)
df_to_backup = _df_to_backup()

if df_to_backup is None:
return
# DataFrames that will be saved to SQL
with contextlib.closing(
sqlite3.connect("{}.db".format(self.properties.db_name))
Expand Down
13 changes: 9 additions & 4 deletions BAC0/tasks/TaskManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ def process(cls):
)
cls.tasks.remove(task.id)
except Exception as error:
cls._log.error(
"Super Mega Giga big error {}. Removing task.".format(error)
)
cls.tasks.remove(task.id)
if task.name == "Ping Task":
cls._log.warning(
"Ping failed with error {} ({}).".format(error, task)
)
else:
cls._log.error(
"Super Mega Giga big error {}. Removing task.".format(error)
)
cls.tasks.remove(task.id)
else:
if not cls.manager.is_alive() and cls.enable:
cls._log.error(
Expand Down

0 comments on commit 37f24e6

Please sign in to comment.