Hi, I got a kotlin server app that is deployed in ...
# server
l
Hi, I got a kotlin server app that is deployed in pods (k8s). In some cases, k8s can send SIGTERM / SIGKILL signals to kill my app. What is the good way, in Kotlin, to gracefully shutdown (close all connections...)? Is there better than the java way?
Copy code
Runtime.getRuntime().addShutdownHook(myShutdownHook);
s
I build a small
SuspendApp { }
variant on top of
runBlocking
that does exactly this on top of Structured Concurrency. So, if you receive a
SIGTERM
or
SIGINT
it will
cancel
the
CoroutineScope
, and it'll block the shutdown thread until the entire program is finished (
CoroutineScope.join
). This allows you to run finalisers, such as committing offsets to Kafka,
stop(500, 500)
any network engine, clean-up, etc. Side-note: it lives under the Arrow-kt umbrella, but it only has KotlinX Coroutines as a dependency. https://github.com/arrow-kt/suspendapp
👍🏾 1
gratitude thank you 1
👍 2
e
You should watch Simon’s excellent video on the topic as well. I can’t find it now sadly
👍🏾 1
s
I think you're referring to this one @Emil Kantis,

https://www.youtube.com/watch?v=A69_t_oEP_E

Thanks ☺️
gratitude thank you 1
K 2