Skip to content

Commit

Permalink
Adds db connectivity check to status (#150)
Browse files Browse the repository at this point in the history
* Adds db connectivity check to status

* Changes healthcheck implementation
  • Loading branch information
dufrannea authored and guillaumebort committed Aug 23, 2017
1 parent 4091d08 commit 0186ace
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 11 additions & 7 deletions core/src/main/scala/com/criteo/cuttle/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ private[cuttle] case class App[S <: Scheduling](project: CuttleProject[S], execu

val publicApi: PartialService = {

case GET at url"/api/status" =>
Ok(
Json.obj(
"project" -> project.name.asJson,
"version" -> Option(project.version).filterNot(_.isEmpty).asJson,
"status" -> "ok".asJson
))
case GET at url"/api/status" => {
val projectJson = (status : String) => Json.obj(
"project" -> project.name.asJson,
"version" -> Option(project.version).filterNot(_.isEmpty).asJson,
"status" -> status.asJson
)
executor.healthCheck() match {
case Success(_) => Ok(projectJson("ok"))
case _ => InternalServerError(projectJson("ko"))
}
}

case GET at url"/api/statistics?events=$events&jobs=$jobs" =>
val filteredJobs = Try(jobs.split(",").toSeq.filter(_.nonEmpty)).toOption
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/com/criteo/cuttle/Database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,10 @@ private[cuttle] trait Queries {
new ExecutionStat(startTime, endTime, durationSeconds, waitingSeconds, status)
})
}

val healthCheck : ConnectionIO[Boolean] =
sql"""select 1 from dual"""
.query[Boolean]
.unique
}

5 changes: 4 additions & 1 deletion core/src/main/scala/com/criteo/cuttle/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.{Timer, TimerTask}
import java.util.concurrent.atomic.AtomicBoolean
import java.time.{Duration, Instant, ZoneId}

import scala.util.{Failure, Success}
import scala.util.{Failure, Success, Try}
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.stm._
Expand Down Expand Up @@ -570,4 +570,7 @@ class Executor[S <: Scheduling] private[cuttle] (
.foreach({ case (e, _) => e.updateWaitingTime(intervalSeconds) })
}
}

private[cuttle] def healthCheck(): Try[Boolean] =
Try(queries.healthCheck.transact(xa).unsafePerformIO)
}

0 comments on commit 0186ace

Please sign in to comment.