spray.io strange get/delete/detach directives behavior - scala

I have written the following code using spray routing directives:
path("goal" / Segment) { id =>
get {
detach(ec) {
val goal = srv.find(id)
complete(goal)
}
} ~
delete {
detach(ec) {
srv.delete(id)
complete(OK)
}
}
}
The problem is the execution path is strange. First it goes to get->detach->srv.find->complete
then to delete -> detach -> srv.delete -> complete
and then it comes back to get->detach->... and completes there. The issue is srv.delete is executed which is not a desired behavior because I loose data. Can anyone explain me this behavior and tell me how to fix it ?
Thanks in advance.

I don't understand why but it seems that this modification works properly:
path("goal" / Segment) { id =>
get {
detach(ec) {
val goal = srv.find(id)
complete(goal)
}
} ~
delete {
detach(ec) {
complete {
srv.delete(id) // here
OK
}
}
}
}
Examples in the documentation here shows only cases where after detach comes only complete/reject directive. Therefore I suspect it was tested only with these.

I had the same issue, it looks like something related to Spray route DSL, I solved it adding the command in the path:
(get & path("goal" / Segment)) { id =>
detach(ec) {
val goal = srv.find(id)
complete(goal)
}
} ~
(delete & path("goal" / Segment)) { id =>
detach(ec) {
srv.delete(id)
complete(OK)
}
}
It would be nice to see another solution to be honest instead of having to always replicate the path but up to now this was the only thing which worked.

Related

Repeat Single based on onSuccess() value

