micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

@Scheduled `suspend fun` never runs

Open rawilder opened this issue 2 years ago • 9 comments

Expected Behavior

	@Scheduled(fixedDelay = "1s")
	suspend fun suspendTest() {
		println("suspend Hello World")
	}

Expected is that "suspend Hello World" prints once per second

Actual Behaviour

Nothing prints

Steps To Reproduce

  1. start a project with micronaut-core in kotlin
  2. make a suspending function inside a singleton
  3. annotate it with @Scheduled
  4. run and observe behavior

Environment Information

jdk 11 macos 12.3

Example Application

https://github.com/rawilder/scheduled-kotlin

Version

3.4.2

rawilder avatar Apr 15 '22 20:04 rawilder

@rawilder What is the point in making it a suspend function?

jameskleeh avatar Apr 17 '22 02:04 jameskleeh

Clean usage with other suspending functions. If I wanted to schedule once a day to refresh data from a third party API, that API client is likely a publisher or suspending function. Rather than hard blocking inside the scheduled function (runBlocking or <ReactorType>.block()), it'd be nice to be able to tie together the functionality cleanly. runBlocking has given me a lot of grief so I try to avoid it when possible.

rawilder avatar Apr 17 '22 17:04 rawilder

This is because the scheduled processor checks if the method has 0 arguments. In the case of a suspend function being called from Java, it takes a continuation argument

jameskleeh avatar Apr 20 '22 00:04 jameskleeh

It doesn't make much sense to have it, we can't do anything special to block it, and probably an error message should be thrown in that case.

dstepanov avatar Apr 22 '22 13:04 dstepanov

perhaps this should be a compilation error

graemerocher avatar Apr 22 '22 14:04 graemerocher

So is the recommended solution to wrap it with runBlocking {}?

rawilder avatar Apr 22 '22 14:04 rawilder

I think this should be handled at the framework level, otherwise anyone who executes methods will likely run into this issue and will have to handle it. For ExecutableMethods we should pre-load the continuation

jameskleeh avatar Apr 30 '22 01:04 jameskleeh

This is what I tried. Since the runner checks for 0 arguments, I added an else case to check for one argument of the continuation type but I got stuck trying to figure out what to pass in and where to get it from.

rawilder avatar Jun 21 '22 15:06 rawilder

bump - happening to us too

borrow-checker avatar Sep 07 '22 13:09 borrow-checker