Skip to content

Commit 6b0b6cc

Browse files
committed
Move docker compose readiness code and make it package-private
The `ReadinessCheck` interface has been removed making the dedicated package less necessary. By relocating the code we can make more of it pacakge-private. See gh-35544
1 parent 060581d commit 6b0b6cc

15 files changed

+102
-234
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: 1 addition & 1 deletion
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

Lines changed: 11 additions & 18 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,15 +23,11 @@
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;
3531
import org.springframework.core.log.LogMessage;
3632

3733
/**
@@ -40,9 +36,8 @@
4036
* @author Moritz Halbritter
4137
* @author Andy Wilkinson
4238
* @author Phillip Webb
43-
* @since 3.1.0
4439
*/
45-
public class ServiceReadinessChecks {
40+
class ServiceReadinessChecks {
4641

4742
private static final Log logger = LogFactory.getLog(ServiceReadinessChecks.class);
4843

@@ -54,30 +49,28 @@ public class ServiceReadinessChecks {
5449

5550
private final Consumer<Duration> sleep;
5651

57-
private final ReadinessProperties properties;
52+
private final DockerComposeProperties.Readiness properties;
5853

5954
private final TcpConnectServiceReadinessCheck check;
6055

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

67-
ServiceReadinessChecks(Clock clock, Consumer<Duration> sleep, SpringFactoriesLoader loader, ClassLoader classLoader,
68-
Environment environment, Binder binder,
69-
Function<ReadinessProperties.Tcp, TcpConnectServiceReadinessCheck> tcpCheckFactory) {
61+
ServiceReadinessChecks(DockerComposeProperties.Readiness properties, Clock clock, Consumer<Duration> sleep,
62+
TcpConnectServiceReadinessCheck check) {
7063
this.clock = clock;
7164
this.sleep = sleep;
72-
this.properties = ReadinessProperties.get(binder);
73-
this.check = tcpCheckFactory.apply(this.properties.getTcp());
65+
this.properties = properties;
66+
this.check = check;
7467
}
7568

7669
/**
7770
* Wait for the given services to be ready.
7871
* @param runningServices the services to wait for
7972
*/
80-
public void waitUntilReady(List<RunningService> runningServices) {
73+
void waitUntilReady(List<RunningService> runningServices) {
8174
Duration timeout = this.properties.getTimeout();
8275
Instant start = this.clock.instant();
8376
while (true) {
Lines changed: 3 additions & 3 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;
@@ -34,9 +34,9 @@ class TcpConnectServiceReadinessCheck {
3434

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

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

39-
TcpConnectServiceReadinessCheck(ReadinessProperties.Tcp properties) {
39+
TcpConnectServiceReadinessCheck(DockerComposeProperties.Readiness.Tcp properties) {
4040
this.properties = properties;
4141
}
4242

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/readiness/ReadinessProperties.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/readiness/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.boot.docker.compose.core.DockerCompose;
3737
import org.springframework.boot.docker.compose.core.DockerComposeFile;
3838
import org.springframework.boot.docker.compose.core.RunningService;
39-
import org.springframework.boot.docker.compose.readiness.ServiceReadinessChecks;
4039
import org.springframework.context.ApplicationContext;
4140
import org.springframework.context.ApplicationListener;
4241
import org.springframework.context.support.GenericApplicationContext;

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposePropertiesTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void getWhenNoPropertiesReturnsNew() {
4848
assertThat(properties.getStop().getCommand()).isEqualTo(StopCommand.STOP);
4949
assertThat(properties.getStop().getTimeout()).isEqualTo(Duration.ofSeconds(10));
5050
assertThat(properties.getProfiles().getActive()).isEmpty();
51+
assertThat(properties.getReadiness().getTimeout()).isEqualTo(Duration.ofMinutes(2));
52+
assertThat(properties.getReadiness().getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(200));
53+
assertThat(properties.getReadiness().getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(200));
5154
}
5255

5356
@Test
@@ -60,6 +63,9 @@ void getWhenPropertiesReturnsBound() {
6063
source.put("spring.docker.compose.stop.command", "down");
6164
source.put("spring.docker.compose.stop.timeout", "5s");
6265
source.put("spring.docker.compose.profiles.active", "myprofile");
66+
source.put("spring.docker.compose.readiness.timeout", "10s");
67+
source.put("spring.docker.compose.readiness.tcp.connect-timeout", "400ms");
68+
source.put("spring.docker.compose.readiness.tcp.read-timeout", "500ms");
6369
Binder binder = new Binder(new MapConfigurationPropertySource(source));
6470
DockerComposeProperties properties = DockerComposeProperties.get(binder);
6571
assertThat(properties.getFile()).isEqualTo(new File("my-compose.yml"));
@@ -69,6 +75,9 @@ void getWhenPropertiesReturnsBound() {
6975
assertThat(properties.getStop().getCommand()).isEqualTo(StopCommand.DOWN);
7076
assertThat(properties.getStop().getTimeout()).isEqualTo(Duration.ofSeconds(5));
7177
assertThat(properties.getProfiles().getActive()).containsExactly("myprofile");
78+
assertThat(properties.getReadiness().getTimeout()).isEqualTo(Duration.ofSeconds(10));
79+
assertThat(properties.getReadiness().getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(400));
80+
assertThat(properties.getReadiness().getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(500));
7281
}
7382

7483
}

0 commit comments

Comments
 (0)