Try catch around database updates in recorder. Resolves 6919 (#8349)

* Try catch around database updates in recorder. Resolves 6919

* Fixing failed test for line length

* Catch only OperationalError and retry connections before giving up

* Including alchemy exceptions in single function
This commit is contained in:
Sean 2017-07-07 06:46:50 +02:00 committed by Paulus Schoutsen
parent 8a7cfce67b
commit 12129f0e6a

View File

@ -158,6 +158,7 @@ class Recorder(threading.Thread):
"""Start processing events to save."""
from .models import States, Events
from homeassistant.components import persistent_notification
from sqlalchemy import exc
tries = 1
connected = False
@ -273,6 +274,12 @@ class Recorder(threading.Thread):
self.queue.task_done()
continue
tries = 1
updated = False
while not updated and tries <= 10:
if tries != 1:
time.sleep(CONNECT_RETRY_WAIT)
try:
with session_scope(session=self.get_session()) as session:
dbevent = Events.from_event(event)
session.add(dbevent)
@ -281,6 +288,17 @@ class Recorder(threading.Thread):
dbstate = States.from_event(event)
dbstate.event_id = dbevent.event_id
session.add(dbstate)
updated = True
except exc.OperationalError as err:
_LOGGER.error("Error in database connectivity: %s. "
"(retrying in %s seconds)", err,
CONNECT_RETRY_WAIT)
tries += 1
if not updated:
_LOGGER.error("Error in database update. Could not save "
"after %d tries. Giving up", tries)
self.queue.task_done()