Skip to content

Commit

Permalink
Updates to print thread dump only if appConfig enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vega113 committed Nov 18, 2023
1 parent 793fcd5 commit 2351296
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/ShutdownHook.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import org.slf4j.Logger
import play.api.Configuration

import scala.concurrent.Future
import play.api.inject.ApplicationLifecycle

import javax.inject._

class ShutdownHook @Inject()(lifecycle: ApplicationLifecycle) {
class ShutdownHook @Inject()(lifecycle: ApplicationLifecycle, config: Configuration) {
val logger: Logger = org.slf4j.LoggerFactory.getLogger("ShutdownHook")

lifecycle.addStopHook { () =>
logger.info("ShutdownHook is running")
private def threadDump(): Unit = {
val threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean
val threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds, 100)
val stackTrace = threadInfos.map(threadInfo => {
Expand All @@ -19,6 +19,13 @@ class ShutdownHook @Inject()(lifecycle: ApplicationLifecycle) {
"""
}).mkString("\n")
logger.info(s"Thread dump:\n$stackTrace")
}

lifecycle.addStopHook { () =>
logger.info("ShutdownHook is running")
if (config.get[Boolean]("emo.threadDumpOnShutdown")) {
threadDump()
}
Future.successful(())
}
}
2 changes: 2 additions & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ play {
emo.config {
loginTokenExpirationTime = 365 days
loginTokenExpirationTime = ${?LOGIN_TOKEN_EXPIRATION_TIME}
threadDumpOnShutdown = false
threadDumpOnShutdown = ${?THREAD_DUMP_ON_SHUTDOWN}
}

database.dispatcher {
Expand Down

0 comments on commit 2351296

Please sign in to comment.