Nikky
02/27/2022, 12:22 PMserver.start(wait = true)
will just block my application completely when i call server.stop(500, 500)
not sure why.. could it be that joining the jobs is blocking ?
for now i am using this..
server = embeddedServer(...)
server.start(wait = false)
while (true) {
sleep(1)
}
and in the signal handler i call
server.stop(500, 500)
if anybody knows a cleaner solution.. would like to make this work better
i also noticed.. if i use delay
instead of sleep it will also continue to block..?hfhbd
02/27/2022, 12:32 PMcoroutineScope {
val server = embeddedServer(..)
server.start(wait = false)
action()
server.stop()
}
suspend fun action() = suspendCoroutine { cont ->
registerSignalHandler(callback = {
cont.resume()
})
}
Nikky
02/27/2022, 2:57 PMsuspendCoroutine
until the signal handler triggers the continuation ?hfhbd
02/27/2022, 3:30 PMephemient
02/27/2022, 5:13 PMval stopSignal = Semaphore(1, 1)
registerSignalHandler { stopSignal.release() }
coroutineScope {
...
stopSignal.acquire()
}
note that in both solutions, your program will crash if the signal handler is invoked multiple times. you can use a CompletableDeferred
instead if that is a concernNikky
02/27/2022, 5:44 PMval completableJob = SupervisorJob()
SignalHandler.onSignal(SIGINT) { _, signalName ->
logger.warn { "received signal $signalName, stopping server.." }
// completableJob.complete() // this will cause it to hang
completableJob.cancel()
}
embeddedServer(
factory = CIO,
host = serverOptions.host,
port = serverOptions.port,
logger = KtorKLogger(),
parentCoroutineContext = completableJob
) {
configureHTTP()
configureRouting()
}.start(true)
on a sidenote.. ith all these solutions there seems to be the problem that after the interrupt.. any println
or logging calls seem to disappear
might be a problem caused by interrupting.. i wonder.. what mistake i am overlooking..
PS: found that using cancel will work..CompletableDeferred
too