when user Ctrl+C app, that lambda never gets calle...
# ktor
v
when user Ctrl+C app, that lambda never gets called
r
Are you sure it’s supposed to work like that? Maybe this event is thrown only when some
.stop()
function is called on the app
👍 1
I don’t see anything in the docs stating that this works with signals
You could make it work with signals yourself I guess, calling that
.stop()
function, if it exists
v
Nope I'm not sure, I was just expecting it to be the behavior. So rephrasing my question, is there anyway that we can receive an event when an app is stopped?
I'm asking this because I need some lifecycle support for my app when it gets destroyed
so for example I'm running inside containers that get killed via SIGINT and right now they don't get a chance to perform some cleaning tasks
One alternative as you mentioned is to stop using the recommended way of creating a module
and running
embeddedServer
combined with some shutdownHook on the main thread to capture those signals, but I wonder why ain't this the default?
r
Trying to deal with signals in the JVM is generally unreliable, as the JVM will kill itself anyway after some time if it received some signals and you take to long to terminate... Or at least that was the case years ago
v
I can't comment on that, not sure really. But coming from Spring Boot, they usually hook the container stop to a shutdown hook which would allow such scenarios.
What I did so far, and not a real problem really, but would be nice to have an "official" way of handling this
Copy code
val server = embeddedServer(Netty, port = 8080){
        environment.monitor.subscribe(ApplicationStopping){
            println("Cleaning up")
        }
    }.start(false)
    Runtime.getRuntime().addShutdownHook(Thread {
        server.stop(1, 5, TimeUnit.SECONDS)
    })
    Thread.currentThread().join()