如何使用 reactivemongo 检查 MongoDB 是否启动和 运行

How to check if MongoDB is up and running using reactivemongo

我正在尝试编写一个辅助函数,让我的仪表板检查 MongoDB 是否为 运行。根据结果​​,它可以在可视化仪表板上警告管理员。我通读了反应式 mongo 文档 here,到目前为止我有以下功能,但不幸的是它不起作用。

import reactivemongo.api.MongoDriver

def isMongoUp(host: String): Boolean = {

  var connected = false
  val driver = new MongoDriver()
  val connection = driver.connection(List(host))
  val db = connection("myDB")
  db.map {
    case Failure(e) => connected = false
    case Success(result) => connected = true
  }

}

更新 1:

def isMongoUp(host: String): Boolean = {
  var connected = false
  val driver = new MongoDriver()
  val connection = driver.connection(List(host))
  val db = connection.database("myDB")
  db.map {
    case sth:DefaultDB => connected = true
    case _ => connected = false
  }
  connected

}

val result = isMongoUp("localhost")
result

我是这样完成的:

def isMongoUp(host: String): Boolean = {

    val driver = new MongoDriver()
    val connection = driver.connection(List(host))
    val db = connection.database("myDB")
    val connectedFuture: Future[Boolean] = db.map { list =>
      list match {
        case sth: DefaultDB => true
        case _ => false
      }
    }.recover { case error: Throwable => false}
    val timeout = scala.concurrent.duration.Duration(5, "seconds")

    val result = Await.result(connectedFuture, timeout)
    result

  }