16
16
17
17
package de .upb .cs .swt .delphi .cli .commands
18
18
19
- import java .util .concurrent .TimeoutException
20
- import java .util .concurrent .TimeUnit
19
+ import java .util .concurrent .{TimeUnit , TimeoutException }
21
20
22
21
import akka .actor .ActorSystem
23
22
import akka .http .scaladsl .Http
@@ -33,10 +32,13 @@ import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
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
+ final var SEARCH_TIMEOUT : Int = 10
41
+
40
42
/**
41
43
* Executes the command implementation
42
44
*
@@ -59,47 +61,52 @@ 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
- try {
63
- val response = Await .result(responseFuture, Duration (config.timeout + " seconds" ))
64
- val resultFuture : Future [String ] = response match {
65
- case HttpResponse (StatusCodes .OK , headers, entity, _) =>
66
- entity.dataBytes.runFold(ByteString (" " ))(_ ++ _).map { body =>
67
- body.utf8String
68
- }
69
- case resp@ HttpResponse (code, _, _, _) => {
70
- error(config)(" Request failed, response code: " + code)
71
- resp.discardEntityBytes()
72
- Future (" " )
64
+ Try (Await .result(responseFuture, Duration (config.timeout.getOrElse(SEARCH_TIMEOUT ) + " 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)
73
71
}
74
72
}
73
+ }
75
74
76
- val result = Await .result(resultFuture, Duration .Inf )
77
-
78
- val took = (System .nanoTime() - start).nanos.toUnit(TimeUnit .SECONDS )
75
+ private def parseResponse (response : HttpResponse , config : Config , start : Long )
76
+ (implicit ec : ExecutionContextExecutor , materializer : ActorMaterializer ): Unit = {
79
77
80
- if (config.raw || result.equals(" " )) {
81
- reportResult(config)(result)
78
+ val resultFuture : Future [String ] = response match {
79
+ case HttpResponse (StatusCodes .OK , headers, entity, _) =>
80
+ entity.dataBytes.runFold(ByteString (" " ))(_ ++ _).map { body =>
81
+ body.utf8String
82
+ }
83
+ case resp@ HttpResponse (code, _, _, _) => {
84
+ error(config)(" Request failed, response code: " + code)
85
+ resp.discardEntityBytes()
86
+ Future (" " )
82
87
}
88
+ }
89
+
90
+ val result = Await .result(resultFuture, Duration .Inf )
83
91
84
- if (! (config.raw || result.equals(" " )) || ! config.csv.equals(" " )) {
85
- val unmarshalledFuture = Unmarshal (result).to[List [SearchResult ]]
86
-
87
- val processFuture = unmarshalledFuture.transform {
88
- case Success (unmarshalled) => {
89
- processResults(config, unmarshalled, took)
90
- Success (unmarshalled)
91
- }
92
- case Failure (e) => {
93
- error(config)(result)
94
- Failure (e)
95
- }
92
+ val took = (System .nanoTime() - start).nanos.toUnit(TimeUnit .SECONDS )
93
+
94
+ if (config.raw || result.equals(" " )) {
95
+ reportResult(config)(result)
96
+ }
97
+
98
+ if (! (config.raw || result.equals(" " )) || ! config.csv.equals(" " )) {
99
+ val unmarshalledFuture = Unmarshal (result).to[List [SearchResult ]]
100
+
101
+ val processFuture = unmarshalledFuture.transform {
102
+ case Success (unmarshalled) => {
103
+ processResults(config, unmarshalled, took)
104
+ Success (unmarshalled)
105
+ }
106
+ case Failure (e) => {
107
+ error(config)(result)
108
+ Failure (e)
96
109
}
97
- }
98
- } catch {
99
- case e : TimeoutException => {
100
- error(config)(" The query timed out after " + (System .nanoTime() - start).nanos.toUnit(TimeUnit .SECONDS ) +
101
- " seconds. To set a longer timeout, use the --timeout option." )
102
- Failure (e)
103
110
}
104
111
}
105
112
}
0 commit comments