Skip to content

Commit

Permalink
Health checker imprs
Browse files Browse the repository at this point in the history
  • Loading branch information
pun-ky committed Apr 14, 2021
1 parent 323715d commit 7c65281
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package com.cognifide.gradle.common.health

class HealthCheck(val name: String, val action: () -> Any?) {

private var result: Any? = null

@Suppress("TooGenericExceptionCaught")
fun perform(): HealthStatus = try {
HealthStatus(this, true, action())
HealthStatus(this, true, action().takeIf { it != Unit })
} catch (e: Exception) {
HealthStatus(this, false, e.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class HealthChecker(val common: CommonExtension) {
}

fun noHttp(checkName: String, url: String, criteria: HttpCheck.() -> Unit = {}) = check(checkName) {
var result: Any? = null
common.http {
val check = HttpCheck(url).apply(criteria)
apply(httpOptions)
Expand All @@ -185,20 +186,25 @@ class HealthChecker(val common: CommonExtension) {
}
if (responds) {
throw IllegalStateException("HTTP ${check.method.toUpperCase()} '${check.url}' is available")
} else {
result = "${check.method} ${check.url} -> unavailable"
}
}
result
}

fun host(checkName: String, hostName: String, port: Int, timeout: Int = 1000) = check(checkName) {
if (!NetUtils.isHostReachable(hostName, port, timeout)) {
throw IllegalStateException("Host '$hostName' at port $port is not reachable")
}
"$hostName:$port -> reachable"
}

fun noHost(checkName: String, hostName: String, port: Int, timeout: Int = 1000) = check(checkName) {
if (NetUtils.isHostReachable(hostName, port, timeout)) {
throw IllegalStateException("Host '$hostName' at port $port is reachable")
}
"$hostName:$port -> unreachable"
}

// Default options
Expand Down

0 comments on commit 7c65281

Please sign in to comment.