Allow docker compose service readiness checks to be bypassed

Add `spring.docker.compose.readiness.wait` property that can be used to
determine how Spring Boot should wait for docker compose services to
become ready.

Fixes gh-35545
This commit is contained in:
Phillip Webb
2023-05-17 16:21:34 -07:00
parent d018aa8f6d
commit a8602a1814
4 changed files with 95 additions and 5 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.DockerCompose;
import org.springframework.boot.docker.compose.core.DockerComposeFile;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Readiness.Wait;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Start;
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Stop;
import org.springframework.context.ApplicationContext;
@@ -110,15 +111,19 @@ class DockerComposeLifecycleManager {
LifecycleManagement lifecycleManagement = this.properties.getLifecycleManagement();
Start start = this.properties.getStart();
Stop stop = this.properties.getStop();
Wait wait = this.properties.getReadiness().getWait();
if (lifecycleManagement.shouldStart() && !dockerCompose.hasRunningServices()) {
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
wait = (wait != Wait.ONLY_IF_STARTED) ? wait : Wait.ALWAYS;
if (lifecycleManagement.shouldStop()) {
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
}
}
List<RunningService> runningServices = new ArrayList<>(dockerCompose.getRunningServices());
runningServices.removeIf(this::isIgnored);
this.serviceReadinessChecks.waitUntilReady(runningServices);
if (wait == Wait.ALWAYS || wait == null) {
this.serviceReadinessChecks.waitUntilReady(runningServices);
}
publishEvent(new DockerComposeServicesReadyEvent(this.applicationContext, runningServices));
}

View File

@@ -244,6 +244,11 @@ public class DockerComposeProperties {
*/
public static class Readiness {
/**
* Wait strategy to use.
*/
private Wait wait = Wait.ALWAYS;
/**
* Timeout of the readiness checks.
*/
@@ -254,6 +259,14 @@ public class DockerComposeProperties {
*/
private final Tcp tcp = new Tcp();
public Wait getWait() {
return this.wait;
}
public void setWait(Wait wait) {
this.wait = wait;
}
public Duration getTimeout() {
return this.timeout;
}
@@ -266,6 +279,29 @@ public class DockerComposeProperties {
return this.tcp;
}
/**
* Readiness wait strategies.
*/
public enum Wait {
/**
* Always perform readiness checks.
*/
ALWAYS,
/**
* Always perform readiness checks.
*/
NEVER,
/**
* Only perform readiness checks if docker was started with lifecycle
* management.
*/
ONLY_IF_STARTED
}
/**
* TCP properties.
*/