mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
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:
parent
8a7cfce67b
commit
12129f0e6a
@ -158,6 +158,7 @@ class Recorder(threading.Thread):
|
|||||||
"""Start processing events to save."""
|
"""Start processing events to save."""
|
||||||
from .models import States, Events
|
from .models import States, Events
|
||||||
from homeassistant.components import persistent_notification
|
from homeassistant.components import persistent_notification
|
||||||
|
from sqlalchemy import exc
|
||||||
|
|
||||||
tries = 1
|
tries = 1
|
||||||
connected = False
|
connected = False
|
||||||
@ -273,14 +274,31 @@ class Recorder(threading.Thread):
|
|||||||
self.queue.task_done()
|
self.queue.task_done()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
with session_scope(session=self.get_session()) as session:
|
tries = 1
|
||||||
dbevent = Events.from_event(event)
|
updated = False
|
||||||
session.add(dbevent)
|
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)
|
||||||
|
|
||||||
if event.event_type == EVENT_STATE_CHANGED:
|
if event.event_type == EVENT_STATE_CHANGED:
|
||||||
dbstate = States.from_event(event)
|
dbstate = States.from_event(event)
|
||||||
dbstate.event_id = dbevent.event_id
|
dbstate.event_id = dbevent.event_id
|
||||||
session.add(dbstate)
|
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()
|
self.queue.task_done()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user