Skip to content

Commit 6ad71c9

Browse files
wilkinsonaphilwebb
authored andcommitted
Reduce number of CLI calls to improve startup time with devtools
Update `DockerComposeLifecycleManager` so that `docker ps` is not called multiple times. See gh-35435
1 parent b0c76c1 commit 6ad71c9

File tree

5 files changed

+7
-40
lines changed

5 files changed

+7
-40
lines changed

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultDockerCompose.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ public boolean hasDefinedServices() {
6969
return !this.cli.run(new DockerCliCommand.ComposeConfig()).services().isEmpty();
7070
}
7171

72-
@Override
73-
public boolean hasRunningServices() {
74-
return runComposePs().stream().anyMatch(this::isRunning);
75-
}
76-
7772
@Override
7873
public List<RunningService> getRunningServices() {
7974
List<DockerCliComposePsResponse> runningPsResponses = runComposePs().stream().filter(this::isRunning).toList();

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCompose.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ public interface DockerCompose {
7373
*/
7474
boolean hasDefinedServices();
7575

76-
/**
77-
* Return if services defined in the {@link DockerComposeFile} for the active profile
78-
* are running.
79-
* @return {@code true} if services are running
80-
* @see #hasDefinedServices()
81-
* @see #getRunningServices()
82-
*/
83-
boolean hasRunningServices();
84-
8576
/**
8677
* Return the running services for the active profile, or an empty list if no services
8778
* are running.

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,21 @@ void start() {
112112
Start start = this.properties.getStart();
113113
Stop stop = this.properties.getStop();
114114
Wait wait = this.properties.getReadiness().getWait();
115-
if (lifecycleManagement.shouldStart() && !dockerCompose.hasRunningServices()) {
115+
List<RunningService> runningServices = dockerCompose.getRunningServices();
116+
if (lifecycleManagement.shouldStart() && runningServices.isEmpty()) {
116117
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
118+
runningServices = dockerCompose.getRunningServices();
117119
wait = (wait != Wait.ONLY_IF_STARTED) ? wait : Wait.ALWAYS;
118120
if (lifecycleManagement.shouldStop()) {
119121
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
120122
}
121123
}
122-
List<RunningService> runningServices = new ArrayList<>(dockerCompose.getRunningServices());
123-
runningServices.removeIf(this::isIgnored);
124+
List<RunningService> relevantServices = new ArrayList<>(runningServices);
125+
relevantServices.removeIf(this::isIgnored);
124126
if (wait == Wait.ALWAYS || wait == null) {
125-
this.serviceReadinessChecks.waitUntilReady(runningServices);
127+
this.serviceReadinessChecks.waitUntilReady(relevantServices);
126128
}
127-
publishEvent(new DockerComposeServicesReadyEvent(this.applicationContext, runningServices));
129+
publishEvent(new DockerComposeServicesReadyEvent(this.applicationContext, relevantServices));
128130
}
129131

130132
protected DockerComposeFile getComposeFile() {

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeTests.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,6 @@ void hasDefinedServicesWhenComposeConfigServicesIsNotEmptyReturnsTrue() {
9696
assertThat(compose.hasDefinedServices()).isTrue();
9797
}
9898

99-
@Test
100-
void hasRunningServicesWhenPsListsRunningServiceReturnsTrue() {
101-
willReturn(List.of(new DockerCliComposePsResponse("id", "name", "image", "exited"),
102-
new DockerCliComposePsResponse("id", "name", "image", "running")))
103-
.given(this.cli)
104-
.run(new DockerCliCommand.ComposePs());
105-
DefaultDockerCompose compose = new DefaultDockerCompose(this.cli, HOST);
106-
assertThat(compose.hasRunningServices()).isTrue();
107-
}
108-
109-
@Test
110-
void hasRunningServicesWhenPsListReturnsAllExitedReturnsFalse() {
111-
willReturn(List.of(new DockerCliComposePsResponse("id", "name", "image", "exited"),
112-
new DockerCliComposePsResponse("id", "name", "image", "running")))
113-
.given(this.cli)
114-
.run(new DockerCliCommand.ComposePs());
115-
DefaultDockerCompose compose = new DefaultDockerCompose(this.cli, HOST);
116-
assertThat(compose.hasRunningServices()).isTrue();
117-
}
118-
11999
@Test
120100
void getRunningServicesReturnsServices() {
121101
String id = "123";

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
@@ -351,7 +351,6 @@ private void setUpRunningServices(boolean started) {
351351
@SuppressWarnings("unchecked")
352352
private void setUpRunningServices(boolean started, Map<String, String> labels) {
353353
given(this.dockerCompose.hasDefinedServices()).willReturn(true);
354-
given(this.dockerCompose.hasRunningServices()).willReturn(true);
355354
RunningService runningService = mock(RunningService.class);
356355
given(runningService.labels()).willReturn(labels);
357356
this.runningServices = List.of(runningService);

0 commit comments

Comments
 (0)