I'm new to backend development, but I'm getting in...
# ktor
c
I'm new to backend development, but I'm getting into the swing of things with ktor deployed on a small server. One feature I want in my backend is to be able to save "reminders" and put them into a database, and then like every minute or so, query the db to see if there are any reminders to send out. How would I do that with ktor? Do I just create a new coroutine that searches the db every 60 seconds or so, and does it's work at that point? Or is this more of a cronjob sort of thing that I'd want to setup on the server somehow. I'm an android dev, so I'm a bit out of my depth here. 😅
p
Cron job better approach. A coroutine might work but depending on how/where the ktor App is running, you don't really control the lifetime of it. It may live in a container in a distributed system that shuts down if there is no incoming requests, things of that nature.
e
there's plenty of job schedulers for JVM, but yeah I would usually consider scheduled tasks as a separate thing from serving live requests
c
interesting. new to this world. "job schedulers" will search that up. seems like keeping ktor to to serving live requests makes sense. thanks
since im here. are any of these job schedulers something that comes from ktor/or any kotlin specific things? I'm tempted to just spin up a service that's just a
Copy code
main{
while(true){
//searchThroughDbAndSendNotifications
delay(60.seconds)
}
}
👀
p
Humm interesting, I would do some research. Perhaps some plugin for ktor already exists. I really haven't done it directly from ktor. Normally I use a cloud API for this, most cloud providers offer this. Basically you will get a call to one of your endpoints at a specific time or periodically. If you are running your own server, then something similar to your solution can work. Sort of
while (isActive) { do this }
e
of course you can have a single binary that contains both code paths, but the way you scale up your live servers vs the way you scale up your asynchronous job processors in response to load is different, so I wouldn't bother trying to shoehorn job scheduling into ktor. they can just be two separate things that your code does.
âž• 3
o
@Colton Idle Since you said "deployed on a small server": If you don't target the cloud and can easily afford an always-on process, using cron would be like using a sledgehammer to crack a nut: More moving parts, extra installation of a cron job required, extra cron logging to be monitored, API calls stringly typed... You can have Ktor running continuously on a JVM, then launch a coroutine, use delay for scheduling at intervals. There are claims that using cron would "reduce the error surface". Does it? If a cron job calls an API, what if there is no one listening?
✅ 1
c
Thanks all, great insights and thanks for that link Oliver. I will take a look!