Skip to content

Commit a44e0c9

Browse files
committed
Looking for issues with buffering.
1 parent 5409695 commit a44e0c9

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

src/main/kotlin/docker/DockerProxy.kt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,30 @@ abstract class DockerProxy(configuration: Configuration) : MigrationStrategy(con
9393
private fun listenForDockerStatistics() {
9494
println("Started a docker stats process")
9595
val numberOfElements = (configuration.slotDuration / 1000).toInt()
96-
val process = ProcessBuilder()
97-
.command("docker", "stats", "--no-trunc", "--format", "{{ json . }}")
98-
.redirectErrorStream(true)
99-
.start()
100-
val reader = process.inputStream.bufferedReader()
96+
10197
while (true) {
102-
val line = reader.readLine()
103-
println("Line read: $line")
104-
val stats = Json.decodeFromString<DockerStatsModel>(line)
105-
val pids = stats.pids.toIntOrNull() ?: 0
106-
val cpuPercentage = stats.cpuPercentage.replace("%", "").toDoubleOrNull() ?: 0.0
107-
val memoryPercentage = stats.memoryPercentage.replace("%", "").toDoubleOrNull() ?: 0.0
108-
109-
val container = localContainers.computeIfAbsent(stats.id) {
110-
DockerContainer(stats.id, pids, CircularList(numberOfElements), CircularList(numberOfElements))
111-
}
112-
container.apply {
113-
cpuUsage.add(cpuPercentage)
114-
memoryUsage.add(memoryPercentage)
115-
processes = pids
116-
updated = System.currentTimeMillis()
98+
// TODO: I believe this could be optimised
99+
val process = ProcessBuilder()
100+
.command("docker", "stats", "--no-trunc", "--format", "{{ json . }}")
101+
.redirectErrorStream(true)
102+
.start()
103+
val reader = process.inputStream.bufferedReader()
104+
105+
reader.readLines().forEach { line ->
106+
val stats = Json.decodeFromString<DockerStatsModel>(line)
107+
val pids = stats.pids.toIntOrNull() ?: 0
108+
val cpuPercentage = stats.cpuPercentage.replace("%", "").toDoubleOrNull() ?: 0.0
109+
val memoryPercentage = stats.memoryPercentage.replace("%", "").toDoubleOrNull() ?: 0.0
110+
111+
val container = localContainers.computeIfAbsent(stats.id) {
112+
DockerContainer(stats.id, pids, CircularList(numberOfElements), CircularList(numberOfElements))
113+
}
114+
container.apply {
115+
cpuUsage.add(cpuPercentage)
116+
memoryUsage.add(memoryPercentage)
117+
processes = pids
118+
updated = System.currentTimeMillis()
119+
}
117120
}
118121
}
119122

0 commit comments

Comments
 (0)