Reputation: 1192
I have a RESTful API built on scala. One of the routes calls an external command and catches the output using ProcessLogger. When I put a small amount of load on the server (via unit test) the external command call starts to intermittently fail. No response comes back and the HTTP request times out. The return code of the Process call isn't populated, there are no exceptions thrown... I can't catch or find anything in the logs. Is there a specific way to catch all issues with Process calls in scala that might help me to debug whats going on?
My code looks like this...
try {
var resultCode:Int =
(Seq("echo",data) #|
Seq("external-command-to-call")) !
ProcessLogger(stdout append _, stderr append _)
println("Output "+resultCode)
println(stderr)
println(stdout)
// check return code
if(resultCode > 0) {
Left("An error occurred")
}
} catch {
case e: Exception => {
Left("An exception occurred")
}
}
9 times out of 10 it will print the "Output", but intermittently it will just die on the external call, never printing "Output". Neither of the "Left" get triggered either. I tried using Try/Success/Failure and catching "Throwable" but no joy.
Upvotes: 2
Views: 671
Reputation: 2686
So the problem can be solved using Future monad of the Scala what you need to do is change those processLogger method to return Future[Int] now you should not use the onSuccess and onFailure method of Future as they are deprecated so you can insure it by adding a call back on this Future.
By using onComplete
or map
or foreach
result.onComplete{
case Success(x) => println(x)
case Failure(ex)=>ex.printStackTrace}
Upvotes: 0
Reputation: 408
This is just an idea from me. Can't we use Scala Futures to solve this problem :) I hope you could use the Future onFailure to handle this situation. I found this similar article about Scala Process Logger in StackOverflow and hope this might be helpful for you. Scala ProcessLogger & Future: How can I make sure all line callbacks are called BEFORE the onComplete block?
When a Future is completed with a value it is a successful completion. If a Future is completed with an exception it is a failure. https://docs.scala-lang.org/overviews/core/futures.html
`val f: Future[List[String]] = Future {
session.getRecentPosts
}
f onFailure {
case t => println("An error has occured: " + t.getMessage)
}
f onSuccess {
case posts => for (post <- posts) println(post)
} `
Upvotes: 2