[recorder] Catch more startup errors #6179 (#6192)

* [recorder] Catch more startup errors #6179

* Rebase on new recorder
This commit is contained in:
Johann Kellerman 2017-03-03 08:44:52 +02:00 committed by Paulus Schoutsen
parent 4da2156ebf
commit fbd0bf77c7
2 changed files with 25 additions and 2 deletions

View file

@ -153,8 +153,8 @@ class Recorder(threading.Thread):
def run(self):
"""Start processing events to save."""
from sqlalchemy.exc import SQLAlchemyError
from .models import States, Events
from homeassistant.components import persistent_notification
while True:
try:
@ -163,10 +163,15 @@ class Recorder(threading.Thread):
self._setup_run()
self.hass.loop.call_soon_threadsafe(self.async_db_ready.set)
break
except SQLAlchemyError as err:
except Exception as err: # pylint: disable=broad-except
_LOGGER.error("Error during connection setup: %s (retrying "
"in %s seconds)", err, CONNECT_RETRY_WAIT)
time.sleep(CONNECT_RETRY_WAIT)
retry = locals().setdefault('retry', 10) - 1
if retry == 0:
msg = "The recorder could not start, please check the log"
persistent_notification.create(self.hass, msg, 'Recorder')
return
purge_task = object()
shutdown_task = object()

View file

@ -1,14 +1,17 @@
"""The tests for the Recorder component."""
# pylint: disable=protected-access
import unittest
from unittest.mock import patch
import pytest
from homeassistant.core import callback
from homeassistant.const import MATCH_ALL
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.const import DATA_INSTANCE
from homeassistant.components.recorder.util import session_scope
from homeassistant.components.recorder.models import States, Events
from tests.common import get_test_home_assistant, init_recorder_component
@ -163,3 +166,18 @@ def test_saving_state_include_domain_exclude_entity(hass_recorder):
assert len(states) == 1
assert hass.states.get('test.ok') == states[0]
assert hass.states.get('test.ok').state == 'state2'
def test_recorder_setup_failure():
"""Test some exceptions."""
hass = get_test_home_assistant()
with patch.object(Recorder, '_setup_connection') as setup, \
patch('homeassistant.components.recorder.time.sleep'):
setup.side_effect = ImportError("driver not found")
rec = Recorder(
hass, purge_days=0, uri='sqlite://', include={}, exclude={})
rec.start()
rec.join()
hass.stop()