Hey everyone, I just launched my first Ktor based ...
# server
c
Hey everyone, I just launched my first Ktor based backend in a production environment and am currently looking for a way to run periodic jobs (e.g. once an hour) to do some data processing, which takes 5mins max. How would you solve this, ideally without going the route of using something like a job queue service?
o
Use a coroutine. Your application module might look like this (untested):
Copy code
fun Application.module() {
    install(ContentNegotiation)

    routing {
        get("/") {
            // do stuff
        }

        static {
            resources()
        }
    }

    launch {  // launch a background job with application lifetime
        while (true) {
            delay(1 /* hours */ * 3_600_000 /* milliseconds */)
            // do processing
        }
    }
}
b
Pro tip, you can do delay(1.hours) for increased readability
👍 1
e
why not a job queue service? IMO you'll likely need to build some monitoring around this to ensure the coroutine didn't crash and check on its status, and by then... just use a pre-built solution?
☝️ 1
o
@Big Chungus I have intentionally avoided
Duration
due to API changes between Kotlin 1.4.x and 1.5. The above should work with both.
👍 1
c
Thanks @Oliver.O and @Big Chungus for the replies, this solution also came up when googling this problem @ephemient we don’t really want to use a separate service for that (for now) to keep the amount of moving parts in our infrastructure low
r
I’ve used a second application module with a
runBlocking
to enable coroutines for a similar case (event bus connection).