I want to repeat a Single based on the single value emitted in onSuccess(). Here is a working example
import org.reactivestreams.Publisher;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.functions.Function;
public class Temp {
void main() {
Job job = new Job();
Single.just(job)
.map(this::processJob)
.repeatWhen(new Function<Flowable<Object>, Publisher<?>>() {
#Override
public Publisher<?> apply(Flowable<Object> objectFlowable) throws Exception {
// TODO repeat when Single emits false
return null;
}
})
.subscribe();
}
/**
* returns true if process succeeded, false if failed
*/
boolean processJob(Job job) {
return true;
}
class Job {
}
}
I understand how repeatWhen works for Observables by relying on the "complete" notification. However since Single doesn't receive that notification I'm not sure what the Flowable<Object> is really giving me. Also why do I need to return a Publisher from this function?
Instead of relying on a boolean value, you could make your job throw an exception when it fails:
class Job {
var isSuccess: Boolean = false
}
fun processJob(job: Job): String {
if (job.isSuccess) {
return "job succeeds"
} else {
throw Exception("job failed")
}
}
val job = Job()
Single.just(job)
.map { processJob(it) }
.retry() // will resubscribe until your job succeeds
.subscribe(
{ value -> print(value) },
{ error -> print(error) }
)
i saw a small discrepancy in the latest docs and your code, so i did a little digging...
(side note - i think the semantics of retryWhen seem like the more appropriate operator for your case, so i've substituted it in for your usage of repeatWhen. but i think the root of your problem remains the same in either case).
the signature for retryWhen is:
retryWhen(Function<? super Flowable<Throwable>,? extends Publisher<?>> handler)
that parameter is a factory function whose input is a source that emits anytime onError is called upstream, giving you the ability to insert custom retry logic that may be influenced through interrogation of the underlying Throwable. this begins to answer your first question of "I'm not sure what the Flowable<Object> is really giving me" - it shouldn't be Flowable<Object> to begin with, it should be Flowable<Throwable> (for the reason i just described).
so where did Flowable<Object> come from? i managed to reproduce IntelliJ's generation of this code through it's auto-complete feature using RxJava version 2.1.17. upgrading to 2.2.0, however, produces the correct result of Flowable<Throwable>. so, see if upgrading to the latest version generates the correct result for you as well.
as for your second question of "Also why do I need to return a Publisher from this function?" - this is used to determine if re-subscription should happen. if the factory function returns a Publisher that emits a terminal state (ie calls onError() or onComplete()) re-subscription will not happen. however, if onNext() is called, it will. (this also explains why the Publisher isn't typed - the type doesn't matter. the only thing that does matter is what kind of notification it publishes).
another way to rewrite this, incorporating the above, might be as follows:
// just some type to use as a signal to retry
private class SpecialException extends RuntimeException {}
// job processing results in a Completable that either completes or
// doesn't (by way of an exception)
private Completable rxProcessJob(Job job) {
return Completable.complete();
// return Completable.error(new SpecialException());
}
...
rxProcessJob(new Job())
.retryWhen(errors -> {
return errors.flatMap(throwable -> {
if(throwable instanceof SpecialException) {
return PublishProcessor.just(1);
}
return PublishProcessor.error(throwable);
});
})
.subscribe(
() -> {
System.out.println("## onComplete()");
},
error -> {
System.out.println("## onError(" + error.getMessage() + ")");
}
);
i hope that helps!
The accepted answer would work, but is hackish. You don't need to throw an error; simply filter the output of processJob which converts the Single to a Maybe, and then use the repeatWhen handler to decide how many times, or with what delay, you may want to resubscribe. See Kotlin code below from a working example, you should be able to easily translate this to Java.
filter { it }
.repeatWhen { handler ->
handler.zipWith(1..3) { _, i -> i }
.flatMap { retryCount -> Flowable.timer(retryDelay.toDouble().pow(retryCount).toLong(), TimeUnit.SECONDS) }
.doOnNext { log.warn("Retrying...") }
}

cannot get rest service

This drives me crazy, I have 2 http requests in a sample application:
open class RestController : Controller() {
val api = Rest()
init {
api.baseURI = "http://127.0.0.1:5059/"
}
}
class PendingCtlr : RestController() {
fun load(): ObservableList<PendingEntity> {
val txt = api.get("pendings").list()
val temp = txt.toModel<PendingEntity>()
return temp.observable()
}
}
class ConfirmedCtrl : RestController() {
fun load(id: Long): ObservableList<ConfirmedEntity> {
val li= api.get("confirmeds").list()
val temp = li.toModel<ConfirmedEntity>()
return temp.observable()
}
}
The first one works, the second one doesn't even hit the application level, it gets rejected with 400 BadRequest by my backend (Werkzeug).
I see absolutely no difference in both functions, and I can call both routes from my Swagger, as well as from python as well as from curl! Could someone please advise at least where to look for debug?
EDIT: The problem was on server side -_- Solved

Spray route rejections

I am building some custom directives with Spray and I find a problem:
When a route is rejected I thought that it was immediately completed with a rejection. However it is not and it tries to complete the inner route. For example, in this code:
path("users") {
post {
entity(as[User]){
user =>
directive_always_reject(){
println("Hello")
complete(200, "Hello")
}
}
}
}
The "hello" is always printed, even if the directive is rejected.
I always thought that when a route is rejected it does not continue to inner routes (or at least is how I understand it)
Did I misunderstand something with Spray or there is a bug somewhere in my code?
Thanks you!
[UPDATE 1]
An implementation of directive_always_reject() could be:
def directive_always_reject[T]() : Directive0 = {
reject(
new ValidationRejection("Some error")
)
}
[UPDATE 2]
I see that a solution could be put the println inside the complete function.
path("users") {
post {
entity(as[User]){
user =>
directive_always_reject(){
complete {
println("Hello")
"Hello"
}
}
}
}
}
However this solution does not apply to me because in my real code I pass the RequestContext to another actor who is the one that completes the request.

Spray rejection handler not detecting an existing path

I have a file static.scala :
val staticRoute = {
path("") {
getFromResource("web/index.html")
} ~ pathPrefix("web") {
getFromResourceDirectory("web")
}
}
Another file a.scala :
val actionRoute = (handleRejections(rejectionHandler) & handleExceptions(exceptionHandler))
{
path("action" / "create") {
(put | post) {
implicit ctx => {
val xmlString = ctx.request.entity.asString
val actionObject = XML.loadString(xmlString).toAction
ActionService.write(actionObject)
sendResponse(StatusCodes.OK, APIResponseOK(Map("id" -> actionObject.id)))
}
}
}
}
My Base.scala contains the definition of the rejectionHandler :
val rejectionHandler = RejectionHandler {
case Nil => ctx => {
complete((StatusCodes.NotFound, "The Requested Resource was not found"))
}
case mcr : MissingCookieRejection =>ctx=> { //testing
complete(StatusCodes.BadRequest, "Missing cookie")
}
}
Both the files extend Base.scala which has the rejection handler defined in it. However on opening the correct port for the server ( localhost:8080 ) which corresponds to
path("")
in static.scala , the rejectionHandler still is going to case Nil and printing the message "The Requested Resource was not found" which should not be the case! Shouldn't it enter the handler if that path is not defined? If i comment out the rejectionHandler everything works as expected. Please help me out !
actionRoute will complete / because it doesn't have a path for it and does have a handleRejections. Which means it does have a route for / and actionRoute ~ staticRoute will never "fall through" to staticRoute.
You usually want to only do rejection handling at the very top level (or maybe within a pathPrefix, if you don't want other routes to use the same prefix). Move the handleRejections out of actionRoute and move it right up to the top level:
handleRejections(myHandler) {
actionsRoute ~ staticRoute
}

Execute some logic asynchronously in spray routing

Here is my simple routing application:
object Main extends App with SimpleRoutingApp {
implicit val system = ActorSystem("my-system")
startServer(interface = "0.0.0.0", port = System.getenv("PORT").toInt) {
import format.UsageJsonFormat._
import spray.httpx.SprayJsonSupport._
path("") {
get {
complete("OK")
}
} ~
path("meter" / JavaUUID) {
meterUUID => pathEnd {
post {
entity(as[Usage]) {
usage =>
// execute some logic asynchronously
// do not wait for the result
complete("OK")
}
}
}
}
}
}
What I want to achieve is to execute some logic asynchronously in my path directive, do not wait for the result and return immediately HTTP 200 OK.
I am quite new to Scala and spray and wondering if there is any spray way to solve this specific problem. Otherwise I would go into direction of creating Actor for every request and letting it to do the job. Please advice.
There's no special way of handling this in spray: simply fire your async action (a method returning a Future, a message sent to an actor, whatever) and call complete right after.
def doStuffAsync = Future {
// literally anything
}
path("meter" / JavaUUID) { meterUUID =>
pathEnd {
post {
entity(as[Usage]) { usage =>
doStuffAsync()
complete("OK")
}
}
}
}
Conversely, if you need to wait for an async action to complete before sending the response, you can use spray-specific directives for working with Futures or Actors.