Created
April 5, 2020 07:15
-
-
Save obszczymucha/cc166ee3e1da9f951724ffbac67ff00b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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