All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.cloudnative.health.ReadinessCheckResult.kt Maven / Gradle / Ivy

There is a newer version: 5.31.1.0
Show newest version
package org.http4k.cloudnative.health

/**
 * A Readiness check is used to determine if the pod is ready to receive traffic. An example is to test
 * if the app can talk to it's database.
 */
interface ReadinessCheck : () -> ReadinessCheckResult {
    val name: String
}

/**
 * The result of a Readiness check. Checks can be combined together with `+()` to provide an overall result.
 */
sealed class ReadinessCheckResult(val pass: Boolean = true) : Iterable {
    abstract val name: String
    override fun iterator() = emptyList().iterator()
}

data class Completed(override val name: String) : ReadinessCheckResult(true)

data class Failed(override val name: String, val cause: Exception) : ReadinessCheckResult(false) {
    constructor(name: String, message: String) : this(name, Exception(message))
}

/**
 * Result of multiple checks, for which it reports an overall result (ie. any failure is fatal).
 */
data class Composite(internal val parts: Iterable = emptyList()) : ReadinessCheckResult(parts.fold(true) { acc, next -> acc && next.pass }) {
    override val name = "overall"
    override fun iterator() = parts.iterator()
}

operator fun ReadinessCheckResult.plus(that: ReadinessCheckResult): Composite = when (this) {
    is Composite -> Composite(parts = parts + listOf(that))
    else -> Composite(listOf(this, that))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy