Skip to content

Commit 168a26c

Browse files
chore(logging): Implement requested changes to improve logging
1 parent e8d86ef commit 168a26c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public DockerService(Config config, StartupScriptsService startupScriptsService)
5858
pullImage();
5959
}
6060
cleanupLeftovers(WORKER_UNIQUE_ID);
61-
executor.submit(() -> initializeCachedContainer(StartupScriptId.EMPTY));
61+
executor.submit(() -> initializeCachedContainer(StartupScriptId.CUSTOM_DEFAULT));
6262
}
6363

6464
private void cleanupLeftovers(UUID currentId) {
@@ -106,6 +106,7 @@ private void pullImage() throws InterruptedException {
106106
* @return The ID of the created container.
107107
*/
108108
private String createContainer(String name) {
109+
LOGGER.debug("Creating container '{}'", name);
109110
HostConfig hostConfig = HostConfig.newHostConfig()
110111
.withAutoRemove(true)
111112
.withInit(true)
@@ -137,10 +138,13 @@ private String createContainer(String name) {
137138
*
138139
* @param name Name of the container.
139140
* @param startupScriptId Script to initialize the container with.
141+
* @throws IOException if an I/O error occurs.
140142
* @return The ContainerState of the newly created container.
141143
*/
142144
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId)
143145
throws IOException {
146+
LOGGER.info("Initializing container '{}' with Startup script ID: {}", name,
147+
startupScriptId);
144148
if (startupScriptId == null || cachedContainers.isEmpty()
145149
|| !cachedContainers.containsKey(startupScriptId)) {
146150
String containerId = createContainer(name);
@@ -159,27 +163,34 @@ public ContainerState initializeContainer(String name, StartupScriptId startupSc
159163
* @param startupScriptId Script to initialize the container with.
160164
*/
161165
private void initializeCachedContainer(StartupScriptId startupScriptId) {
162-
String containerName = cachedContainerName();
166+
LOGGER.info("Initializing cached container with Startup script ID: {}", startupScriptId);
167+
String containerName = newCachedContainerName();
163168
String id = createContainer(containerName);
164169
startContainer(id);
165170

166171
try {
167172
ContainerState containerState = setupContainerWithScript(id, startupScriptId);
168173
cachedContainers.put(startupScriptId, containerState);
169174
} catch (IOException e) {
175+
LOGGER.error("Could not initialize container '{}'", id, e);
170176
killContainerByName(containerName);
171177
throw new RuntimeException(e);
172178
}
173179
}
174180

175181
/**
182+
* Setup container with startup script and also initializes input and output streams for the
183+
* container.
184+
*
176185
* @param containerId The id of the container
177186
* @param startupScriptId The startup script id of the session
178187
* @return ContainerState of the spawned container.
179188
* @throws IOException if an I/O error occurs
180189
*/
181190
private ContainerState setupContainerWithScript(String containerId,
182191
StartupScriptId startupScriptId) throws IOException {
192+
LOGGER.info("Setting up container with id '{}' with Startup script ID: {}", containerId,
193+
startupScriptId);
183194
startContainer(containerId);
184195
PipedInputStream containerInput = new PipedInputStream();
185196
BufferedWriter writer =
@@ -201,9 +212,13 @@ private ContainerState setupContainerWithScript(String containerId,
201212
* @param containerId the ID of the container to start
202213
*/
203214
private void startContainer(String containerId) {
204-
if (!isContainerRunning(containerId)) {
205-
client.startContainerCmd(containerId).exec();
215+
boolean isRunning = isContainerRunning(containerId);
216+
if (isRunning) {
217+
LOGGER.debug("Container '{}' is already running.", containerId);
218+
return;
206219
}
220+
LOGGER.debug("Container '{}' is not running. Starting it now.", containerId);
221+
client.startContainerCmd(containerId).exec();
207222
}
208223

209224
/**
@@ -233,7 +248,7 @@ public void onNext(Frame object) {
233248
String payloadString =
234249
new String(object.getPayload(), StandardCharsets.UTF_8);
235250
if (object.getStreamType() == StreamType.STDOUT) {
236-
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
251+
pipeOut.write(object.getPayload());
237252
} else {
238253
LOGGER.warn("Received STDERR from container {}: {}", containerId,
239254
payloadString);
@@ -257,7 +272,7 @@ public boolean isContainerRunning(String containerId) {
257272
return Boolean.TRUE.equals(containerResponse.getState().getRunning());
258273
}
259274

260-
private String cachedContainerName() {
275+
private String newCachedContainerName() {
261276
return "cached_session_" + UUID.randomUUID();
262277
}
263278

0 commit comments

Comments
 (0)