Be a bit more specific with exception handling.

This commit is contained in:
Michael Lipp 2023-08-21 17:40:51 +02:00
parent 79da75e806
commit ef708476ca

View file

@ -199,15 +199,14 @@ public class VmWatcher extends Component {
return result; return result;
} }
@SuppressWarnings("PMD.CognitiveComplexity")
private void watchVmDefs(V1APIResource crd, String version) { private void watchVmDefs(V1APIResource crd, String version) {
@SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops",
"PMD.AvoidLiteralsInIfCondition", "PMD.AvoidCatchingThrowable" }) "PMD.AvoidCatchingThrowable", "PMD.AvoidCatchingGenericException" })
var watcher = new Thread(() -> { var watcher = new Thread(() -> {
try { try {
// Watch sometimes terminates without apparent reason. // Watch sometimes terminates without apparent reason.
while (true) { while (true) {
Instant started = Instant.now(); Instant startedAt = Instant.now();
var client = Config.defaultClient(); var client = Config.defaultClient();
var coa = new CustomObjectsApi(client); var coa = new CustomObjectsApi(client);
var call = coa.listNamespacedCustomObjectCall(VM_OP_GROUP, var call = coa.listNamespacedCustomObjectCall(VM_OP_GROUP,
@ -220,23 +219,14 @@ public class VmWatcher extends Component {
for (Watch.Response<V1Namespace> item : watch) { for (Watch.Response<V1Namespace> item : watch) {
handleVmDefinitionChange(crd, item); handleVmDefinitionChange(crd, item);
} }
} catch (Throwable e) { } catch (IOException | ApiException | RuntimeException e) {
logger.log(Level.FINE, e, () -> "Probem watching " logger.log(Level.FINE, e, () -> "Problem watching \""
+ "(retrying): " + e.getMessage()); + crd.getName() + "\" (will retry): "
var runningFor = Duration + e.getMessage());
.between(started, Instant.now()).getSeconds(); delayRestart(startedAt);
if (runningFor < 5) {
logger.log(Level.FINE, e, () -> "Waiting... ");
try {
Thread.sleep(5000 - runningFor * 1000);
} catch (InterruptedException e1) {
continue;
}
logger.log(Level.FINE, e, () -> "Restarting");
}
} }
} }
} catch (IOException | ApiException e) { } catch (Throwable e) {
logger.log(Level.SEVERE, e, () -> "Probem watching: " logger.log(Level.SEVERE, e, () -> "Probem watching: "
+ e.getMessage()); + e.getMessage());
} }
@ -246,6 +236,21 @@ public class VmWatcher extends Component {
watcher.start(); watcher.start();
} }
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
private void delayRestart(Instant started) {
var runningFor = Duration
.between(started, Instant.now()).toMillis();
if (runningFor < 5000) {
logger.log(Level.FINE, () -> "Waiting... ");
try {
Thread.sleep(5000 - runningFor);
} catch (InterruptedException e1) { // NOPMD
// Retry
}
logger.log(Level.FINE, () -> "Retrying");
}
}
private void handleVmDefinitionChange(V1APIResource vmsCrd, private void handleVmDefinitionChange(V1APIResource vmsCrd,
Watch.Response<V1Namespace> item) { Watch.Response<V1Namespace> item) {
V1ObjectMeta metadata = item.object.getMetadata(); V1ObjectMeta metadata = item.object.getMetadata();