Skip to content

Instantly share code, notes, and snippets.

@obszczymucha
Created April 5, 2020 07:15
Show Gist options
  • Save obszczymucha/cc166ee3e1da9f951724ffbac67ff00b to your computer and use it in GitHub Desktop.
Save obszczymucha/cc166ee3e1da9f951724ffbac67ff00b to your computer and use it in GitHub Desktop.
case object ClusterDown
case class ClusterUp(status: String)
trait ElasticSearchService[F[_]] {
def clusterHealth: F[Either[ClusterDown.type, ClusterUp]]
}
object ElasticSearchService {
private[this] val log = getLogger.logger
def impl[F[_] : Sync](client: RestHighLevelClient): ElasticSearchService[F] = new ElasticSearchService[F] {
override def clusterHealth: F[Either[ClusterDown.type, ClusterUp]] = {
for {
request <- Sync[F].pure(new ClusterHealthRequest("question", "answer"))
response <- Sync[F].delay(client.cluster().health(request, RequestOptions.DEFAULT)) // <- This will make an actual call to ElasticSearch cluster, how do I test this?
.attempt
.onError(t => logError(t)) // Is this a good approach to log errors?
} yield response.map(r => ClusterUp(r.getStatus.toString)).leftMap(_ => ClusterDown)
}
private def logError(t: Throwable) = Sync[F].delay(log.error("ElasticSearch error.", t))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment