diff --git a/src/main/java/io/cryostat/agent/Agent.java b/src/main/java/io/cryostat/agent/Agent.java index 97a12f1a..5c90b927 100644 --- a/src/main/java/io/cryostat/agent/Agent.java +++ b/src/main/java/io/cryostat/agent/Agent.java @@ -99,10 +99,18 @@ public static void main(String[] args) { client.registration() .addRegistrationListener( evt -> { - if (evt.state) { - client.harvester().start(); - } else { - client.harvester().stop(); + switch (evt.state) { + case REGISTERED: + client.harvester().start(); + break; + case UNREGISTERED: + client.harvester().stop(); + break; + case REFRESHED: + break; + default: + log.error("Unknown registration state: {}", evt.state); + break; } }); client.registration().start(); diff --git a/src/main/java/io/cryostat/agent/Harvester.java b/src/main/java/io/cryostat/agent/Harvester.java index c5237a80..567e2bd9 100644 --- a/src/main/java/io/cryostat/agent/Harvester.java +++ b/src/main/java/io/cryostat/agent/Harvester.java @@ -84,6 +84,9 @@ class Harvester implements FlightRecorderListener { } public void start() { + if (running) { + return; + } if (period <= 0) { log.info("Harvester disabled, period {} < 0", period); return; @@ -99,7 +102,7 @@ public void start() { try { FlightRecorder.addListener(this); this.flightRecorder = FlightRecorder.getFlightRecorder(); - log.info("JFR Harvester started"); + log.info("JFR Harvester started using template {} with period {}ms", template, period); } catch (SecurityException | IllegalStateException e) { log.error("Harvester could not start", e); return; diff --git a/src/main/java/io/cryostat/agent/Registration.java b/src/main/java/io/cryostat/agent/Registration.java index 2eb6fcaa..5858e312 100644 --- a/src/main/java/io/cryostat/agent/Registration.java +++ b/src/main/java/io/cryostat/agent/Registration.java @@ -110,12 +110,18 @@ void tryRegister() { .handleAsync( (plugin, t) -> { if (plugin != null) { + boolean previouslyRegistered = + this.pluginInfo.isInitialized(); this.pluginInfo.copyFrom(plugin); log.info("Registered as {}", this.pluginInfo.getId()); - notify(true); + notify( + previouslyRegistered + ? RegistrationEvent.State.REFRESHED + : RegistrationEvent.State.REGISTERED); tryUpdate(); } else if (t != null) { - notify(false); + this.pluginInfo.clear(); + notify(RegistrationEvent.State.UNREGISTERED); throw new RegistrationException(t); } @@ -214,32 +220,38 @@ CompletableFuture deregister() { return cryostat.deregister(pluginInfo) .handleAsync( (n, t) -> { + this.pluginInfo.clear(); + notify(RegistrationEvent.State.UNREGISTERED); if (t != null) { - notify(false); log.warn( "Failed to deregister as Cryostat discovery plugin [{}]", this.pluginInfo.getId()); } else { - notify(false); log.info( "Deregistered from Cryostat discovery plugin [{}]", this.pluginInfo.getId()); } - this.pluginInfo.clear(); return null; }, executor); } - private void notify(boolean state) { + private void notify(RegistrationEvent.State state) { RegistrationEvent evt = new RegistrationEvent(state); this.listeners.forEach(listener -> listener.accept(evt)); } static class RegistrationEvent { - boolean state; - RegistrationEvent(boolean state) { + enum State { + REGISTERED, + UNREGISTERED, + REFRESHED, + } + + final State state; + + RegistrationEvent(State state) { this.state = state; } }