Skip to content

Commit

Permalink
Adding time elapsed to end-to-end services start and stop
Browse files Browse the repository at this point in the history
logs

GitOrigin-RevId: 0af686c9f2731ab0d805afdc131454e311244012
  • Loading branch information
ericloe-cash authored and svc-squareup-copybara committed Sep 27, 2024
1 parent e5ff29d commit d76ef2a
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions misk/src/main/kotlin/misk/MiskApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import misk.inject.getInstance
import misk.web.jetty.JettyHealthService
import misk.web.jetty.JettyService
import wisp.logging.getLogger
import kotlin.concurrent.thread
import kotlin.system.measureTimeMillis
import kotlin.time.Duration.Companion.milliseconds

/** The entry point for misk applications */
class MiskApplication private constructor(
Expand Down Expand Up @@ -104,8 +107,8 @@ class MiskApplication private constructor(
log.info { "creating application injector" }
val injector = injectorGenerator()
val serviceManager = injector.getInstance<ServiceManager>()
shutdownHook = object : Thread() {
override fun run() {
shutdownHook = thread(start = false) {
measureTimeMillis {
log.info { "received a shutdown hook! performing an orderly shutdown" }
serviceManager.stopAsync()
serviceManager.awaitStopped()
Expand All @@ -122,23 +125,28 @@ class MiskApplication private constructor(
val jettyHealthService = injector.getInstance<JettyHealthService>()
jettyHealthService.stopAsync()
jettyHealthService.awaitTerminated()

log.info { "orderly shutdown complete" }
}.also {
log.info { "orderly shutdown complete in ${it.milliseconds}" }
}
}

Runtime.getRuntime().addShutdownHook(shutdownHook)

// We manage JettyHealthService outside ServiceManager because it must start first and
// shutdown last to keep the container alive via liveness checks.
log.info { "starting services" }
val jettyHealthService = injector.getInstance<JettyHealthService>()
jettyHealthService.startAsync()
serviceManager.startAsync()
serviceManager.awaitHealthy()
jettyHealthService.awaitRunning()

log.info { "all services started successfully" }

val jettyHealthService: JettyHealthService
measureTimeMillis {
log.info { "starting services" }
jettyHealthService = injector.getInstance<JettyHealthService>()
jettyHealthService.startAsync()
serviceManager.startAsync()
serviceManager.awaitHealthy()
jettyHealthService.awaitRunning()
}.also {
log.info { "all services started successfully in ${it.milliseconds}" }
}

serviceManager.awaitStopped()
jettyHealthService.awaitTerminated()
log.info { "all services stopped" }
Expand Down

0 comments on commit d76ef2a

Please sign in to comment.