Style query - what do other folks do for 202 Accepted?

38 views
Skip to first unread message

Timothy Perrett

unread,
May 7, 2013, 8:37:01 PM5/7/13
to spray...@googlegroups.com
Hey all, 

Lets say you have a resource that needs to return 202 Accepted, and the work you're doing is going to be spun off in a Future, or Actor etc. In the definition of the routes, what other non-side effecting definition could one use other than:

get {
  thingThatDoesSomeWork
  complete(HttpResponse(StatusCodes.Accepted))
}

I dont want to wait until that future completes (a la onComplete, onReady, onSucssess), as its clearly an async call, but whats the pattern others are using here? It's not a major issue, it just annoys me having a side-effect like this. 

Cheers, Tim 

Mathias

unread,
May 8, 2013, 3:21:36 AM5/8/13
to spray...@googlegroups.com
Tim,

if I understand you correctly you want to have the route return a 202 response and trigger an async job that is going to be completed sometime in the future.
I can't see how you'd be able to do this without side-effecting since starting an async job and not waiting for its result seems like an inherently side-effecting task to me, no?

One comment, this:

> get {
> thingThatDoesSomeWork
> complete(HttpResponse(StatusCodes.Accepted))
> }

should be better written like this:

get {
complete {
thingThatDoesSomeWork
StatusCodes.Accepted
}
}

The two are almost equivalent but the latter is more resilient to future changes like this one for example:

get {
parameter(`someParam) { p =>
complete {
thingThatDoesSomeWork
StatusCodes.Accepted
}
}
}

which executes the `thingThatDoesSomeWork` at the "right" time, whereas this doesn't:

get {
thingThatDoesSomeWork
parameter(`someParam) { p =>
complete(StatusCodes.Accepted)
}
}

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Reply all
Reply to author
Forward
0 new messages