Improved error handling.

This commit is contained in:
Mark Nellemann 2022-02-10 17:39:53 +01:00
parent e0dc65a1e6
commit 02804232b0
3 changed files with 9 additions and 4 deletions

View file

@ -29,7 +29,7 @@ dependencies {
implementation 'com.squareup.moshi:moshi:1.13.0' implementation 'com.squareup.moshi:moshi:1.13.0'
implementation 'com.serjltt.moshi:moshi-lazy-adapters:2.2' implementation 'com.serjltt.moshi:moshi-lazy-adapters:2.2'
implementation 'org.tomlj:tomlj:1.0.0' implementation 'org.tomlj:tomlj:1.0.0'
implementation 'org.influxdb:influxdb-java:2.21' implementation 'org.influxdb:influxdb-java:2.22'
implementation 'org.slf4j:slf4j-api:1.7.35' implementation 'org.slf4j:slf4j-api:1.7.35'
implementation 'org.slf4j:slf4j-simple:1.7.35' implementation 'org.slf4j:slf4j-simple:1.7.35'

View file

@ -107,9 +107,9 @@ class HmcInstance implements Runnable {
executions = 0; executions = 0;
discover(); discover();
} }
} catch (Exception e) { } catch (Exception e) {
log.error("run()", e); log.error("run() - fatal error: {}", e.getMessage());
throw new RuntimeException(e);
} }
Instant instantEnd = Instant.now(); Instant instantEnd = Instant.now();

View file

@ -67,6 +67,7 @@ class InfluxClient {
try { try {
log.debug("Connecting to InfluxDB - {}", url); log.debug("Connecting to InfluxDB - {}", url);
influxDB = InfluxDBFactory.connect(url, username, password).setDatabase(database); influxDB = InfluxDBFactory.connect(url, username, password).setDatabase(database);
influxDB.version(); // This ensures that we actually try to connect to the db
batchPoints = BatchPoints.database(database).precision(TimeUnit.SECONDS).build(); batchPoints = BatchPoints.database(database).precision(TimeUnit.SECONDS).build();
connected = true; connected = true;
} catch(Exception e) { } catch(Exception e) {
@ -95,8 +96,12 @@ class InfluxClient {
log.trace("writeBatchPoints()"); log.trace("writeBatchPoints()");
try { try {
influxDB.writeWithRetry(batchPoints); influxDB.writeWithRetry(batchPoints);
errorCounter = 0;
} catch (InfluxDBException.DatabaseNotFoundException e) { } catch (InfluxDBException.DatabaseNotFoundException e) {
log.error("writeBatchPoints() - database \"{}\" not found/created: can't write data", database); log.error("writeBatchPoints() - database \"{}\" not found/created: can't write data", database);
if(++errorCounter > 3) {
throw new RuntimeException(e);
}
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
log.warn("writeBatchPoints() {}", e.getMessage()); log.warn("writeBatchPoints() {}", e.getMessage());