Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ESClient creation #241

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ lazy val core = project
publish := {},
publish / skip := true,
libraryDependencies ++= Seq(
elastic4sJavaClient,
catsCore,
catsEffect,
elastic4sJavaClient,
elastic4sCatsEffect,
"joda-time" % "joda-time" % "2.12.7"
)
)
Expand All @@ -45,7 +47,6 @@ lazy val ingestor = (project in file("modules/ingestor"))
publish := {},
publish / skip := true,
libraryDependencies ++= Seq(
elastic4sCatsEffect,
catsCore,
fs2,
fs2IO,
Expand Down Expand Up @@ -88,7 +89,6 @@ lazy val app = (project in file("modules/app"))
jsoniterCore,
jsoniterMacro,
smithy4sHttp4sSwagger,
elastic4sCatsEffect,
catsCore,
catsEffect,
ducktape,
Expand Down
11 changes: 1 addition & 10 deletions modules/app/src/main/scala/app.resources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ package lila.search
package app

import cats.effect.*
import com.sksamuel.elastic4s.cats.effect.instances.*
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.{ ElasticClient, ElasticProperties }
import org.typelevel.log4cats.Logger

class AppResources(val esClient: ESClient[IO])

object AppResources:

def instance(conf: AppConfig)(using Logger[IO]): Resource[IO, AppResources] =
makeClient(conf.elastic)
.map(ESClient.apply[IO])
.map(AppResources(_))

def makeClient(conf: ElasticConfig): Resource[IO, ElasticClient] =
Resource.make(IO(ElasticClient(JavaClient(ElasticProperties(conf.uri))))): client =>
IO(client.close())
ESClient.apply(conf.elastic.uri).map(AppResources.apply)
9 changes: 9 additions & 0 deletions modules/core/src/main/scala/ESClient.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package lila.search

import cats.MonadThrow
import cats.effect.*
import cats.syntax.all.*
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.cats.effect.instances.*
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.{
ElasticClient,
ElasticDsl,
ElasticProperties,
Executor,
Functor,
Index as ESIndex,
Expand All @@ -31,6 +35,11 @@ trait ESClient[F[_]]:

object ESClient:

def apply(uri: String): Resource[IO, ESClient[IO]] =
Resource
.make(IO(ElasticClient(JavaClient(ElasticProperties(uri)))))(client => IO(client.close()))
.map(ESClient.apply[IO])

def apply[F[_]: MonadThrow: Functor: Executor](client: ElasticClient) = new ESClient[F]:

def status: F[String] =
Expand Down
7 changes: 1 addition & 6 deletions modules/ingestor/src/main/scala/app.resources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package ingestor
import cats.effect.{ IO, Resource }
import cats.syntax.all.*
import com.mongodb.ReadPreference
import com.sksamuel.elastic4s.cats.effect.instances.*
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.{ ElasticClient, ElasticProperties }
import mongo4cats.client.MongoClient
import mongo4cats.database.MongoDatabase
import org.typelevel.log4cats.Logger
Expand All @@ -20,9 +17,7 @@ object AppResources:
.parMapN(AppResources.apply)

def makeElasticClient(conf: ElasticConfig) =
Resource
.make(IO(ElasticClient(JavaClient(ElasticProperties(conf.uri)))))(client => IO(client.close()))
.map(ESClient.apply[IO])
ESClient.apply(conf.uri)

def makeMongoClient(conf: MongoConfig) =
MongoClient
Expand Down
Loading