Hi! I have a sever application, which has some mi...
# ktor
p
Hi! I have a sever application, which has some mission-critical background tasks. If they are failed, the best you can do, is just to stop application. What is good coroutine context to start them? I'm trying to start in Application, but it's a SupervisorJob, so it doesn't propagate failures. I can do something like add an inner scope inside Application with handler doing a
System.exit()
, but that's looks too rude way to stop. Maybe you have some better way to do that?
👀 1
p
What about creating outer scope and launch Ktor application along with backend tasks in it?
s
You can launch
embeddedServer
which runs on a
CoroutineScope
which is then the parent of
Application : SupervisorJob
so if you can the outer
CoroutineScope
it also stop the
Application
Copy code
coroutineScope parent@ {
  val engine = embeddedServer(...) {
    // use parent
  }
  // use parent
  // awaitCancellation()
  //engine.start(wait = true)
}
runBlocking
can be a replacement for
coroutineScope
here as well depending on where you need to actually launch this code
p
Looks good. Can this be done with EngineMain to continue using file-based config?
s
I think if you use
EngineMain
you lose control over how the server is started, and thus its lifecycle. Ktor has some support to still rely on file config, but never used it https://ktor.io/docs/configurations.html#embedded-custom I mostly rely on manual configuration, like this https://github.com/nomisRev/ktor-arrow-example/blob/main/src/main/kotlin/io/github/nomisrev/env/Env.kt And I've also used the hoplite library, https://github.com/sksamuel/hoplite
e
Hoplite is really nice to work with 👍