Simple question: How does one schedule a task in K...
# ktor
g
Simple question: How does one schedule a task in Ktor? Code seems to indicate it is capable but docs are missing... "as soon as you have to reverse engineer someone else's library to use it you should question whether you should be using it for anything meaningful", says me, lol. Here is how spring does it:
Copy code
@Scheduled(fixedRate = 60000)
r
Ktor is a web framework, not a general application framework. Why would it provide scheduling capabilities?
g
Maybe your right, probably are, yeah you are, lol... just got used to spring I guess, everyone has their own definition of what these things are, lol. My task is to update a database every hour whose data gets served up over REST, the invocation of that scheduled task will be within fun Application.api() which is ktor. So I should just use something like this you think: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/java.util.-timer/schedule.html
r
Yeah, Spring's
@Scheduled
annotation just abstracts over the JDKs executer API. See https://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html.
I think for advanced use cases it might use Quartz, but you don't really need that unless you want to do cluster-aware job execution and task status persistence.
g
Thanks man, really helps to bounce shit off another brain 😃
👍 1
c
Since ktor is all based on coroutines, I’ve been starting to play around with the idea of spawning a background queue at application startup through a custom Feature. Initial tests seem to work pretty well, but I have no idea how well it would work with any kind of load
g
I haven't really figured out coroutines yet at all really... I mean, I know multithreading and multiprocessing like I know how to breath but I don't get the change in terminology or the distinction. Here is where it looks like ktor added a scheduling feature but I don't know. https://github.com/ktorio/ktor/pull/1272/files
r
A simple "scheduled" task with a coroutine is basically a
while
loop with a call to
delay
... that idiom can extend to all sorts of use cases.
👍 1
👆 1
1
💯 1
g
hmm... hoping there is a library I can use to take of the while loop part, it can get complicated can't it?
r
That pull request was closed as stale, so it was never actually merged into ktor.
g
hmm... okay, makes sense
c
It shoudn’t take much to copy that PR to a separate library (with attribution, of course)
r
Architecturally, I don't know why you would combine updating your db data with your REST service. There is no reason to couple those things together, and plenty of reason not to.
g
Your right, already realized it moment ago and created Applilcation.service() for the ktor main to consume... also got Application.api and web to separate that stuff cause some of the content is entirely data driven.
Here is what I got... trying to do pure kotlin, limit the java as much as possible. Not sure why Application. is greyed out, it isn't for the web and api functions. See anything wrong with this? I feel like I should be invoking the Application object or something and not its children... also don't really understand the module yet... love the connector and embeddedServer thingy but wonder if embeddedServer is okay for enterprise applications... just thinking outloud.
r
Nothing wrong with using
embeddedServer
. Personally I wouldn't mix the
Aggregator
with the ktor app at all, as I don't see any need to -- I'd probably start a separate coroutine or scheduled thread for that (via
ScheduledExecutorService
which is newer and more robust than
Timer
). Don't know what your
web
and
api
calls are doing so can't comment there.
g
serving up HTML and REST JSON data
r
Ok yeah all good there
g
cool... I guess the reason I was intending to put everything in ktor's main is mainly because I want to be able to kick off all services for this website at once with a single gradle task.
r
You can still put it inside the same
main
. Its not "ktor's main" when you use the embedded server, its just
main
🙂
g
ok, move the aggregator out of the env and remove the Application association, that makes sense. Still not really sure about what the module is really doing... should web/api have their own module or be together in one?
Read the docs, but wasn't clear
r
As I understand it, the
module
is just ktor's way of combining all your web-apps features and routing information. Its providing you a nice DSL as
Application
is its receiver.
Your aggregator makes no use of
Application
at all, so there is no need to couple them together.
g
makes perfect sense man, thanks... I'm always fighting myself between the way I want a thing to work, the way I think a thing works and the way a thing actually works, lol, the toils of an engineer learning.
r
I think you would create separate modules for web and api if you want them to have different pipelines e.g. different features / headers / exception handling, etc.
g
Yeah, not sure.. the application.functions themselves are refered to as modules in the below link. https://ktor.io/servers/application.html... seems it would make more sense if module was called Application but I guess that word is too long, lol.
r
I'm not sure either, but... the call to
Application.module
adds a module to a list of modules. Also note the top-level docs https://ktor.io/servers/index.html indicate:
Application modules are started one by one when an application is started, and every module can configure an instance of the application. An application instance is configured by installing features and intercepting pipelines.
Which implies to me if you want different features/pipelines you want different modules.
g
"and every module can configure an instance of the application." that is the part that is confusing...
so modules can configure applications and applications configure modules?
meh, I guess it doesn't matter, lol, overthinking it now I am.
r
It is confusing, yes. I don't think they mean "instance of an Application" literally.
"An instance of the application" --> one application.
g
Ahhhhh! your a smart dude
r
I could still be wrong though 🙂 My ktor apps all have one module. Try it out with a couple of test modules and see.
g
Yeah, really trying to use everything Kotlin... use everything new, no old if possible... starting some new projects. It is a challenge, not a lot of secondary content online for this stuff and much of the primary content is old or incomplete. I think .NET did what kotlin is trying to do quite a while back, but .NET sucks... it just has more sources so was easier to pickup faster I guess.
r
All depends on your background... with a Java background, Kotlin for me was way easier to pick up than .NET. I find .NET has the same problem of outdated information x 10 -- there every doc depends on which of 10 .NET versions you are actually running, and then there is the whole .NET/.NET Core split / migration. Plus I find Microsoft's documentation generally sucks (its likely to get better now its all on GitHub).
g
yeah... kotlin docs aren't bad, MS does suck
What would you say should go in here
Copy code
fun Application.api() { }
currently I have routing and a function for json output in there... but should routing be segregated for a large project and, if so, what would that look like (e.g. Application.api.routes or many can it be turned into a class with member routes?)? Also if it is separated, what else is there... obviously there would have to be more than routes.
260 Views