我正在尝试编写一个帮助函数,它允许我的仪表板检查MongoDB是否正在运行。根据结果,它可以在可视化仪表板上警告管理员。我通读了反应性mongo docs 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
答案 0 :(得分:-1)
我这样做了:
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
}