8000 [recorder] Catch more startup errors #6179 by kellerza · Pull Request #6192 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[recorder] Catch more startup errors #6179 #6192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions homeassistant/components/recorder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def async_initialize(self):

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:
Expand All @@ -163,10 +163,15 @@ def run(self):
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()
Expand Down
18 changes: 18 additions & 0 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
0