Skip to content

Commit

Permalink
extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Aug 17, 2022
1 parent db659d6 commit 1c69e50
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions src/main/java/io/cryostat/agent/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,46 @@ private void tryUpdate(Long id) {
if (pluginInfo == null) {
return;
}
DiscoveryNode selfNode;
try {
selfNode = defineSelf();
} catch (UnknownHostException uhe) {
log.error("Unable to define self", uhe);
return;
}
log.info("publishing self as {}", selfNode.getTarget().getConnectUrl());
cryostat.update(pluginInfo.getId(), Set.of(selfNode))
.onSuccess(
ar -> {
if (id != null) {
getVertx().cancelTimer(id);
}
})
.onFailure(
t -> {
log.error("Update failure", t);
deregister()
.onComplete(
ar -> {
if (ar.failed()) {
Duration registrationRetryPeriod =
Duration.ofSeconds(5);
vertx.setTimer(
registrationRetryPeriod.toMillis(),
this::tryRegister);
return;
}
});
});
}

private DiscoveryNode defineSelf() throws UnknownHostException {
String jmxhost = "localhost";
String appName = "cryostat-agent";
int port = Integer.valueOf(System.getProperty("com.sun.management.jmxremote.port"));

long pid = ProcessHandle.current().pid();
String hostname = null;
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException uhe) {
log.error("Could not determine own hostname", uhe);
}
String hostname = InetAddress.getLocalHost().getHostName();
String javaMain = System.getProperty("sun.java.command", System.getenv("JAVA_MAIN_CLASS"));
if (StringUtils.isBlank(javaMain)) {
javaMain = null;
Expand All @@ -156,31 +185,7 @@ private void tryUpdate(Long id) {

DiscoveryNode selfNode =
new DiscoveryNode("cryostat-agent-" + pluginInfo.getId(), NODE_TYPE, target);

log.info("publishing self as {}", selfNode.getTarget().getConnectUrl());
cryostat.update(pluginInfo.getId(), Set.of(selfNode))
.onSuccess(
ar -> {
if (id != null) {
getVertx().cancelTimer(id);
}
})
.onFailure(
t -> {
log.error("Update failure", t);
deregister()
.onComplete(
ar -> {
if (ar.failed()) {
Duration registrationRetryPeriod =
Duration.ofSeconds(5);
vertx.setTimer(
registrationRetryPeriod.toMillis(),
this::tryRegister);
return;
}
});
});
return selfNode;
}

@Override
Expand Down

0 comments on commit 1c69e50

Please sign in to comment.