Hey, any documentation reference on how to read co...
# announcements
c
Hey, any documentation reference on how to read configuration parameters from files and reload the app on configuration changes ?
b
Well you can always start an app as a coroutines job and then have a separate coroutine periodically checking for config changes. When it detects the changes, cancel app job and spawn a new one. It's pretty simple to implement
c
I would prefer using something that already exists as a library/module
b
Oh. Never heard of one I'm affraid.
👌 1
But again, I don't think there's a need for such a library. Here's a pseudo-code for it:
Copy code
fun main() {
  runBlocking{
    var config = TODO("Read initial config as data class")
    var app = launch(Dispatchers.Main) {
      initApp(config)
    }
    launch(Dispatchers.Default) {
      delay(TODO("Check interval"))
      val currentConfig = TODO("Read config")
      if(config != currentConfig) {
       app.cancelAndJoin()
        config = currentConfig
        app = launch(Dispatchers.Main) {
          initApp(config)
        }
      }
    }
  }
}
m
If you don’t mind using a Java library, OWNER offers a hot reload functionality for properties, plus lots more: http://owner.aeonbits.org/docs/reload/
a
Also there's
WatchService
to detect changes
âž• 1
m
otherwise try looking into these libs, maybe there’s some with hot reloading https://kotlin.libhunt.com/categories/5622-configuration