Skip to content

Commit

Permalink
Use RetryOnError decorator in InfluxDB
Browse files Browse the repository at this point in the history
This replaces the scheduling logic in the InfluxDB component with the
RetryOnError decorator from homeassistant.util

Signed-off-by: Jan Losinski <losinski@wh2.tu-dresden.de>
  • Loading branch information
janLo committed Apr 27, 2017
1 parent ce11b34 commit e2b3d7e
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions homeassistant/components/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
https://home-assistant.io/components/influxdb/
"""
import logging
import datetime

import functools
import voluptuous as vol

from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_UNAVAILABLE, STATE_UNKNOWN, CONF_HOST,
CONF_PORT, CONF_SSL, CONF_VERIFY_SSL, CONF_USERNAME, CONF_PASSWORD,
CONF_EXCLUDE, CONF_INCLUDE, CONF_DOMAINS, CONF_ENTITIES)
from homeassistant.util import RetryOnError
from homeassistant.helpers import state as state_helper
from homeassistant.helpers.event import track_point_in_utc_time
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['influxdb==3.0.0']
Expand Down Expand Up @@ -98,7 +96,6 @@ def setup(hass, config):
default_measurement = conf.get(CONF_DEFAULT_MEASUREMENT)
override_measurement = conf.get(CONF_OVERRIDE_MEASUREMENT)
max_tries = conf.get(CONF_RETRY_COUNT)
retry_delay = datetime.timedelta(seconds=20)

try:
influx = InfluxDBClient(**kwargs)
Expand Down Expand Up @@ -172,28 +169,12 @@ def influx_event_listener(event):

_write_data(json_body)

def _write_data(json_body, current_try=0, event=None):
@RetryOnError(hass, retry_limit=max_tries, retry_delay=20)
def _write_data(json_body):
try:
influx.write_points(json_body)
if current_try > 0:
_LOGGER.info("Retried write to InfluxDB successful.")
except exceptions.InfluxDBClientError:
_LOGGER.exception("Error saving event %s to InfluxDB", json_body)
except IOError as io_error:
if max_tries is not None and current_try < max_tries:
_LOGGER.warning("Could not write data to InfluxDB, "
"try %d/%d will retry: %s",
current_try + 1, max_tries, io_error)

next_ts = dt_util.utcnow() + retry_delay
track_point_in_utc_time(hass,
functools.partial(_write_data,
json_body,
current_try + 1),
next_ts)
else:
_LOGGER.exception("Error saving event %s to InfluxDB",
json_body)

hass.bus.listen(EVENT_STATE_CHANGED, influx_event_listener)

Expand Down

0 comments on commit e2b3d7e

Please sign in to comment.