Skip to content

TimeoutException in search #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/scala/de/upb/cs/swt/delphi/cli/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ case class Config(server: String = sys.env.getOrElse("DELPHI_SERVER", "https://d
query : String = "",
limit : Option[Int] = None,
id : String = "",
timeout : Option[Int] = None,
args: List[String] = List(),
opts: List[String] = List()) {

Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package de.upb.cs.swt.delphi.cli

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import de.upb.cs.swt.delphi.cli.commands.{RetrieveCommand, SearchCommand, TestCommand}

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

cmd("search").action((s, c) => c.copy(mode = "search"))
.text("Search artifact using a query.")
.children(
arg[String]("query").action((x,c) => c.copy(query = x)).text("The query to be used."),
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)"),
opt[Int]("limit").action((x, c) => c.copy(limit = Some(x))).text("The maximal number of results returned."),
opt[Unit](name="list").action((_, c) => c.copy(list = true)).text("Output results as list (raw option overrides this)"),
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)")
opt[Int]("timeout").action((x, c) => c.copy(timeout = Some(x))).text("Timeout in seconds.")
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

import java.util.concurrent.TimeUnit
import java.util.concurrent.{TimeUnit, TimeoutException}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
Expand All @@ -29,14 +29,16 @@ import akka.util.ByteString
import de.upb.cs.swt.delphi.cli.Config
import de.upb.cs.swt.delphi.cli.artifacts.SearchResult
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
import de.upb.cs.swt.delphi.cli.commands.RetrieveCommand.information
import spray.json.DefaultJsonProtocol

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success}
import scala.concurrent.{Await, ExecutionContextExecutor, Future}
import scala.util.{Failure, Success, Try}

object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {

val searchTimeout: Int = 10

/**
* Executes the command implementation
*
Expand All @@ -59,7 +61,20 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
Http().singleRequest(HttpRequest(uri = searchUri, method = HttpMethods.POST, entity = entity))
}

val response = Await.result(responseFuture, 10 seconds)
Try(Await.result(responseFuture, Duration(config.timeout.getOrElse(searchTimeout) + " seconds"))).
map(response => parseResponse(response, config, start)(ec, materializer)).
recover {
case e : TimeoutException => {
error(config)("The query timed out after " + (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS) +
" seconds. To set a longer timeout, use the --timeout option.")
Failure(e)
}
}
}

private def parseResponse(response: HttpResponse, config: Config, start: Long)
(implicit ec: ExecutionContextExecutor, materializer: ActorMaterializer): Unit = {

val resultFuture: Future[String] = response match {
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
entity.dataBytes.runFold(ByteString(""))(_ ++ _).map { body =>
Expand Down