Skip to content

Commit

Permalink
tilbyr en ekstra readysjekk
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsteinsland committed Nov 7, 2024
1 parent 9cfd90d commit 4437b6b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.EmbeddedServer
import io.ktor.server.engine.EngineConnectorBuilder
import io.ktor.server.engine.applicationEnvironment
import io.ktor.server.engine.connector
import io.ktor.server.metrics.micrometer.MicrometerMetrics
import io.ktor.server.plugins.BadRequestException
import io.ktor.server.plugins.NotFoundException
Expand Down Expand Up @@ -73,21 +74,34 @@ fun naisApp(
timersConfig: Timer.Builder.(ApplicationCall, Throwable?) -> Unit = { _, _ -> },
mdcEntries: Map<String, (ApplicationCall) -> String?> = emptyMap(),
port: Int = 8080,
readyCheck: () -> Boolean = { true },
preStopHook: suspend () -> Unit = { delay(5000) },
cioConfiguration: CIOApplicationEngine.Configuration.() -> Unit = { },
applicationModule: Application.() -> Unit
): EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration> {
val config = serverConfig(
environment = applicationEnvironment {
log = applicationLogger
}
) {
module { standardApiModule(meterRegistry, objectMapper, callLogger, naisEndpoints, callIdHeaderName, preStopHook, timersConfig, mdcEntries) }
module {
standardApiModule(
meterRegistry = meterRegistry,
objectMapper = objectMapper,
callLogger = callLogger,
naisEndpoints = naisEndpoints,
callIdHeaderName = callIdHeaderName,
preStopHook = preStopHook,
readyCheck = readyCheck,
timersConfig = timersConfig,
mdcEntries = mdcEntries
)
}
module(applicationModule)
}
val app = EmbeddedServer(config, CIO) {
connectors.add(EngineConnectorBuilder().apply {
this.port = port
})
connector { this.port = port }
apply(cioConfiguration)
}
return app
}
Expand All @@ -99,6 +113,7 @@ fun Application.standardApiModule(
naisEndpoints: NaisEndpoints,
callIdHeaderName: String,
preStopHook: suspend () -> Unit = { delay(5000) },
readyCheck: () -> Boolean = { true },
timersConfig: Timer.Builder.(ApplicationCall, Throwable?) -> Unit = { _, _ -> },
mdcEntries: Map<String, (ApplicationCall) -> String?> = emptyMap(),
) {
Expand Down Expand Up @@ -192,7 +207,7 @@ fun Application.standardApiModule(
}

get(naisEndpoints.isreadyEndpoint) {
if (!readyToggle.get()) return@get call.respondText("NOT READY", ContentType.Text.Plain, HttpStatusCode.ServiceUnavailable)
if (!readyToggle.get() || !readyCheck()) return@get call.respondText("NOT READY", ContentType.Text.Plain, HttpStatusCode.ServiceUnavailable)
call.respondText("READY", ContentType.Text.Plain)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory
import java.net.ServerSocket
import java.net.URI
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean

class NaisfulAppTest {

Expand All @@ -41,6 +42,16 @@ class NaisfulAppTest {
}
}

@Test
fun `ready check`() {
val ready = AtomicBoolean(true)
testApp(readyCheck = ready::get) {
assertEquals("READY", get("/isready").bodyAsText())
ready.set(false)
assertEquals("NOT READY", get("/isready").bodyAsText())
}
}

@Test
fun `custom routes`() {
val endpoints = NaisEndpoints(
Expand Down Expand Up @@ -106,7 +117,12 @@ class NaisfulAppTest {
}
}

private fun testApp(naisEndpoints: NaisEndpoints = NaisEndpoints.Default, applicationModule: Application.() -> Unit = {}, testBlock: suspend HttpClient.() -> Unit) {
private fun testApp(
naisEndpoints: NaisEndpoints = NaisEndpoints.Default,
readyCheck: () -> Boolean = { true },
applicationModule: Application.() -> Unit = {},
testBlock: suspend HttpClient.() -> Unit
) {
val meterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
val objectMapper = jacksonObjectMapper()
val randomPort = ServerSocket(0).localPort
Expand All @@ -122,9 +138,15 @@ class NaisfulAppTest {
}
}
application {
standardApiModule(meterRegistry, objectMapper, environment.log, naisEndpoints, "callId", {
delay(250)
})
standardApiModule(
meterRegistry = meterRegistry,
objectMapper = objectMapper,
callLogger = environment.log,
naisEndpoints = naisEndpoints,
callIdHeaderName = "callId",
preStopHook = { delay(250) },
readyCheck = readyCheck
)
applicationModule()
}
startApplication()
Expand Down

0 comments on commit 4437b6b

Please sign in to comment.