Hey guys, what do you use for scheduling repeating...
# ktor
h
Hey guys, what do you use for scheduling repeating jobs on Ktor as a server?
c
h
I'm not sure how this will help me when an action that needs to be run has to interact with the Ktor code inside the server My use case: • Create a cron job that runs everyday at specific time • It's specific to a user • User can disable it • When it runs it needs to send a push notification to the user a.k.a interacting with the database and reading few parameters that the user stored for the repeating task • Must be persisted if server reboots
c
Next time you‘d include that info in the initial question. 😉
v
Dunno if there is a more Kotlin one, but usually in JVM land I'd use Quartz, unless you run on something else
thank you color 1
c
But still doable with cron. Create an API on the server that you call with
curl
and do the things you want to do.
v
You can also cut your food with a spoon, but why should you? :-D
😆 1
c
Because you don’t know how to use a knife 😉
v
Usually it's easier to learn using it though, especially mid- to long-term. :-D
c
I’d still use something “outside” my API-server to trigger periodic tasks. nowadays server run in containers that could be scaled down and not be alive all the time. if you integrate your tasks into your API you need to get sure its awake all the time. with
cron
the server would just wake up.
v
Nowadays some servers are running in containers. And some are scaled up or down to 0. But by far not all. And as OP wants to do it in-process, it seems to not be the case for him. ;-)
👍🏼 1
👍 1
h
I’d still use something “outside” my API-server to trigger periodic tasks. nowadays server run in containers that could be scaled down and not be alive all the time. if you integrate your tasks into your API you need to get sure its awake all the time. with cron the server would just wake up.
As far as I know a Ktor server never sleeps, it's always idling
c
Not in a container environment like docker/kubernetes/Google cloud run/Azure Container Apps/Amazon ECS. But as Björn mentioned you obviously don’t care about something like this.
h
My Ktor server is running in a docker hosted on a VPS, I guess it's idling all the time?
c
if you don’t have any scaling configuration, it might “idle all the time”. 🙂
j
👍 1
thank you color 1
Or just...
while (true)
+
delay
in a simple coroutine, depending on your needs
h
Thanks, it looks like cardiologist is in process only and won't survive reboots etc.. I guess I'll try quartz
j
Cron inside the container + an endpoint it hits is what I would recommend
k
If you want cardiologist to survive process death then write your recurring schedules to a db. That's what quartz will do
🫡 1
c
I’ve just started working on an official Ktor integration for Ballast’s Scheduler. The scheduler itself has been out for a while, but I haven’t pushed the Ktor integration to a branch yet. This Gist will get you set up pretty quickly, though, and is what I will be releasing relatively soon. Just like with Cardiologist, these tasks run in-process and do not survive process death, so you’d want to post the jobs to a job queue like SQS to make them more resilient. However, I am also working on adding a persistent Job Queue module which does this work automatically, and I am hoping to have snapshot builds with this functionality up in the next few days. I’ll post back here once it’s ready for you to try out.
h
If you want cardiologist to survive process death then write your recurring schedules to a file or db. That's what quartz will do
I'll try that, I like the cardiologist's Kotlin API
👍 1
h
I have a flask app that has a custom cli command that I call with a cron job, it would be cool if ktor had something similar
j
You can do that today. Ktor doesn't need to support it
👍 1