Hello. Quick question about the Application monito...
# ktor
s
Hello. Quick question about the Application monitoring of events [1]. Quite specifically
ApplicationStopPreparing
and
ApplicationStopping
[2], [3]. While testing I'm noticing my
println("ApplicationStopPreparing")
is printing a couple times. Do I need to unsubscribe from the event if I am going to do things during
ApplicationStopPreparing
that I only want to happen once? Is there guidance or documentation on the differences between these Application shutdown states and the application lifecycle? [1] https://ktor.io/docs/events.html [2] https://api.ktor.io/ktor-server/ktor-server-core/io.ktor.server.application/-application-stop-preparing.html [3] https://api.ktor.io/ktor-server/ktor-server-core/io.ktor.server.application/-application-stopping.html
K 1
a
What server engine do you use?
s
Netty
Also, embeddedServer if it makes a difference.
a
The
ApplicationStopPreparing
event is fired whenever the
stop
method is called and the problem is that it's called twice - when the signal to terminate a program is sent and after unblocking the thread before shutting down the server. You can use a flag to run your code only once in an event handler.
s
Thanks for the reply. I will add some logic to try to insure my stuff only runs once.