0

Please pardon me if the question sound naive as i am pretty new in akka.

I am trying to use the mallet api in scala akka in rest API But getting error Request timeout encountered

below is the snapshot of my Server

Configuration.parser.parse(args,Configuration.ConfigurationOptions()) match {
        case Some(config) =>
          val serverBinding: Future[Http.ServerBinding] = Http().bindAndHandle(routes, config.interface, config.port)
          serverBinding.onComplete {
            case Success(bound) =>
              println(s"Server online at http://${bound.localAddress.getHostString}:${bound.localAddress.getPort}/")
            case Failure(e) =>
              log.error("Server could not start: ", e)
              system.terminate()
          }
        case None =>
          system.terminate()
      }

      Await.result(system.whenTerminated, Duration.Inf)

snapshot of Router

lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          parameter('malletFile) { malletFile =>
          {val modelFile: Future[MalletModel] =
            (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }
        }
    }

and finally the snapshot of MalletActor

class MalletActor(implicit val uaCache: Cache[String, MalletModel],
                  implicit val executionContext: ExecutionContext)
  extends Actor with ActorLogging with JsonSupport {

  import MalletActor._

  def receive: Receive = {
    case GetMalletOutput(malletFile) => sender() ! createMalletResult2(malletFile)
  }

  def createMalletResult2(malletFile: String): MalletModel = {

    logger.debug("run count...")
        val res = MalletResult(malletFile)
        val converted = res.Score.parseJson.convertTo[MalletRepo]
        val fileName = converted.ContentId

        val fileTemp = new File("src/main/resources/new_corpus/" + fileName)
        if (fileTemp.exists) {
          fileTemp.delete()
        }

        val output = new BufferedWriter(new FileWriter("src/main/resources/new_corpus/" + fileName))
        output.write(converted.ContentText)
        output.close()

    //runMalletInferring()

    val tmpDir = "src/main/resources/"
    logger.debug("Import all documents to mallet...")
    Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
    logger.debug("Run training process...")
    InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" "))
    logger.debug("Inferring process finished.")

I am the getting error while calling text2Vector.main , the new vectorized file is getting created in new_corpus directory and new_corpus is getting generated as well. however after that I am getting the below error

Server online at http://127.0.0.1:9000/
12:45:38.622 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - run count...
12:45:38.634 [Sophi-Mallet-Api-akka.actor.default-dispatcher-3] DEBUG io.sophi.api.mallet.actors.MalletActor$ - Import all documents to mallet...
Couldn't open cc.mallet.util.MalletLogger resources/logging.properties file.
 Perhaps the 'resources' directories weren't copied into the 'class' directory.
 Continuing.
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO: Labels = 
May 26, 2019 12:45:38 PM cc.mallet.classify.tui.Text2Vectors main
INFO:    src/main/resources/new_corpus/
May 26, 2019 12:45:46 PM cc.mallet.classify.tui.Text2Vectors main
INFO:  rewriting previous instance list, with ID = 4e0a5a65540221c3:d508579:14b2ca15a26:-7ff7
[INFO] [05/26/2019 12:45:58.476] [Sophi-Mallet-Api-akka.actor.default-dispatcher-4] [akka.actor.ActorSystemImpl(Sophi-Mallet-Api)] Request timeout encountered for request [GET /mallet Empty]

in web browser as well I am getting the error

The server was not able to produce a timely response to your request.
Please try again in a short while!
danD
  • 666
  • 1
  • 7
  • 29

1 Answers1

0

I figure out the solution myself. Please correct me if it still is vague.

There were couple of change needed. First in application configuration file (application.conf)

below code needed to be updated.

server{
    idle-timeout = infinite
    request-timeout = infinite
}
settings {
  akka-workers-count = 100
  akka-workers-count = ${?AKKA_WORKERS_COUNT}
  actor-timeout = 100
  actor-timeout = ${?ACTOR_TIMEOUT}
}

And the issue is needed to be handled in server code by putting the api call inside timeout response

  val timeoutResponse = HttpResponse(
    StatusCodes.EnhanceYourCalm,
    entity = "Running Mallet modelling.")

  lazy val apiRoutes: Route =
    ignoreTrailingSlash {
      pathSingleSlash {
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<html><body>Hello world!</body></html>"))
      } ~
        path("health") {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "ok"))
        } ~
        pathPrefix("mallet") {
          withRequestTimeout(5.minute, request => timeoutResponse) {
          parameter('malletFile) { malletFile => {
            val modelFile: Future[MalletModel] =
              (malletActor ? GetMalletOutput(malletFile)).mapTo[MalletModel]
            complete(modelFile)
          }
          }

          }
        }
    }
danD
  • 666
  • 1
  • 7
  • 29