Skip to content

Commit 6ee305e

Browse files
authored
Merge pull request #30 from delphi-hub/search-timeout
TimeoutException in search
2 parents 655272a + fb14f37 commit 6ee305e

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/main/scala/de/upb/cs/swt/delphi/cli/Config.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ case class Config(server: String = sys.env.getOrElse("DELPHI_SERVER", "https://d
3333
query : String = "",
3434
limit : Option[Int] = None,
3535
id : String = "",
36+
timeout : Option[Int] = None,
3637
args: List[String] = List(),
3738
opts: List[String] = List()) {
3839

src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package de.upb.cs.swt.delphi.cli
1818

1919
import akka.actor.ActorSystem
2020
import akka.http.scaladsl.Http
21-
import akka.stream.ActorMaterializer
2221
import de.upb.cs.swt.delphi.cli.commands.{RetrieveCommand, SearchCommand, TestCommand}
2322

2423
import scala.concurrent.duration.Duration
@@ -53,18 +52,19 @@ object DelphiCLI extends App {
5352
.text("Retrieve a project's description, specified by ID.")
5453
.children(
5554
arg[String]("id").action((x, c) => c.copy(id = x)).text("The ID of the project to retrieve"),
55+
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)"),
5656
opt[Unit]('f', "file").action((_, c) => c.copy(opts = List("file"))).text("Use to load the ID from file, " +
57-
"with the filepath given in place of the ID"),
58-
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)")
57+
"with the filepath given in place of the ID")
5958
)
6059

6160
cmd("search").action((s, c) => c.copy(mode = "search"))
6261
.text("Search artifact using a query.")
6362
.children(
6463
arg[String]("query").action((x,c) => c.copy(query = x)).text("The query to be used."),
64+
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)"),
6565
opt[Int]("limit").action((x, c) => c.copy(limit = Some(x))).text("The maximal number of results returned."),
6666
opt[Unit](name="list").action((_, c) => c.copy(list = true)).text("Output results as list (raw option overrides this)"),
67-
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)")
67+
opt[Int]("timeout").action((x, c) => c.copy(timeout = Some(x))).text("Timeout in seconds.")
6868
)
6969
}
7070
}

src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package de.upb.cs.swt.delphi.cli.commands
1818

19-
import java.util.concurrent.TimeUnit
19+
import java.util.concurrent.{TimeUnit, TimeoutException}
2020

2121
import akka.actor.ActorSystem
2222
import akka.http.scaladsl.Http
@@ -29,14 +29,16 @@ import akka.util.ByteString
2929
import de.upb.cs.swt.delphi.cli.Config
3030
import de.upb.cs.swt.delphi.cli.artifacts.SearchResult
3131
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
32-
import de.upb.cs.swt.delphi.cli.commands.RetrieveCommand.information
3332
import spray.json.DefaultJsonProtocol
3433

3534
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}
3837

3938
object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
39+
40+
val searchTimeout: Int = 10
41+
4042
/**
4143
* Executes the command implementation
4244
*
@@ -59,7 +61,20 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
5961
Http().singleRequest(HttpRequest(uri = searchUri, method = HttpMethods.POST, entity = entity))
6062
}
6163

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+
6378
val resultFuture: Future[String] = response match {
6479
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
6580
entity.dataBytes.runFold(ByteString(""))(_ ++ _).map { body =>

0 commit comments

Comments
 (0)