Be a bit more specific with exception handling.
This commit is contained in:
parent
79da75e806
commit
ef708476ca
1 changed files with 23 additions and 18 deletions
|
|
@ -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 (IOException | ApiException | RuntimeException e) {
|
||||||
|
logger.log(Level.FINE, e, () -> "Problem watching \""
|
||||||
|
+ crd.getName() + "\" (will retry): "
|
||||||
|
+ e.getMessage());
|
||||||
|
delayRestart(startedAt);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
logger.log(Level.FINE, e, () -> "Probem watching "
|
|
||||||
+ "(retrying): " + e.getMessage());
|
|
||||||
var runningFor = Duration
|
|
||||||
.between(started, Instant.now()).getSeconds();
|
|
||||||
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) {
|
|
||||||
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();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue