diff --git a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/HealthCheckSetup.kt b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/HealthCheckSetup.kt index 01432fc4..c772bad8 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/HealthCheckSetup.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/HealthCheckSetup.kt @@ -26,6 +26,7 @@ import org.springframework.stereotype.Service import java.io.IOException import java.net.InetSocketAddress import javax.annotation.PostConstruct +import javax.annotation.PreDestroy @Service class HealthCheckSetup( @@ -37,6 +38,8 @@ class HealthCheckSetup( private val log = LoggerFactory.getLogger(HealthCheckSetup::class.java) } + lateinit var server: HttpServer + @PostConstruct fun start() { if (!healthConfig.isEnabled()) { @@ -46,7 +49,7 @@ class HealthCheckSetup( // health check is a rare operation, no reason to set up anything complex try { log.info("Run Health Server on ${healthConfig.host}:${healthConfig.port}${healthConfig.path}") - val server = HttpServer.create( + server = HttpServer.create( InetSocketAddress( healthConfig.host, healthConfig.port @@ -76,7 +79,7 @@ class HealthCheckSetup( fun getHealth(): Detailed { val errors = healthConfig.configs().mapNotNull { val up = multistreamHolder.getUpstream(it.blockchain) - if (up == null || !up.isAvailable()) { + if (!up.isAvailable()) { return@mapNotNull "${it.blockchain} UNAVAILABLE" } val avail = up.getAll().count { it.getStatus() == UpstreamAvailability.OK } @@ -100,7 +103,7 @@ class HealthCheckSetup( var chainUnavailable = false val up = multistreamHolder.getUpstream(chain) val required = healthConfig.chains[chain] - if (up == null || !up.isAvailable()) { + if (!up.isAvailable()) { if (required != null) { anyUnavailable = true } @@ -135,6 +138,14 @@ class HealthCheckSetup( ) } + @PreDestroy + fun shutdown() { + if (::server.isInitialized) { + log.info("Shutting down health Server...") + server.stop(0) + } + } + data class Detailed( val ok: Boolean, val details: List diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt index 165c20fe..2fe79ecc 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/CurrentMultistreamHolder.kt @@ -49,7 +49,11 @@ open class CurrentMultistreamHolder( fun shutdown() { log.info("Closing upstream connections...") chainMapping.values.forEach { - it.stop() + try { + it.stop() + } catch (e: Exception) { + log.trace("Error during multistream shutdown", e) + } } } }