Remove pluggable docker compose ReadinessCheck

Remove pluggable `ReadinessCheck` interface and only use the
`TcpConnectServiceReadinessCheck` implementation for now. We
may re-introduce pluggable checks in a future version.

See gh-35544
This commit is contained in:
Phillip Webb
2023-05-17 14:19:18 -07:00
parent 3d41e41b94
commit 060581d078
6 changed files with 26 additions and 117 deletions

View File

@@ -24,10 +24,8 @@ import org.springframework.boot.docker.compose.core.RunningService;
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @since 3.1.0
* @see ServiceReadinessCheck
*/
public class ServiceNotReadyException extends RuntimeException {
class ServiceNotReadyException extends RuntimeException {
private final RunningService service;
@@ -40,11 +38,7 @@ public class ServiceNotReadyException extends RuntimeException {
this.service = service;
}
/**
* Return the service that was not ready.
* @return the non-ready service
*/
public RunningService getService() {
RunningService getService() {
return this.service;
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.readiness;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.core.env.Environment;
/**
* Strategy used to check if a {@link RunningService} is ready. Implementations may be
* registered in {@code spring.factories}. The following constructor arguments types are
* supported:
* <ul>
* <li>{@link ClassLoader}</li>
* <li>{@link Environment}</li>
* <li>{@link Binder}</li>
* </ul>
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @since 3.1.0
*/
public interface ServiceReadinessCheck {
/**
* Checks whether the given {@code service} is ready.
* @param service service to check
* @throws ServiceNotReadyException if the service is not ready
*/
void check(RunningService service) throws ServiceNotReadyException;
}

View File

@@ -32,12 +32,10 @@ import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
import org.springframework.core.log.LogMessage;
/**
* A collection of {@link ServiceReadinessCheck} instances that can be used to
* {@link #wait() wait} for {@link RunningService services} to be ready.
* Utility used to {@link #wait() wait} for {@link RunningService services} to be ready.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
@@ -58,7 +56,7 @@ public class ServiceReadinessChecks {
private final ReadinessProperties properties;
private final List<ServiceReadinessCheck> checks;
private final TcpConnectServiceReadinessCheck check;
public ServiceReadinessChecks(ClassLoader classLoader, Environment environment, Binder binder) {
this(Clock.systemUTC(), ServiceReadinessChecks::sleep,
@@ -68,15 +66,11 @@ public class ServiceReadinessChecks {
ServiceReadinessChecks(Clock clock, Consumer<Duration> sleep, SpringFactoriesLoader loader, ClassLoader classLoader,
Environment environment, Binder binder,
Function<ReadinessProperties.Tcp, ServiceReadinessCheck> tcpCheckFactory) {
ArgumentResolver argumentResolver = ArgumentResolver.of(ClassLoader.class, classLoader)
.and(Environment.class, environment)
.and(Binder.class, binder);
Function<ReadinessProperties.Tcp, TcpConnectServiceReadinessCheck> tcpCheckFactory) {
this.clock = clock;
this.sleep = sleep;
this.properties = ReadinessProperties.get(binder);
this.checks = new ArrayList<>(loader.load(ServiceReadinessCheck.class, argumentResolver));
this.checks.add(tcpCheckFactory.apply(this.properties.getTcp()));
this.check = tcpCheckFactory.apply(this.properties.getTcp());
}
/**
@@ -106,16 +100,14 @@ public class ServiceReadinessChecks {
continue;
}
logger.trace(LogMessage.format("Checking readiness of service '%s'", service));
for (ServiceReadinessCheck check : this.checks) {
try {
check.check(service);
logger.trace(LogMessage.format("Service '%s' is ready", service));
}
catch (ServiceNotReadyException ex) {
logger.trace(LogMessage.format("Service '%s' is not ready", service), ex);
exceptions = (exceptions != null) ? exceptions : new ArrayList<>();
exceptions.add(ex);
}
try {
this.check.check(service);
logger.trace(LogMessage.format("Service '%s' is ready", service));
}
catch (ServiceNotReadyException ex) {
logger.trace(LogMessage.format("Service '%s' is not ready", service), ex);
exceptions = (exceptions != null) ? exceptions : new ArrayList<>();
exceptions.add(ex);
}
}
return (exceptions != null) ? exceptions : Collections.emptyList();

View File

@@ -24,14 +24,13 @@ import java.net.SocketTimeoutException;
import org.springframework.boot.docker.compose.core.RunningService;
/**
* Default {@link ServiceReadinessCheck} that checks readiness by connecting to the
* exposed TCP ports.
* Checks readiness by connecting to the exposed TCP ports.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class TcpConnectServiceReadinessCheck implements ServiceReadinessCheck {
class TcpConnectServiceReadinessCheck {
private static final String DISABLE_LABEL = "org.springframework.boot.readiness-check.tcp.disable";
@@ -41,8 +40,7 @@ class TcpConnectServiceReadinessCheck implements ServiceReadinessCheck {
this.properties = properties;
}
@Override
public void check(RunningService service) {
void check(RunningService service) {
if (service.labels().containsKey(DISABLE_LABEL)) {
return;
}