From 86b91acf9938286775514d99aafd6e4e6a4172a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Thu, 6 Feb 2020 22:39:51 +0100 Subject: [PATCH 1/9] added recorder vars db_max_retries and db_retry_wait --- homeassistant/components/recorder/__init__.py | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index ab56a5fc33b6c7..739c9ef178bb95 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -59,14 +59,16 @@ DEFAULT_URL = "sqlite:///{hass_config_path}" DEFAULT_DB_FILE = "home-assistant_v2.db" +DEFAULT_DB_MAX_RETRIES = 10 +DEFAULT_DB_RETRY_WAIT = 3 CONF_DB_URL = "db_url" +CONF_DB_MAX_RETRIES = "db_max_retries" +CONF_DB_RETRY_WAIT = "db_retry_wait" CONF_PURGE_KEEP_DAYS = "purge_keep_days" CONF_PURGE_INTERVAL = "purge_interval" CONF_EVENT_TYPES = "event_types" -CONNECT_RETRY_WAIT = 3 - FILTER_SCHEMA = vol.Schema( { vol.Optional(CONF_EXCLUDE, default={}): vol.Schema( @@ -96,6 +98,8 @@ vol.Coerce(int), vol.Range(min=0) ), vol.Optional(CONF_DB_URL): cv.string, + vol.Optional(CONF_DB_MAX_RETRIES): cv.positive_int, + vol.Optional(CONF_DB_RETRY_WAIT): cv.positive_int, } ) }, @@ -138,6 +142,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if not db_url: db_url = DEFAULT_URL.format(hass_config_path=hass.config.path(DEFAULT_DB_FILE)) + db_max_retries = conf.get(CONF_DB_MAX_RETRIES, None) + if not db_max_retries: + db_max_retries = DEFAULT_DB_MAX_RETRIES + + db_retry_wait = conf.get(CONF_DB_RETRY_WAIT, None) + if not db_retry_wait: + db_retry_wait = DEFAULT_DB_RETRY_WAIT + include = conf.get(CONF_INCLUDE, {}) exclude = conf.get(CONF_EXCLUDE, {}) instance = hass.data[DATA_INSTANCE] = Recorder( @@ -145,6 +157,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: keep_days=keep_days, purge_interval=purge_interval, uri=db_url, + db_max_retries=db_max_retries, + db_retry_wait=db_retry_wait, include=include, exclude=exclude, ) @@ -174,6 +188,8 @@ def __init__( keep_days: int, purge_interval: int, uri: str, + db_max_retries: int, + db_retry_wait: int, include: Dict, exclude: Dict, ) -> None: @@ -186,6 +202,8 @@ def __init__( self.queue: Any = queue.Queue() self.recording_start = dt_util.utcnow() self.db_url = uri + self.db_max_retries = db_max_retries + self.db_retry_wait = db_retry_wait self.async_db_ready = asyncio.Future() self.engine: Any = None self.run_info: Any = None @@ -217,9 +235,9 @@ def run(self): tries = 1 connected = False - while not connected and tries <= 10: + while not connected and tries <= self.db_max_retries: if tries != 1: - time.sleep(CONNECT_RETRY_WAIT) + time.sleep(self.db_retry_wait) try: self._setup_connection() migration.migrate_schema(self) @@ -230,7 +248,7 @@ def run(self): _LOGGER.error( "Error during connection setup: %s (retrying in %s seconds)", err, - CONNECT_RETRY_WAIT, + self.db_retry_wait, ) tries += 1 @@ -337,9 +355,9 @@ def async_purge(now): tries = 1 updated = False - while not updated and tries <= 10: + while not updated and tries <= self.db_max_retries: if tries != 1: - time.sleep(CONNECT_RETRY_WAIT) + time.sleep(self.db_retry_wait) try: with session_scope(session=self.get_session()) as session: try: @@ -367,7 +385,7 @@ def async_purge(now): "Error in database connectivity: %s. " "(retrying in %s seconds)", err, - CONNECT_RETRY_WAIT, + self.db_retry_wait, ) tries += 1 From 18dfd975a349337be33c08b7b79c7492befc520e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Fri, 7 Feb 2020 00:47:51 +0100 Subject: [PATCH 2/9] fixed test_recorder_setup_failure I failed because it was missing the two new variables. I simply added these with default values. --- tests/components/recorder/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index ae04066651f029..8b171a0c04abc4 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -198,7 +198,7 @@ def test_recorder_setup_failure(): ): setup.side_effect = ImportError("driver not found") rec = Recorder( - hass, keep_days=7, purge_interval=2, uri="sqlite://", include={}, exclude={} + hass, keep_days=7, purge_interval=2, uri="sqlite://", db_max_retries=10, db_retry_wait=3 include={}, exclude={} ) rec.start() rec.join() From 1288de6ea7a7e9fae186675181d1534c5ff401ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Fri, 7 Feb 2020 00:55:21 +0100 Subject: [PATCH 3/9] fixed syntax error in test_recorder_setup_failure --- tests/components/recorder/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 8b171a0c04abc4..43e1f612fc422a 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -198,7 +198,7 @@ def test_recorder_setup_failure(): ): setup.side_effect = ImportError("driver not found") rec = Recorder( - hass, keep_days=7, purge_interval=2, uri="sqlite://", db_max_retries=10, db_retry_wait=3 include={}, exclude={} + hass, keep_days=7, purge_interval=2, uri="sqlite://", db_max_retries=10, db_retry_wait=3, include={}, exclude={} ) rec.start() rec.join() From ca1dd5c95c881266fefc61cc1e681cc9d6adb459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Fri, 7 Feb 2020 19:38:48 +0100 Subject: [PATCH 4/9] fixed formatting error in test_init_py for recorder component --- tests/components/recorder/test_init.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 43e1f612fc422a..95ca474f45fa59 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -198,7 +198,14 @@ def test_recorder_setup_failure(): ): setup.side_effect = ImportError("driver not found") rec = Recorder( - hass, keep_days=7, purge_interval=2, uri="sqlite://", db_max_retries=10, db_retry_wait=3, include={}, exclude={} + hass, + keep_days=7, + purge_interval=2, + uri="sqlite://", + db_max_retries=10, + db_retry_wait=3, + include={}, + exclude={} ) rec.start() rec.join() From 5a3e012160733e5b5ab558faa7d631c89c1f0d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Fri, 7 Feb 2020 19:44:33 +0100 Subject: [PATCH 5/9] fixed typo in test case --- tests/components/recorder/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 95ca474f45fa59..a21ef578ca9c8a 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -205,7 +205,7 @@ def test_recorder_setup_failure(): db_max_retries=10, db_retry_wait=3, include={}, - exclude={} + exclude={}, ) rec.start() rec.join() From 8bac36bdd014bd9f2f5299aae40019bf23ea6ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Sat, 8 Feb 2020 18:13:36 +0100 Subject: [PATCH 6/9] Updated the way the default keys for db_,max_wait and db_retry_wait is set Implemented based on suggestions from @springstan --- homeassistant/components/recorder/__init__.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 739c9ef178bb95..7608bfb7eafdaa 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -98,8 +98,8 @@ vol.Coerce(int), vol.Range(min=0) ), vol.Optional(CONF_DB_URL): cv.string, - vol.Optional(CONF_DB_MAX_RETRIES): cv.positive_int, - vol.Optional(CONF_DB_RETRY_WAIT): cv.positive_int, + vol.Optional(CONF_DB_MAX_RETRIES, default=DEFAULT_DB_MAX_RETRIES): cv.positive_int, + vol.Optional(CONF_DB_RETRY_WAIT, default=DEFAULT_DB_RETRY_WAIT): cv.positive_int, } ) }, @@ -142,13 +142,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if not db_url: db_url = DEFAULT_URL.format(hass_config_path=hass.config.path(DEFAULT_DB_FILE)) - db_max_retries = conf.get(CONF_DB_MAX_RETRIES, None) - if not db_max_retries: - db_max_retries = DEFAULT_DB_MAX_RETRIES - - db_retry_wait = conf.get(CONF_DB_RETRY_WAIT, None) - if not db_retry_wait: - db_retry_wait = DEFAULT_DB_RETRY_WAIT + db_max_retries = conf.get(CONF_DB_MAX_RETRIES) + db_retry_wait = conf.get(CONF_DB_RETRY_WAIT) include = conf.get(CONF_INCLUDE, {}) exclude = conf.get(CONF_EXCLUDE, {}) From 5505ba21cb1b477e1cbd37d410221c611fcc990f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Sat, 8 Feb 2020 18:20:07 +0100 Subject: [PATCH 7/9] Updated config_schema call to adhere to Black --- homeassistant/components/recorder/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 7608bfb7eafdaa..bb1e37dfd710f3 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -98,8 +98,12 @@ vol.Coerce(int), vol.Range(min=0) ), vol.Optional(CONF_DB_URL): cv.string, - vol.Optional(CONF_DB_MAX_RETRIES, default=DEFAULT_DB_MAX_RETRIES): cv.positive_int, - vol.Optional(CONF_DB_RETRY_WAIT, default=DEFAULT_DB_RETRY_WAIT): cv.positive_int, + vol.Optional( + CONF_DB_MAX_RETRIES, default=DEFAULT_DB_MAX_RETRIES + ): cv.positive_int, + vol.Optional( + CONF_DB_RETRY_WAIT, default=DEFAULT_DB_RETRY_WAIT + ): cv.positive_int, } ) }, From d11d446a7b42f8c21c8fe09329caa1b488109a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Sat, 8 Feb 2020 19:46:54 +0100 Subject: [PATCH 8/9] changed conf.get to conf[dict] for var retrieval --- homeassistant/components/recorder/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index bb1e37dfd710f3..fba7c326f53c2e 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -141,13 +141,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: conf = config[DOMAIN] keep_days = conf.get(CONF_PURGE_KEEP_DAYS) purge_interval = conf.get(CONF_PURGE_INTERVAL) + db_max_retries = conf[CONF_DB_MAX_RETRIES] + db_retry_wait = conf[CONF_DB_RETRY_WAIT] db_url = conf.get(CONF_DB_URL, None) if not db_url: db_url = DEFAULT_URL.format(hass_config_path=hass.config.path(DEFAULT_DB_FILE)) - db_max_retries = conf.get(CONF_DB_MAX_RETRIES) - db_retry_wait = conf.get(CONF_DB_RETRY_WAIT) + include = conf.get(CONF_INCLUDE, {}) exclude = conf.get(CONF_EXCLUDE, {}) From 8112a1eadef2a658b2cca1f0313c07b22b199542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20=C3=98strup?= Date: Sat, 8 Feb 2020 19:59:31 +0100 Subject: [PATCH 9/9] removed 2 blank lines --- homeassistant/components/recorder/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index fba7c326f53c2e..af34d4dd9f6318 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -148,8 +148,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if not db_url: db_url = DEFAULT_URL.format(hass_config_path=hass.config.path(DEFAULT_DB_FILE)) - - include = conf.get(CONF_INCLUDE, {}) exclude = conf.get(CONF_EXCLUDE, {}) instance = hass.data[DATA_INSTANCE] = Recorder(