Skip to content

Commit 9eb71d8

Browse files
committed
Added monitoring thread for docker container
1 parent fea8788 commit 9eb71d8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/integration/host/src/test/java/integration/util/ContainerHelper.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,69 @@ protected Long execInContainer(
280280
}
281281

282282
final ExecCreateCmdResponse execCreateCmdResponse = cmd.exec();
283+
284+
// Start monitoring thread
285+
final java.util.concurrent.atomic.AtomicBoolean executionComplete = new java.util.concurrent.atomic.AtomicBoolean(false);
286+
Thread monitorThread = new Thread(() -> {
287+
int checkCount = 0;
288+
while (!executionComplete.get()) {
289+
try {
290+
Thread.sleep(30000); // Check every 30 seconds
291+
InspectContainerResponse status = dockerClient.inspectContainerCmd(containerId).exec();
292+
checkCount++;
293+
294+
System.out.println(String.format(
295+
"[Monitor %d] Container status: running=%s, status=%s",
296+
checkCount,
297+
status.getState().getRunning(),
298+
status.getState().getStatus()
299+
));
300+
301+
if (!status.getState().getRunning()) {
302+
System.out.println("⚠️ Container stopped running!");
303+
System.out.println("Exit code: " + status.getState().getExitCodeLong());
304+
System.out.println("Finished at: " + status.getState().getFinishedAt());
305+
if (status.getState().getError() != null) {
306+
System.out.println("Error: " + status.getState().getError());
307+
}
308+
309+
// Pull container logs when it crashes
310+
System.out.println("=== CONTAINER LOGS (last 50 lines) ===");
311+
try {
312+
dockerClient.logContainerCmd(containerId)
313+
.withStdOut(true)
314+
.withStdErr(true)
315+
.withTail(50)
316+
.exec(new com.github.dockerjava.api.async.ResultCallback.Adapter<com.github.dockerjava.api.model.Frame>() {
317+
@Override
318+
public void onNext(com.github.dockerjava.api.model.Frame frame) {
319+
System.out.print(new String(frame.getPayload()));
320+
}
321+
}).awaitCompletion();
322+
} catch (Exception logEx) {
323+
System.out.println("Failed to get container logs: " + logEx.getMessage());
324+
}
325+
System.out.println("=== END CONTAINER LOGS ===");
326+
break;
327+
}
328+
} catch (InterruptedException e) {
329+
Thread.currentThread().interrupt();
330+
break;
331+
} catch (Exception e) {
332+
System.out.println("Monitor error: " + e.getMessage());
333+
}
334+
}
335+
});
336+
monitorThread.setDaemon(true);
337+
monitorThread.start();
338+
283339
try (final FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
284340
callback.addConsumer(OutputFrame.OutputType.STDOUT, consumer);
285341
callback.addConsumer(OutputFrame.OutputType.STDERR, consumer);
286342
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(callback).awaitCompletion();
343+
} finally {
344+
executionComplete.set(true);
345+
monitorThread.interrupt();
287346
}
288347

289348
return dockerClient.inspectExecCmd(execCreateCmdResponse.getId()).exec().getExitCodeLong();

0 commit comments

Comments
 (0)