How to Run Cron Jobs in Kotlin Ktor?

廉价感情. 提交于 2020-01-05 07:12:36

问题


Is there a way to run Cron Jobs with Ktor? My end objective is to host a Cron Job written with Kotlin for the Coinverse app's backend service to populate data.

I'm currently hosting multiple Java .jar apps written in Kotlin on AppEngine. I'm looking to refactor these apps into Ktor apps on AppEngine with a Cron Job for scheduled tasks, as the .jar apps have more issues with dependencies.

I'm looking for Ktor's equivalent to Cloud Functions' built-in implementation for Cron Jobs with JavaScript.

functions.pubsub.schedule

Back-up option: If Ktor does not have this feature and I want to keep the code in Kotlin, Google has an alpha, Using Kotlin with Google Cloud Functions. It appears Kotlin + Cloud Functions' built-in implementation could be used with this approach.


回答1:


Sergey Mashkov from the JetBrains team suggests in the kotlinlang Slack group to launch a Kotlin Coroutine on the Application scope using an infinite loop and delay.

Then, the Ktor app can be deployed to AppEngine.

fun Application.main() {
    launch {
        while(true) {
            delay(600000)
            // Populate data here.
        }
    }
}


来源:https://stackoverflow.com/questions/59516599/how-to-run-cron-jobs-in-kotlin-ktor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!