Skip to content

Commit e22b5f7

Browse files
author
Auri Munoz
committed
Deregistering service instances during shutdown
1 parent 8a50ec6 commit e22b5f7

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

extensions/smallrye-stork/deployment/src/main/java/io/quarkus/stork/deployment/SmallRyeStorkProcessor.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,29 @@ void checkThatTheKubernetesExtensionIsUsedWhenKubernetesServiceDiscoveryInOnTheC
100100
void initializeStork(SmallRyeStorkRecorder storkRecorder, ShutdownContextBuildItem shutdown, VertxBuildItem vertx,
101101
StorkConfiguration configuration) {
102102
storkRecorder.initialize(shutdown, vertx.getVertx(), configuration);
103-
if (QuarkusClassLoader.isClassPresentAtRuntime(SERVICE_REGISTRAR_PROVIDER)) {
104-
storkRecorder.deregisterServiceInstance(shutdown, configuration);
105-
}
106103
}
107104

108105
@BuildStep
109106
@Record(ExecutionTime.RUNTIME_INIT)
110107
void checkStorkConsulRegistrar(BuildProducer<StorkRegistrationBuildItem> registration,
111108
BuildProducer<RunTimeConfigurationDefaultBuildItem> config,
112-
StorkRegistrarConfigRecorder initializerRecorder, StorkConfiguration configuration) {
109+
StorkRegistrarConfigRecorder registrarConfigRecorder, StorkConfiguration configuration) {
113110
if (QuarkusClassLoader.isClassPresentAtRuntime(CONSUL_SERVICE_REGISTRAR_PROVIDER)) {
114-
initializerRecorder.setupServiceRegistrarConfig(configuration, CONSUL_SERVICE_REGISTRAR_TYPE);
111+
registrarConfigRecorder.setupServiceRegistrarConfig(configuration, CONSUL_SERVICE_REGISTRAR_TYPE);
115112
} else if (QuarkusClassLoader.isClassPresentAtRuntime(EUREKA_SERVICE_REGISTRAR_PROVIDER)) {
116-
initializerRecorder.setupServiceRegistrarConfig(configuration, EUREKA_SERVICE_REGISTRAR_TYPE);
113+
registrarConfigRecorder.setupServiceRegistrarConfig(configuration, EUREKA_SERVICE_REGISTRAR_TYPE);
117114
}
118115
registration.produce(new StorkRegistrationBuildItem());
119116

120117
}
121118

122119
@BuildStep
123120
@Record(ExecutionTime.RUNTIME_INIT)
124-
void registerServiceInstance(StorkInitializedBuildItem storkInitializedBuildItem,
121+
void registerServiceInstance(StorkInitializedBuildItem storkInitializedBuildItem, ShutdownContextBuildItem shutdown,
125122
SmallRyeStorkRegistrationRecorder registrationRecorder, StorkConfiguration configuration) {
126123
if (QuarkusClassLoader.isClassPresentAtRuntime(SERVICE_REGISTRAR_PROVIDER)) {
127124
registrationRecorder.registerServiceInstance(configuration);
125+
registrationRecorder.deregisterServiceInstance(shutdown, configuration);
128126
}
129127
}
130128

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package io.quarkus.stork;
22

3-
import java.net.InetAddress;
43
import java.util.List;
5-
import java.util.Map;
64

75
import jakarta.enterprise.inject.Instance;
86
import jakarta.enterprise.inject.spi.CDI;
97

10-
import org.eclipse.microprofile.config.Config;
11-
import org.eclipse.microprofile.config.ConfigProvider;
12-
138
import io.quarkus.runtime.RuntimeValue;
149
import io.quarkus.runtime.ShutdownContext;
1510
import io.quarkus.runtime.annotations.Recorder;
@@ -40,35 +35,4 @@ public void run() {
4035
});
4136
}
4237

43-
public void deregisterServiceInstance(ShutdownContext shutdown, StorkConfiguration configuration) {
44-
shutdown.addLastShutdownTask(new Runnable() {
45-
@Override
46-
public void run() {
47-
Stork.shutdown();
48-
}
49-
});
50-
}
51-
52-
private void deregisterServiceInstance(StorkConfiguration configuration) {
53-
List<ServiceConfig> serviceConfigs = StorkConfigUtil.toStorkServiceConfig(configuration);
54-
List<ServiceConfig> registrationConfigs = serviceConfigs.stream()
55-
.filter(serviceConfig -> serviceConfig.serviceRegistrar() != null).toList();
56-
57-
registrationConfigs.get(0).serviceName();
58-
Config quarkusConfig = ConfigProvider.getConfig();
59-
for (ServiceConfig serviceConfig : serviceConfigs) {
60-
String serviceName = serviceConfig.serviceName();
61-
Map<String, String> parameters = serviceConfig.serviceRegistrar().parameters();
62-
String host = parameters.containsKey("ip-address") ? parameters.get("ip-address")
63-
: quarkusConfig.getValue("quarkus.http.host", String.class);
64-
int port = parameters.containsKey("port") ? Integer.parseInt(parameters.get("port"))
65-
: Integer.parseInt(quarkusConfig.getValue("quarkus.http.port", String.class));
66-
if (host == null || host.isEmpty()) {
67-
InetAddress inetAddress = StorkConfigUtil.detectAddress();
68-
host = inetAddress != null ? inetAddress.getHostAddress() : host;
69-
}
70-
Stork.getInstance().getService(serviceName).getServiceRegistrar().registerServiceInstance(serviceName, host,
71-
port).await().indefinitely();
72-
}
73-
}
7438
}

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/SmallRyeStorkRegistrationRecorder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.microprofile.config.Config;
88
import org.eclipse.microprofile.config.ConfigProvider;
99

10+
import io.quarkus.runtime.ShutdownContext;
1011
import io.quarkus.runtime.annotations.Recorder;
1112
import io.smallrye.stork.Stork;
1213
import io.smallrye.stork.api.config.ServiceConfig;
@@ -32,4 +33,22 @@ public void registerServiceInstance(StorkConfiguration configuration) {
3233
port).await().indefinitely();
3334
}
3435
}
36+
37+
public void deregisterServiceInstance(ShutdownContext shutdown, StorkConfiguration configuration) {
38+
shutdown.addLastShutdownTask(new Runnable() {
39+
@Override
40+
public void run() {
41+
deregisterServiceInstance(configuration);
42+
}
43+
});
44+
}
45+
46+
private void deregisterServiceInstance(StorkConfiguration configuration) {
47+
List<ServiceConfig> serviceConfigs = StorkConfigUtil.toStorkServiceConfig(configuration);
48+
for (ServiceConfig serviceConfig : serviceConfigs) {
49+
String serviceName = serviceConfig.serviceName();
50+
Stork.getInstance().getService(serviceName).getServiceRegistrar().deregisterServiceInstance(serviceName).await()
51+
.indefinitely();
52+
}
53+
}
3554
}

0 commit comments

Comments
 (0)