Difficulty working with AsyncResult

86 views
Skip to first unread message

Wes

unread,
May 23, 2016, 8:58:15 AM5/23/16
to scalatra-user
Hi,

I was wondering if anyone can help clear something up. I'm trying to work with Akka and AsyncResult and can't seem to get everything working is it should.

I've got the following Scalatra endpoint:

get("/job/:id") {
  new AsyncResult() {
    override val is = supervisor ? StatusRequest(params("id"))
    is onFailure {
      case e: FileNotFoundException =>
        logger.error(s"Unable to find job status: ${e.getMessage}")
        status_=(404)
      case e: Exception =>
        status_=(500)
    }
  }
}

I'm trying to return a 404 in case a file could not be found. Problem is that I'm only getting 200s. The message is handled in an actor as follows:

case statusRequest: StatusRequest =>
  log.info(s"Status requested: $statusRequest")
  val json = fileContents(s"$baseDirectory/jobs/${statusRequest.jobId}/state")
  json match {
    case Success(s) =>
      sender ! read[JobStatus](s)
    case Failure(e) =>
      sender ! Status.Failure(e)
  }

Testing the endpoint yields the following results:

» curl -v localhost:1337/job/1463472416-15
*   Trying ::1...
* Connected to localhost (::1) port 1337 (#0)
> GET /job/1463472416-15 HTTP/1.1
> Host: localhost:1337
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< Date: Mon, 23 May 2016 12:56:04 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 1518
< Server: Jetty(9.2.15.v20160210)
<
java.io.FileNotFoundException: ./supervisor/jobs/1463472416-15/state (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at scala.io.Source$.fromFile(Source.scala:91)
at scala.io.Source$.fromFile(Source.scala:76)
at scala.io.Source$.fromFile(Source.scala:54)
at webiq.supervisor.actors.Supervisor$$anonfun$fileContents$1.apply(Supervisor.scala:84)
at webiq.supervisor.actors.Supervisor$$anonfun$fileContents$1.apply(Supervisor.scala:84)
at scala.util.Try$.apply(Try.scala:192)
at webiq.supervisor.actors.Supervisor.fileContents(Supervisor.scala:84)
at webiq.supervisor.actors.Supervisor$$anonfun$receive$1.applyOrElse(Supervisor.scala:36)
at akka.actor.Actor$class.aroundReceive(Actor.scala:482)
at webiq.supervisor.actors.Supervisor.aroundReceive(Supervisor.scala:18)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
at akka.actor.ActorCell.invoke(ActorCell.scala:495)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
* Connection #0 to host localhost left intact

The following logging line is printed though:

logger.error(s"Unable to find job status: ${e.getMessage}")

What am I missing here?

Wes

unread,
May 23, 2016, 10:09:04 AM5/23/16
to scalatra-user
Changing the endpoint code did the trick, still unsure if this is the preferred way of doing it.

get("/job/:id") {
  new AsyncResult() {
    val is = supervisor ? StatusRequest(params("id"))
    is.onFailure {
      case e: FileNotFoundException =>
        response.status_=(ResponseStatus(404))
        response.end()
      case e: Exception =>
        response.status_=(ResponseStatus(500))
        response.end()
    }
  }
}


Reply all
Reply to author
Forward
0 new messages