From ed7d29eb07ae9544954fc9cb84a8417a80f600fe Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Wed, 29 Jun 2022 22:42:54 +0200 Subject: [PATCH] [grid] Saving Selenium logs in Dynamic Grid Fixes https://github.com/SeleniumHQ/docker-selenium/issues/1575 --- .../grid/node/docker/DockerSession.java | 23 ++++++++++++++++++- .../node/docker/DockerSessionFactory.java | 3 ++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSession.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSession.java index 70615c5adf6f8..758c57deb87d9 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSession.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSession.java @@ -27,13 +27,20 @@ import org.openqa.selenium.remote.tracing.Tracer; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Duration; import java.time.Instant; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; public class DockerSession extends ProtocolConvertingSession { + private static final Logger LOG = Logger.getLogger(DockerSession.class.getName()); private final Container container; private final Container videoContainer; + private final DockerAssetsPath assetsPath; DockerSession( Container container, @@ -46,10 +53,12 @@ public class DockerSession extends ProtocolConvertingSession { Capabilities capabilities, Dialect downstream, Dialect upstream, - Instant startTime) { + Instant startTime, + DockerAssetsPath assetsPath) { super(tracer, client, id, url, downstream, upstream, stereotype, capabilities, startTime); this.container = Require.nonNull("Container", container); this.videoContainer = videoContainer; + this.assetsPath = Require.nonNull("Assets path", assetsPath); } @Override @@ -57,6 +66,18 @@ public void stop() { if (videoContainer != null) { videoContainer.stop(Duration.ofSeconds(10)); } + saveLogs(); container.stop(Duration.ofMinutes(1)); } + + private void saveLogs() { + String sessionAssetsPath = assetsPath.getContainerPath(getId()); + String seleniumServerLog = String.format("%s/selenium-server.log", sessionAssetsPath); + try { + List logs = container.getLogs().getLogLines(); + Files.write(Paths.get(seleniumServerLog), logs); + } catch (Exception e) { + LOG.log(Level.WARNING, "Error saving logs", e); + } + } } diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java index d55dc18d6bf17..3eaad63d8ea50 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java @@ -266,7 +266,8 @@ public Either apply(CreateSessionRequest sess mergedCapabilities, downstream, result.getDialect(), - Instant.now())); + Instant.now(), + assetsPath)); } }