Skip to content

Commit d018aa8

Browse files
committed
Merge branch 'gh-35544'
Closes gh-35544
2 parents 3d41e41 + 6b0b6cc commit d018aa8

17 files changed

+125
-348
lines changed

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.boot.docker.compose.core.RunningService;
3232
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Start;
3333
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Stop;
34-
import org.springframework.boot.docker.compose.readiness.ServiceReadinessChecks;
3534
import org.springframework.context.ApplicationContext;
3635
import org.springframework.context.ApplicationListener;
3736
import org.springframework.context.event.SimpleApplicationEventMulticaster;
@@ -88,7 +87,7 @@ class DockerComposeLifecycleManager {
8887
this.eventListeners = eventListeners;
8988
this.skipCheck = skipCheck;
9089
this.serviceReadinessChecks = (serviceReadinessChecks != null) ? serviceReadinessChecks
91-
: new ServiceReadinessChecks(this.classLoader, applicationContext.getEnvironment(), binder);
90+
: new ServiceReadinessChecks(properties.getReadiness());
9291
}
9392

9493
void start() {

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeProperties.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public class DockerComposeProperties {
7575

7676
private final Skip skip = new Skip();
7777

78+
private final Readiness readiness = new Readiness();
79+
7880
public boolean isEnabled() {
7981
return this.enabled;
8082
}
@@ -123,6 +125,10 @@ public Skip getSkip() {
123125
return this.skip;
124126
}
125127

128+
public Readiness getReadiness() {
129+
return this.readiness;
130+
}
131+
126132
static DockerComposeProperties get(Binder binder) {
127133
return binder.bind(NAME, DockerComposeProperties.class).orElseGet(DockerComposeProperties::new);
128134
}
@@ -233,4 +239,66 @@ public void setInTests(boolean inTests) {
233239

234240
}
235241

242+
/**
243+
* Readiness properties.
244+
*/
245+
public static class Readiness {
246+
247+
/**
248+
* Timeout of the readiness checks.
249+
*/
250+
private Duration timeout = Duration.ofMinutes(2);
251+
252+
/**
253+
* TCP properties.
254+
*/
255+
private final Tcp tcp = new Tcp();
256+
257+
public Duration getTimeout() {
258+
return this.timeout;
259+
}
260+
261+
public void setTimeout(Duration timeout) {
262+
this.timeout = timeout;
263+
}
264+
265+
public Tcp getTcp() {
266+
return this.tcp;
267+
}
268+
269+
/**
270+
* TCP properties.
271+
*/
272+
public static class Tcp {
273+
274+
/**
275+
* Timeout for connections.
276+
*/
277+
private Duration connectTimeout = Duration.ofMillis(200);
278+
279+
/**
280+
* Timeout for reads.
281+
*/
282+
private Duration readTimeout = Duration.ofMillis(200);
283+
284+
public Duration getConnectTimeout() {
285+
return this.connectTimeout;
286+
}
287+
288+
public void setConnectTimeout(Duration connectTimeout) {
289+
this.connectTimeout = connectTimeout;
290+
}
291+
292+
public Duration getReadTimeout() {
293+
return this.readTimeout;
294+
}
295+
296+
public void setReadTimeout(Duration readTimeout) {
297+
this.readTimeout = readTimeout;
298+
}
299+
300+
}
301+
302+
}
303+
236304
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.docker.compose.readiness;
17+
package org.springframework.boot.docker.compose.lifecycle;
1818

1919
import java.time.Duration;
2020
import java.util.List;
@@ -23,9 +23,7 @@
2323
import org.springframework.boot.docker.compose.core.RunningService;
2424

2525
/**
26-
* Exception thrown if readiness checking has timed out. Related
27-
* {@link ServiceNotReadyException ServiceNotReadyExceptions} are available from
28-
* {@link #getSuppressed()}.
26+
* Exception thrown if readiness checking has timed out.
2927
*
3028
* @author Moritz Halbritter
3129
* @author Andy Wilkinson
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.docker.compose.readiness;
17+
package org.springframework.boot.docker.compose.lifecycle;
1818

1919
import org.springframework.boot.docker.compose.core.RunningService;
2020

@@ -24,10 +24,8 @@
2424
* @author Moritz Halbritter
2525
* @author Andy Wilkinson
2626
* @author Phillip Webb
27-
* @since 3.1.0
28-
* @see ServiceReadinessCheck
2927
*/
30-
public class ServiceNotReadyException extends RuntimeException {
28+
class ServiceNotReadyException extends RuntimeException {
3129

3230
private final RunningService service;
3331

@@ -40,11 +38,7 @@ public class ServiceNotReadyException extends RuntimeException {
4038
this.service = service;
4139
}
4240

43-
/**
44-
* Return the service that was not ready.
45-
* @return the non-ready service
46-
*/
47-
public RunningService getService() {
41+
RunningService getService() {
4842
return this.service;
4943
}
5044

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.docker.compose.readiness;
17+
package org.springframework.boot.docker.compose.lifecycle;
1818

1919
import java.time.Clock;
2020
import java.time.Duration;
@@ -23,28 +23,21 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.function.Consumer;
26-
import java.util.function.Function;
2726

2827
import org.apache.commons.logging.Log;
2928
import org.apache.commons.logging.LogFactory;
3029

31-
import org.springframework.boot.context.properties.bind.Binder;
3230
import org.springframework.boot.docker.compose.core.RunningService;
33-
import org.springframework.core.env.Environment;
34-
import org.springframework.core.io.support.SpringFactoriesLoader;
35-
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
3631
import org.springframework.core.log.LogMessage;
3732

3833
/**
39-
* A collection of {@link ServiceReadinessCheck} instances that can be used to
40-
* {@link #wait() wait} for {@link RunningService services} to be ready.
34+
* Utility used to {@link #wait() wait} for {@link RunningService services} to be ready.
4135
*
4236
* @author Moritz Halbritter
4337
* @author Andy Wilkinson
4438
* @author Phillip Webb
45-
* @since 3.1.0
4639
*/
47-
public class ServiceReadinessChecks {
40+
class ServiceReadinessChecks {
4841

4942
private static final Log logger = LogFactory.getLog(ServiceReadinessChecks.class);
5043

@@ -56,34 +49,28 @@ public class ServiceReadinessChecks {
5649

5750
private final Consumer<Duration> sleep;
5851

59-
private final ReadinessProperties properties;
52+
private final DockerComposeProperties.Readiness properties;
6053

61-
private final List<ServiceReadinessCheck> checks;
54+
private final TcpConnectServiceReadinessCheck check;
6255

63-
public ServiceReadinessChecks(ClassLoader classLoader, Environment environment, Binder binder) {
64-
this(Clock.systemUTC(), ServiceReadinessChecks::sleep,
65-
SpringFactoriesLoader.forDefaultResourceLocation(classLoader), classLoader, environment, binder,
66-
TcpConnectServiceReadinessCheck::new);
56+
ServiceReadinessChecks(DockerComposeProperties.Readiness properties) {
57+
this(properties, Clock.systemUTC(), ServiceReadinessChecks::sleep,
58+
new TcpConnectServiceReadinessCheck(properties.getTcp()));
6759
}
6860

69-
ServiceReadinessChecks(Clock clock, Consumer<Duration> sleep, SpringFactoriesLoader loader, ClassLoader classLoader,
70-
Environment environment, Binder binder,
71-
Function<ReadinessProperties.Tcp, ServiceReadinessCheck> tcpCheckFactory) {
72-
ArgumentResolver argumentResolver = ArgumentResolver.of(ClassLoader.class, classLoader)
73-
.and(Environment.class, environment)
74-
.and(Binder.class, binder);
61+
ServiceReadinessChecks(DockerComposeProperties.Readiness properties, Clock clock, Consumer<Duration> sleep,
62+
TcpConnectServiceReadinessCheck check) {
7563
this.clock = clock;
7664
this.sleep = sleep;
77-
this.properties = ReadinessProperties.get(binder);
78-
this.checks = new ArrayList<>(loader.load(ServiceReadinessCheck.class, argumentResolver));
79-
this.checks.add(tcpCheckFactory.apply(this.properties.getTcp()));
65+
this.properties = properties;
66+
this.check = check;
8067
}
8168

8269
/**
8370
* Wait for the given services to be ready.
8471
* @param runningServices the services to wait for
8572
*/
86-
public void waitUntilReady(List<RunningService> runningServices) {
73+
void waitUntilReady(List<RunningService> runningServices) {
8774
Duration timeout = this.properties.getTimeout();
8875
Instant start = this.clock.instant();
8976
while (true) {
@@ -106,16 +93,14 @@ private List<ServiceNotReadyException> check(List<RunningService> runningService
10693
continue;
10794
}
10895
logger.trace(LogMessage.format("Checking readiness of service '%s'", service));
109-
for (ServiceReadinessCheck check : this.checks) {
110-
try {
111-
check.check(service);
112-
logger.trace(LogMessage.format("Service '%s' is ready", service));
113-
}
114-
catch (ServiceNotReadyException ex) {
115-
logger.trace(LogMessage.format("Service '%s' is not ready", service), ex);
116-
exceptions = (exceptions != null) ? exceptions : new ArrayList<>();
117-
exceptions.add(ex);
118-
}
96+
try {
97+
this.check.check(service);
98+
logger.trace(LogMessage.format("Service '%s' is ready", service));
99+
}
100+
catch (ServiceNotReadyException ex) {
101+
logger.trace(LogMessage.format("Service '%s' is not ready", service), ex);
102+
exceptions = (exceptions != null) ? exceptions : new ArrayList<>();
103+
exceptions.add(ex);
119104
}
120105
}
121106
return (exceptions != null) ? exceptions : Collections.emptyList();
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.docker.compose.readiness;
17+
package org.springframework.boot.docker.compose.lifecycle;
1818

1919
import java.io.IOException;
2020
import java.net.InetSocketAddress;
@@ -24,25 +24,23 @@
2424
import org.springframework.boot.docker.compose.core.RunningService;
2525

2626
/**
27-
* Default {@link ServiceReadinessCheck} that checks readiness by connecting to the
28-
* exposed TCP ports.
27+
* Checks readiness by connecting to the exposed TCP ports.
2928
*
3029
* @author Moritz Halbritter
3130
* @author Andy Wilkinson
3231
* @author Phillip Webb
3332
*/
34-
class TcpConnectServiceReadinessCheck implements ServiceReadinessCheck {
33+
class TcpConnectServiceReadinessCheck {
3534

3635
private static final String DISABLE_LABEL = "org.springframework.boot.readiness-check.tcp.disable";
3736

38-
private final ReadinessProperties.Tcp properties;
37+
private final DockerComposeProperties.Readiness.Tcp properties;
3938

40-
TcpConnectServiceReadinessCheck(ReadinessProperties.Tcp properties) {
39+
TcpConnectServiceReadinessCheck(DockerComposeProperties.Readiness.Tcp properties) {
4140
this.properties = properties;
4241
}
4342

44-
@Override
45-
public void check(RunningService service) {
43+
void check(RunningService service) {
4644
if (service.labels().containsKey(DISABLE_LABEL)) {
4745
return;
4846
}

0 commit comments

Comments
 (0)