Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support kotlin coroutines in quarkus-scheduler #21428

Closed
mschorsch opened this issue Nov 14, 2021 · 26 comments · Fixed by #24741
Closed

Support kotlin coroutines in quarkus-scheduler #21428

mschorsch opened this issue Nov 14, 2021 · 26 comments · Fixed by #24741

Comments

@mschorsch
Copy link
Contributor

mschorsch commented Nov 14, 2021

Description

quarkus-scheduler does not support suspend functions.

In the past i bridged the gap with custom code, but i think it should be supported by quarkus.

I had this requirement multiple times.

@ApplicationScoped
class SomeBean {

    @Scheduled(every="10s")     
    suspend fun doSomething() {
        // ...
    }
}

Implementation ideas

No response

@quarkus-bot
Copy link

quarkus-bot bot commented Nov 14, 2021

/cc @evanchooly, @mkouba

@mschorsch
Copy link
Contributor Author

@geoand 😃

@geoand
Copy link
Contributor

geoand commented Nov 14, 2021

Interesting idea!

@mkouba
Copy link
Contributor

mkouba commented Nov 15, 2021

Hm, what's the point of turning a scheduled method into a coroutine? I mean it's not called directly from a user code and it's always executed on the main executor for blocking tasks. But I'm probably missing something here...

@geoand
Copy link
Contributor

geoand commented Nov 15, 2021

I assume so users can use coroutine enabled APIs

@mschorsch
Copy link
Contributor Author

I assume so users can use coroutine enabled APIs

That's the point.

With this change other suspend functions can be called directly.

@mkouba
Copy link
Contributor

mkouba commented Nov 16, 2021

Ok, contributions are welcome. I have no idea what needs to be implemented ;-).

@geoand
Copy link
Contributor

geoand commented Nov 16, 2021

I can take a look sometime after I get back, but it won't be high priority

@evanchooly
Copy link
Member

I'm not convinced the complexity is worth it. It's trivially simple to launch subsequent suspend functions via runBlocking or launch. Given the asynchronous out-of band execution of the functions it feels like time could be better spent on more user facing concerns.

@evanchooly
Copy link
Member

Has anything come of this? what's the problem with using runBlocking {} inside your scheduled task?

@mschorsch
Copy link
Contributor Author

runBlocking blocks the underlying thread.

If i make for example an API call inside a scheduled method, the underlying thread is blocked as long as the API call takes.

@evanchooly
Copy link
Member

launch {} doesn't however

@mschorsch
Copy link
Contributor Author

Do you mean GlobalScope.launch? This isn't a good option because of:

It is easy to accidentally create resource or memory leaks when GlobalScope is used. A coroutine launched in GlobalScope is not subject to the principle of structured concurrency, so if it hangs or gets delayed due to a problem (e.g. due to a slow network), it will stay working and consuming resources.

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/

I've written some glue code using a custom coroutine scope but i think it would be best if Quarkus would manage the ApplicationCoroutineScope in the same manner as in resteasy reactive.

@geoand
Copy link
Contributor

geoand commented Jan 19, 2022

This can definitely be done the proper way (like we have done in RESTEasy Reactive), but it's pretty low on our priority list...

@mschorsch
Copy link
Contributor Author

@geoand @mkouba Thanks

@geoand
Copy link
Contributor

geoand commented Apr 8, 2022

🎉

@andreas-eberle
Copy link
Contributor

@geoand : When will this be released? Could it make it to 2.8.1?

@geoand
Copy link
Contributor

geoand commented Apr 19, 2022

It will be part of Quarkus 2.9

@andreas-eberle
Copy link
Contributor

Is there a planned release date for 2.9 already? 2.8 just came out and we cannot upgrade since 2.7.3 since we get into trouble with Hibernate Reactive...

@geoand
Copy link
Contributor

geoand commented Apr 19, 2022

CR1 should be end of April if all goes well

@JamshedAlamQaderi
Copy link

Can anyone tell me how to use @Scheduled with kotlin coroutine?

@geoand
Copy link
Contributor

geoand commented Sep 13, 2022

Just annotate a public suspend function with @Suspend

@JamshedAlamQaderi
Copy link

@geoand thanks for your help. Can you provide me the actual package for @Suspend

@geoand
Copy link
Contributor

geoand commented Sep 13, 2022

Sorry, I meant @Scheduled

@JamshedAlamQaderi
Copy link

I am using io.quarkus:quarkus-scheduler. Is that okay for coroutine?

@geoand
Copy link
Contributor

geoand commented Sep 13, 2022

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants