Skip to content

Commit

Permalink
Control max heap dumps via env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Кирилл committed Jul 19, 2024
1 parent 4911c0c commit 6d94238
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/kotlin/io/emeraldpay/dshackle/HeapDumpCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import java.util.concurrent.TimeUnit

class HeapDumpCreator {
companion object {
private const val MAX_DUMPS = 3

private val log = LoggerFactory.getLogger(HeapDumpCreator::class.java)
private val executorService = Executors.newSingleThreadScheduledExecutor()

fun init() {
val envVars = System.getenv()
val enableCreateDumps = envVars["HEAP_DUMP_ENABLE"]?.toBoolean() ?: true
val dumpsPath = envVars["HEAP_DUMP_PATH"] ?: "/etc/dshackle/dumps"
val maxDumps = envVars["MAX_HEAP_DUMPS"]?.toInt() ?: 3
val dumpsPathExists = heapDumpsPathExists(dumpsPath)
.also {
if (!it) {
Expand All @@ -33,7 +32,7 @@ class HeapDumpCreator {
}

if (enableCreateDumps && dumpsPathExists) {
executorService.scheduleAtFixedRate({ dumpCheckTask(dumpsPath) }, 0, 30, TimeUnit.MINUTES)
executorService.scheduleAtFixedRate({ dumpCheckTask(dumpsPath, maxDumps) }, 0, 30, TimeUnit.MINUTES)

val appName = envVars["DSHACKLE_APP_NAME"] ?: "dshackle"

Expand Down Expand Up @@ -64,15 +63,15 @@ class HeapDumpCreator {
}
}

private fun dumpCheckTask(dumpsPath: String) {
private fun dumpCheckTask(dumpsPath: String, maxDumps: Int) {
val files = File(dumpsPath).listFiles()
?.filter { it.name.endsWith("hprof") }
?: emptyList()

if (files.isNotEmpty()) {
log.warn("There are some heap dumps, please send them to dshackle developers")
}
if (files.size >= MAX_DUMPS) {
if (files.size >= maxDumps) {
files.minBy {
Files.readAttributes(Paths.get(it.absolutePath), BasicFileAttributes::class.java).creationTime()
}.run {
Expand Down

0 comments on commit 6d94238

Please sign in to comment.