Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/main/kotlin/com/company/commitet_jm/component/ShellExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import java.io.IOException
import java.util.concurrent.TimeUnit

@Component
class ShellExecutor(var workingDir: File = File("."), var timeout:Long = 1) {
class ShellExecutor(var workingDir: File = File("."), var timeout:Long = 5) {

companion object {
private val log = LoggerFactory.getLogger(ShellExecutor::class.java)
}

fun executeCommand(command: List<String?>): String {
data class CommandResult(val exitCode: Int, val output: String, val error: String)

fun executeCommandWithResult(command: List<String?>, customTimeout: Long = timeout): CommandResult {
try {
val process = ProcessBuilder(command.filterNotNull())
.directory(workingDir)
Expand All @@ -22,23 +24,24 @@ class ShellExecutor(var workingDir: File = File("."), var timeout:Long = 1) {
.start()

// Используем ограничение по времени для предотвращения блокировки
val finished = process.waitFor(timeout, TimeUnit.MINUTES)
val finished = process.waitFor(customTimeout, TimeUnit.MINUTES)

if (!finished) {
process.destroyForcibly()
throw RuntimeException("Command timed out after $timeout minutes")
throw RuntimeException("Command timed out after $customTimeout minutes")
}

val output = process.inputStream.bufferedReader().use { it.readText() }
val error = process.errorStream.bufferedReader().use { it.readText() }
val exitCode = process.exitValue()

if (process.exitValue() != 0) {
log.error("Command failed: ${command.filterNotNull().joinToString(" ")}\nError: $error")
throw RuntimeException("exec command failed: $error")
if (exitCode != 0) {
log.error("Command failed with exit code $exitCode: ${command.filterNotNull().joinToString(" ")}\nError: $error")
} else {
log.info("Command executed successfully: ${command.filterNotNull().joinToString(" ")}\nOutput: $output")
}

log.info("Command executed: ${command.filterNotNull().joinToString(" ")}\nOutput: $output")
return output
return CommandResult(exitCode, output, error)
} catch (e: IOException) {
log.error("IO error executing command: ${e.message}")
throw e
Expand All @@ -48,4 +51,12 @@ class ShellExecutor(var workingDir: File = File("."), var timeout:Long = 1) {
throw RuntimeException("Operation interrupted")
}
}

fun executeCommand(command: List<String?>, customTimeout: Long = timeout): String {
val result = executeCommandWithResult(command, customTimeout)
if (result.exitCode != 0) {
throw RuntimeException("exec command failed: ${result.error}")
}
return result.output
}
}
Loading
Loading