Sat, 22 Jul 2023 11:32:27 +0200
make vcs command timeout configurable
relates to #274
--- a/src/main/kotlin/de/uapcore/lightpit/vcs/HgConnector.kt Tue Jul 18 18:05:49 2023 +0200 +++ b/src/main/kotlin/de/uapcore/lightpit/vcs/HgConnector.kt Sat Jul 22 11:32:27 2023 +0200 @@ -27,7 +27,6 @@ package de.uapcore.lightpit.vcs import java.nio.file.Files -import java.nio.file.Path import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.deleteRecursively @@ -42,7 +41,7 @@ * Checks, if the specified binary is available and executable. */ fun checkAvailability(): Boolean { - return when (val versionInfo = invokeCommand(Path.of("."), "--version")) { + return when (val versionInfo = invokeCommand("--version")) { is VcsConnectorResult.Success -> versionInfo.content.contains("Mercurial") else -> false } @@ -61,13 +60,15 @@ } catch (e: Throwable) { return VcsConnectorResult.Error("Creating temporary directory for VCS connection failed: " + e.message) } - val init = invokeCommand(tmpDir, "init") + val init = invokeCommand("init", workingDir = tmpDir) if (init is VcsConnectorResult.Error) { return init } val commitLogContent = when (val result = invokeCommand( - tmpDir, "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n" + "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n", + workingDir = tmpDir, + timeout = 60 )) { is VcsConnectorResult.Error -> return result is VcsConnectorResult.Success -> result.content
--- a/src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.kt Tue Jul 18 18:05:49 2023 +0200 +++ b/src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.kt Sat Jul 22 11:32:27 2023 +0200 @@ -7,13 +7,17 @@ /** * Invokes the VCS binary with the given [args] and returns the output on stdout. */ - protected fun invokeCommand(workingDir: Path, vararg args : String): VcsConnectorResult<String> { + protected fun invokeCommand( + vararg args: String, + workingDir: Path = Path.of("."), + timeout: Long = 30L + ): VcsConnectorResult<String> { return try { val command = mutableListOf(path) command.addAll(args) val process = ProcessBuilder(command).directory(workingDir.toFile()).start() val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8) - if (process.waitFor(30, TimeUnit.SECONDS)) { + if (process.waitFor(timeout, TimeUnit.SECONDS)) { if (process.exitValue() == 0) { VcsConnectorResult.Success(stdout) } else {