Merge pull request #95 from p2p-org/small-health-endpoint-fixes

This commit is contained in:
a10zn8
2022-12-19 21:59:04 +04:00
committed by GitHub
2 changed files with 19 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.stereotype.Service
import java.io.IOException import java.io.IOException
import java.net.InetSocketAddress import java.net.InetSocketAddress
import javax.annotation.PostConstruct import javax.annotation.PostConstruct
import javax.annotation.PreDestroy
@Service @Service
class HealthCheckSetup( class HealthCheckSetup(
@@ -37,6 +38,8 @@ class HealthCheckSetup(
private val log = LoggerFactory.getLogger(HealthCheckSetup::class.java) private val log = LoggerFactory.getLogger(HealthCheckSetup::class.java)
} }
lateinit var server: HttpServer
@PostConstruct @PostConstruct
fun start() { fun start() {
if (!healthConfig.isEnabled()) { if (!healthConfig.isEnabled()) {
@@ -46,7 +49,7 @@ class HealthCheckSetup(
// health check is a rare operation, no reason to set up anything complex // health check is a rare operation, no reason to set up anything complex
try { try {
log.info("Run Health Server on ${healthConfig.host}:${healthConfig.port}${healthConfig.path}") log.info("Run Health Server on ${healthConfig.host}:${healthConfig.port}${healthConfig.path}")
val server = HttpServer.create( server = HttpServer.create(
InetSocketAddress( InetSocketAddress(
healthConfig.host, healthConfig.host,
healthConfig.port healthConfig.port
@@ -76,7 +79,7 @@ class HealthCheckSetup(
fun getHealth(): Detailed { fun getHealth(): Detailed {
val errors = healthConfig.configs().mapNotNull { val errors = healthConfig.configs().mapNotNull {
val up = multistreamHolder.getUpstream(it.blockchain) val up = multistreamHolder.getUpstream(it.blockchain)
if (up == null || !up.isAvailable()) { if (!up.isAvailable()) {
return@mapNotNull "${it.blockchain} UNAVAILABLE" return@mapNotNull "${it.blockchain} UNAVAILABLE"
} }
val avail = up.getAll().count { it.getStatus() == UpstreamAvailability.OK } val avail = up.getAll().count { it.getStatus() == UpstreamAvailability.OK }
@@ -100,7 +103,7 @@ class HealthCheckSetup(
var chainUnavailable = false var chainUnavailable = false
val up = multistreamHolder.getUpstream(chain) val up = multistreamHolder.getUpstream(chain)
val required = healthConfig.chains[chain] val required = healthConfig.chains[chain]
if (up == null || !up.isAvailable()) { if (!up.isAvailable()) {
if (required != null) { if (required != null) {
anyUnavailable = true 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( data class Detailed(
val ok: Boolean, val ok: Boolean,
val details: List<String> val details: List<String>

View File

@@ -49,7 +49,11 @@ open class CurrentMultistreamHolder(
fun shutdown() { fun shutdown() {
log.info("Closing upstream connections...") log.info("Closing upstream connections...")
chainMapping.values.forEach { chainMapping.values.forEach {
it.stop() try {
it.stop()
} catch (e: Exception) {
log.trace("Error during multistream shutdown", e)
}
} }
} }
} }