From 846567e10c9a25bb6ecffd1e5732c33e2fbea8d3 Mon Sep 17 00:00:00 2001 From: To-om Date: Wed, 24 Jul 2019 17:33:50 +0200 Subject: [PATCH] #1051 cache ES status to make /api/status more responsive --- thehive-backend/app/controllers/StatusCtrl.scala | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/thehive-backend/app/controllers/StatusCtrl.scala b/thehive-backend/app/controllers/StatusCtrl.scala index 2f9f698798..4466a847ec 100644 --- a/thehive-backend/app/controllers/StatusCtrl.scala +++ b/thehive-backend/app/controllers/StatusCtrl.scala @@ -1,40 +1,48 @@ package controllers +import akka.actor.ActorSystem + import scala.collection.immutable import scala.concurrent.ExecutionContext import scala.util.Try - import play.api.Configuration import play.api.libs.json.Json.toJsFieldJsValueWrapper import play.api.libs.json.{JsBoolean, JsObject, JsString, Json} import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents} - import com.sksamuel.elastic4s.http.ElasticDsl import connectors.Connector import javax.inject.{Inject, Singleton} import models.HealthStatus import org.elasticsearch.client.Node - import org.elastic4play.Timed import org.elastic4play.database.DBIndex import org.elastic4play.services.AuthSrv import org.elastic4play.services.auth.MultiAuthSrv +import scala.concurrent.duration.{DurationInt, FiniteDuration} + @Singleton class StatusCtrl @Inject()( connectors: immutable.Set[Connector], configuration: Configuration, dbIndex: DBIndex, authSrv: AuthSrv, + system: ActorSystem, components: ControllerComponents, implicit val ec: ExecutionContext ) extends AbstractController(components) { private[controllers] def getVersion(c: Class[_]) = Option(c.getPackage.getImplementationVersion).getOrElse("SNAPSHOT") + private var clusterStatusName: String = "Init" + val checkStatusInterval: FiniteDuration = configuration.getOptional[FiniteDuration]("statusCheckInterval").getOrElse(1.minute) + private def updateStatus(): Unit = { + clusterStatusName = Try(dbIndex.clusterStatusName).getOrElse("ERROR") + system.scheduler.scheduleOnce(checkStatusInterval)(updateStatus()) + } + updateStatus() @Timed("controllers.StatusCtrl.get") def get: Action[AnyContent] = Action { - val clusterStatusName = Try(dbIndex.clusterStatusName).getOrElse("ERROR") Ok( Json.obj( "versions" → Json.obj(