Skip to content

Commit e40b62b

Browse files
chore: address requested changes and enhance logging
1 parent e8d86ef commit e40b62b

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

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

+24-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313
import org.springframework.beans.factory.DisposableBean;
14+
import org.springframework.lang.Nullable;
1415
import org.springframework.stereotype.Service;
1516

1617
import org.togetherjava.jshellapi.Config;
@@ -58,7 +59,7 @@ public DockerService(Config config, StartupScriptsService startupScriptsService)
5859
pullImage();
5960
}
6061
cleanupLeftovers(WORKER_UNIQUE_ID);
61-
executor.submit(() -> initializeCachedContainer(StartupScriptId.EMPTY));
62+
executor.submit(() -> initializeCachedContainer(StartupScriptId.CUSTOM_DEFAULT));
6263
}
6364

6465
private void cleanupLeftovers(UUID currentId) {
@@ -106,6 +107,7 @@ private void pullImage() throws InterruptedException {
106107
* @return The ID of the created container.
107108
*/
108109
private String createContainer(String name) {
110+
LOGGER.debug("Creating container '{}'", name);
109111
HostConfig hostConfig = HostConfig.newHostConfig()
110112
.withAutoRemove(true)
111113
.withInit(true)
@@ -137,10 +139,13 @@ private String createContainer(String name) {
137139
*
138140
* @param name Name of the container.
139141
* @param startupScriptId Script to initialize the container with.
142+
* @throws IOException if an I/O error occurs.
140143
* @return The ContainerState of the newly created container.
141144
*/
142-
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId)
143-
throws IOException {
145+
public ContainerState initializeContainer(String name,
146+
@Nullable StartupScriptId startupScriptId) throws IOException {
147+
LOGGER.info("Initializing container '{}' with Startup script ID: {}", name,
148+
startupScriptId);
144149
if (startupScriptId == null || cachedContainers.isEmpty()
145150
|| !cachedContainers.containsKey(startupScriptId)) {
146151
String containerId = createContainer(name);
@@ -159,27 +164,34 @@ public ContainerState initializeContainer(String name, StartupScriptId startupSc
159164
* @param startupScriptId Script to initialize the container with.
160165
*/
161166
private void initializeCachedContainer(StartupScriptId startupScriptId) {
162-
String containerName = cachedContainerName();
167+
LOGGER.info("Initializing cached container with Startup script ID: {}", startupScriptId);
168+
String containerName = newCachedContainerName();
163169
String id = createContainer(containerName);
164170
startContainer(id);
165171

166172
try {
167173
ContainerState containerState = setupContainerWithScript(id, startupScriptId);
168174
cachedContainers.put(startupScriptId, containerState);
169175
} catch (IOException e) {
176+
LOGGER.error("Could not initialize container {}", id, e);
170177
killContainerByName(containerName);
171178
throw new RuntimeException(e);
172179
}
173180
}
174181

175182
/**
183+
* Setup container with startup script and also initializes input and output streams for the
184+
* container.
185+
*
176186
* @param containerId The id of the container
177187
* @param startupScriptId The startup script id of the session
178188
* @return ContainerState of the spawned container.
179189
* @throws IOException if an I/O error occurs
180190
*/
181191
private ContainerState setupContainerWithScript(String containerId,
182192
StartupScriptId startupScriptId) throws IOException {
193+
LOGGER.info("Setting up container with id {} with Startup script ID: {}", containerId,
194+
startupScriptId);
183195
startContainer(containerId);
184196
PipedInputStream containerInput = new PipedInputStream();
185197
BufferedWriter writer =
@@ -201,9 +213,13 @@ private ContainerState setupContainerWithScript(String containerId,
201213
* @param containerId the ID of the container to start
202214
*/
203215
private void startContainer(String containerId) {
204-
if (!isContainerRunning(containerId)) {
205-
client.startContainerCmd(containerId).exec();
216+
boolean isRunning = isContainerRunning(containerId);
217+
if (isRunning) {
218+
LOGGER.debug("Container {} is already running.", containerId);
219+
return;
206220
}
221+
LOGGER.debug("Container {} is not running. Starting it now.", containerId);
222+
client.startContainerCmd(containerId).exec();
207223
}
208224

209225
/**
@@ -233,7 +249,7 @@ public void onNext(Frame object) {
233249
String payloadString =
234250
new String(object.getPayload(), StandardCharsets.UTF_8);
235251
if (object.getStreamType() == StreamType.STDOUT) {
236-
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
252+
pipeOut.write(object.getPayload());
237253
} else {
238254
LOGGER.warn("Received STDERR from container {}: {}", containerId,
239255
payloadString);
@@ -257,7 +273,7 @@ public boolean isContainerRunning(String containerId) {
257273
return Boolean.TRUE.equals(containerResponse.getState().getRunning());
258274
}
259275

260-
private String cachedContainerName() {
276+
private String newCachedContainerName() {
261277
return "cached_session_" + UUID.randomUUID();
262278
}
263279

0 commit comments

Comments
 (0)