From a00ec98d05334d795249c798b2e89654b73d3847 Mon Sep 17 00:00:00 2001 From: Arnaud Dufranne Date: Mon, 21 Aug 2017 11:20:27 +0200 Subject: [PATCH 1/2] Adds db connectivity check to status --- core/src/main/scala/com/criteo/cuttle/App.scala | 16 ++++++++++------ .../main/scala/com/criteo/cuttle/Database.scala | 5 +++++ .../main/scala/com/criteo/cuttle/Executor.scala | 6 ++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/com/criteo/cuttle/App.scala b/core/src/main/scala/com/criteo/cuttle/App.scala index 01d958c25..cac8854a5 100644 --- a/core/src/main/scala/com/criteo/cuttle/App.scala +++ b/core/src/main/scala/com/criteo/cuttle/App.scala @@ -142,12 +142,16 @@ 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 - )) + executor.healthCheck + .fold( + t => InternalServerError(t.getMessage()), + _ => Ok( + Json.obj( + "project" -> project.name.asJson, + "version" -> Option(project.version).filterNot(_.isEmpty).asJson, + "status" -> "ok".asJson + )) + ) case GET at url"/api/statistics?events=$events&jobs=$jobs" => val filteredJobs = Try(jobs.split(",").toSeq.filter(_.nonEmpty)).toOption diff --git a/core/src/main/scala/com/criteo/cuttle/Database.scala b/core/src/main/scala/com/criteo/cuttle/Database.scala index 94445b3d3..45fbaa7a0 100644 --- a/core/src/main/scala/com/criteo/cuttle/Database.scala +++ b/core/src/main/scala/com/criteo/cuttle/Database.scala @@ -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 } diff --git a/core/src/main/scala/com/criteo/cuttle/Executor.scala b/core/src/main/scala/com/criteo/cuttle/Executor.scala index 8434c7b3e..c41835261 100644 --- a/core/src/main/scala/com/criteo/cuttle/Executor.scala +++ b/core/src/main/scala/com/criteo/cuttle/Executor.scala @@ -570,4 +570,10 @@ class Executor[S <: Scheduling] private[cuttle] ( .foreach({ case (e, _) => e.updateWaitingTime(intervalSeconds) }) } } + + private[cuttle] def healthCheck(): Either[Throwable, Boolean] = { + import fs2.interop.cats._ + + queries.healthCheck.attempt.transact(xa).unsafePerformIO + } } From d3fede1ccb2f08baa04b16a17ab7a0df1128553a Mon Sep 17 00:00:00 2001 From: Arnaud Dufranne Date: Tue, 22 Aug 2017 11:29:32 +0200 Subject: [PATCH 2/2] Changes healthcheck implementation --- .../main/scala/com/criteo/cuttle/App.scala | 22 +++++++++---------- .../scala/com/criteo/cuttle/Database.scala | 6 ++--- .../scala/com/criteo/cuttle/Executor.scala | 9 +++----- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/core/src/main/scala/com/criteo/cuttle/App.scala b/core/src/main/scala/com/criteo/cuttle/App.scala index cac8854a5..4fd9dae59 100644 --- a/core/src/main/scala/com/criteo/cuttle/App.scala +++ b/core/src/main/scala/com/criteo/cuttle/App.scala @@ -141,17 +141,17 @@ private[cuttle] case class App[S <: Scheduling](project: CuttleProject[S], execu val publicApi: PartialService = { - case GET at url"/api/status" => - executor.healthCheck - .fold( - t => InternalServerError(t.getMessage()), - _ => 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 diff --git a/core/src/main/scala/com/criteo/cuttle/Database.scala b/core/src/main/scala/com/criteo/cuttle/Database.scala index 45fbaa7a0..28d5d83e0 100644 --- a/core/src/main/scala/com/criteo/cuttle/Database.scala +++ b/core/src/main/scala/com/criteo/cuttle/Database.scala @@ -229,8 +229,8 @@ private[cuttle] trait Queries { } val healthCheck : ConnectionIO[Boolean] = - sql"""select 1 from dual""" - .query[Boolean] - .unique + sql"""select 1 from dual""" + .query[Boolean] + .unique } diff --git a/core/src/main/scala/com/criteo/cuttle/Executor.scala b/core/src/main/scala/com/criteo/cuttle/Executor.scala index c41835261..1a13622d4 100644 --- a/core/src/main/scala/com/criteo/cuttle/Executor.scala +++ b/core/src/main/scala/com/criteo/cuttle/Executor.scala @@ -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._ @@ -571,9 +571,6 @@ class Executor[S <: Scheduling] private[cuttle] ( } } - private[cuttle] def healthCheck(): Either[Throwable, Boolean] = { - import fs2.interop.cats._ - - queries.healthCheck.attempt.transact(xa).unsafePerformIO - } + private[cuttle] def healthCheck(): Try[Boolean] = + Try(queries.healthCheck.transact(xa).unsafePerformIO) }