i’m trying to do some async processing in the scope of a ktor web request, our server is expecting to receive a payload and upon receiving the payload, the server is supposed to return a http 200 immediately and then do some processing on the payload (which will involve making some API calls), any ideas on how to best do this in a ktor app?
12/22/2020, 8:27 PM
schedule/launch the next tasks in GlobalScope or similar , maybe wrap them in a timeout or so,
then return 200
12/22/2020, 8:32 PM
launching them in the globalscope will ensure they won’t get canceled when the server returns a response?
12/22/2020, 8:37 PM
its the easiest and most straightforward way to start a task outside of structured concurrency
this is for a webhook integration with a third party service and the third party recommends returning a 200 response as fast as possible, so i don’t have control over the request body
12/22/2020, 11:48 PM
Yeah, I was expecting something similar. So then you need to go out of request scope.
Application has a coroutine scope. Maybe you can inject some BackgroundOperationProvider where you want to process your payload, and this provider can get application scope injected inside, so you can launch inside this scope. Just a quick idea...
It is written there "application coroutine scope that is cancelled immediately at application stop so useful for launching background coroutines". Seems matching what you want