Skip to content

Commit

Permalink
#1051 cache ES status to make /api/status more responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Apr 6, 2020
1 parent 40bb7d3 commit 846567e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions thehive-backend/app/controllers/StatusCtrl.scala
Original file line number Diff line number Diff line change
@@ -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(
Expand Down

0 comments on commit 846567e

Please sign in to comment.