首页 > 解决方案 > scala Akka中的Mallet Api抛出错误-请求遇到请求超时[GET /mallet Empty]

问题描述

如果这个问题听起来很幼稚,请原谅我,因为我是 akka 的新手。

我正在尝试在rest API中使用scala akka中的mallet 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!

标签: scalaakkamallet

解决方案


我自己想出了解决方案。如果仍然含糊不清,请纠正我。

需要进行一些更改。首先在应用程序配置文件(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)
          }
          }

          }
        }
    }

推荐阅读