Scala Akka中的Mallet Api投掷错误-请求[GET / mallet Empty]遇到请求超时

时间:2019-05-26 17:57:15

标签: scala akka mallet

如果这个问题听起来很幼稚,请原谅我,因为我在阿卡(akka)还很新。

我试图在Rest API的Scala Akka中使用槌状API 但是遇到错误请求超时

下面是我的服务器的快照

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)

路由器快照

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)
          }
          }
        }
    }

最后是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.")

我在调用text2Vector.main时遇到错误,正在new_corpus目录中创建了新的矢量化文件,并且也在生成了new_corpus。但是之后,我得到了以下错误

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]

在网络浏览器中,我也收到错误消息

The server was not able to produce a timely response to your request.
Please try again in a short while!

1 个答案:

答案 0 :(得分:0)

我自己找出解决方案。如果仍然不清楚,请纠正我。

需要进行一些更改。 首先进入应用程序配置文件(application.conf)

下面的代码需要更新。

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

该问题需要在服务器代码中处理 通过将api调用放入超时响应中

  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)
          }
          }

          }
        }
    }