How to return response early on 202 ACCEPTED?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to return response early on 202 ACCEPTED?

Christian
I have a service that tracks events generated from the frontend. I'd prefer to send a response immediately.

---
    @Override
    void execute() throws Exception {
        post("page_view") {
            parse(TrackingPayload)
                    .observe()
                    .single()
                    .flatMap { payload ->
                Observable.merge(
                        trackingService.trackEvent("page_view", addTimestamp(payload.tracking)),
                        trackingService.trackSession(payload.session),
                        trackingService.trackUser(payload.user)
                )
            }.subscribe this.&logUploadOperation

            context.response.status(202).send()
        }

        post("job_view") {
            parse(TrackingPayload)
                    .observe()
                    .single()
                    .flatMap { payload ->
                def jobViewStream = Observable.from(payload.jobViews).flatMap { jobView ->
                    trackingService.trackEvent("job_view", addTimestamp(jobView))
                }

                Observable.merge(
                        jobViewStream,
                        trackingService.trackSession(payload.session),
                        trackingService.trackUser(payload.user)
                )
            }.subscribe this.&logUploadOperation

            context.response.status(202).send()
        }
    }
---

Doing this works fine for "page_view" but will error for "job_view" with a very cryptic message:

---
[ratpack-compute-1-3] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.

java.nio.channels.ClosedChannelException
---

Is this generally the correct approach or am I doing it wrong entirely?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to return response early on 202 ACCEPTED?

Luke Daley
Administrator
You should use Execution.fork() and run your observables in that.
Loading...