Skip to content

Commit b3e0a9a

Browse files
committed
Kill containers on program exit
1 parent 85886e5 commit b3e0a9a

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.github.dockerjava.core.DefaultDockerClientConfig;
88
import com.github.dockerjava.core.DockerClientImpl;
99
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.DisposableBean;
1013
import org.springframework.stereotype.Service;
1114

1215
import java.io.*;
@@ -19,7 +22,8 @@
1922
import java.util.concurrent.TimeUnit;
2023

2124
@Service
22-
public class DockerService implements AutoCloseable {
25+
public class DockerService implements DisposableBean {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(DockerService.class);
2327

2428
private static final String WORKER_LABEL = "jshell-api-worker";
2529
private static final UUID WORKER_UNIQUE_ID = UUID.randomUUID();
@@ -36,15 +40,15 @@ public DockerService() {
3640
.build();
3741
this.client = DockerClientImpl.getInstance(clientConfig, httpClient);
3842

39-
cleanupLeftovers();
43+
cleanupLeftovers(WORKER_UNIQUE_ID);
4044
}
4145

42-
private void cleanupLeftovers() {
46+
private void cleanupLeftovers(UUID currentId) {
4347
for (Container container : client.listContainersCmd().withLabelFilter(Set.of(WORKER_LABEL)).exec()) {
4448
String containerHumanName = container.getId() + " " + Arrays.toString(container.getNames());
45-
System.out.println("Found worker container " + containerHumanName);
46-
if (!container.getLabels().get(WORKER_LABEL).equals(WORKER_UNIQUE_ID.toString())) {
47-
System.out.println("Killing container " + containerHumanName);
49+
LOGGER.info("Found worker container '{}'", containerHumanName);
50+
if (!container.getLabels().get(WORKER_LABEL).equals(currentId.toString())) {
51+
LOGGER.info("Killing container '{}'", containerHumanName);
4852
client.killContainerCmd(container.getId()).exec();
4953
}
5054
}
@@ -110,7 +114,11 @@ public void onNext(Frame object) {
110114
if (object.getStreamType() == StreamType.STDOUT) {
111115
pipeOut.write(object.getPayload());
112116
} else {
113-
System.err.println(":( " + payloadString);
117+
LOGGER.warn(
118+
"Received STDERR from container {}: {}",
119+
containerId,
120+
payloadString
121+
);
114122
}
115123
} catch (IOException e) {
116124
throw new UncheckedIOException(e);
@@ -128,12 +136,15 @@ public void killContainerByName(String name) {
128136
}
129137
}
130138

139+
public boolean isDead(String containerName) {
140+
return client.listContainersCmd().withNameFilter(Set.of(containerName)).exec().isEmpty();
141+
}
142+
131143
@Override
132-
public void close() throws Exception {
144+
public void destroy() throws Exception {
145+
LOGGER.info("destroy() called. Destroying all containers...");
146+
cleanupLeftovers(UUID.randomUUID());
133147
client.close();
134148
}
135149

136-
public boolean isDead(String containerName) {
137-
return client.listContainersCmd().withNameFilter(Set.of(containerName)).exec().isEmpty();
138-
}
139150
}

0 commit comments

Comments
 (0)