16
16
17
17
package de .upb .cs .swt .delphi .cli .commands
18
18
19
- import java .util .concurrent .TimeUnit
19
+ import java .util .concurrent .{ TimeUnit , TimeoutException }
20
20
21
21
import akka .actor .ActorSystem
22
22
import akka .http .scaladsl .Http
@@ -29,14 +29,16 @@ import akka.util.ByteString
29
29
import de .upb .cs .swt .delphi .cli .Config
30
30
import de .upb .cs .swt .delphi .cli .artifacts .SearchResult
31
31
import de .upb .cs .swt .delphi .cli .artifacts .SearchResultJson ._
32
- import de .upb .cs .swt .delphi .cli .commands .RetrieveCommand .information
33
32
import spray .json .DefaultJsonProtocol
34
33
35
34
import scala .concurrent .duration ._
36
- import scala .concurrent .{Await , Future }
37
- import scala .util .{Failure , Success }
35
+ import scala .concurrent .{Await , ExecutionContextExecutor , Future }
36
+ import scala .util .{Failure , Success , Try }
38
37
39
38
object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
39
+
40
+ val searchTimeout : Int = 10
41
+
40
42
/**
41
43
* Executes the command implementation
42
44
*
@@ -59,7 +61,20 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
59
61
Http ().singleRequest(HttpRequest (uri = searchUri, method = HttpMethods .POST , entity = entity))
60
62
}
61
63
62
- val response = Await .result(responseFuture, 10 seconds)
64
+ Try (Await .result(responseFuture, Duration (config.timeout.getOrElse(searchTimeout) + " seconds" ))).
65
+ map(response => parseResponse(response, config, start)(ec, materializer)).
66
+ recover {
67
+ case e : TimeoutException => {
68
+ error(config)(" The query timed out after " + (System .nanoTime() - start).nanos.toUnit(TimeUnit .SECONDS ) +
69
+ " seconds. To set a longer timeout, use the --timeout option." )
70
+ Failure (e)
71
+ }
72
+ }
73
+ }
74
+
75
+ private def parseResponse (response : HttpResponse , config : Config , start : Long )
76
+ (implicit ec : ExecutionContextExecutor , materializer : ActorMaterializer ): Unit = {
77
+
63
78
val resultFuture : Future [String ] = response match {
64
79
case HttpResponse (StatusCodes .OK , headers, entity, _) =>
65
80
entity.dataBytes.runFold(ByteString (" " ))(_ ++ _).map { body =>
0 commit comments