HI, We are using kotlin with ktor server side. Is ...
# server
a
HI, We are using kotlin with ktor server side. Is there a simple way to schedule a task? In spring I just use @scheduler annotation. any examples?
s
You could use Arrow Fx Schedule, and Coroutines.
Copy code
fun Application.task(): Job = launch {
   Schedule.spaced<Unit>(1.minutes).repeat {
     // Run task
   }
}
👍 1
It has a wide set of operators for building complex Schedules. https://arrow-kt.io/docs/apidocs/arrow-fx-coroutines/arrow.fx.coroutines/-schedule/
a
I'll check. thx
Do I have to wrap it in run blocking?
Copy code
runBlocking {
            Schedule.spaced<Unit>(5.minutes).repeat {
                <http://log.info|log.info> { "here" }
            }
        }
Is it good practice?
s
You should use
Application
from
Ktor
which is a
CoroutineScope
, and then you can
launch
the
Job
with the same lifecycle as your Ktor server.
👍 2
Copy code
fun Application.task(): Job = launch {
   Schedule.spaced<Unit>(1.minutes).repeat {
     // Run task
   }
}
Application
here is
Ktor
Application