From 7140172799e38fd3948c3dd04252218b620f0e53 Mon Sep 17 00:00:00 2001 From: Jonas Chapuis Date: Tue, 9 Jan 2024 17:53:12 +0100 Subject: [PATCH 1/2] Change RepositoryInterpreter to return a Resource So that finalizable operations required by the repository can be bundled with the overall repository finalization --- .../internal/ShardedRepositoryDeployer.scala | 18 ++++++++---------- .../core/interpret/RepositoryInterpreter.scala | 5 +++-- .../internal/ShardedRepositoryDeployer.scala | 18 ++++++++---------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/akka-runtime/src/main/scala/endless/runtime/akka/deploy/internal/ShardedRepositoryDeployer.scala b/akka-runtime/src/main/scala/endless/runtime/akka/deploy/internal/ShardedRepositoryDeployer.scala index 41789d0c..465f847f 100644 --- a/akka-runtime/src/main/scala/endless/runtime/akka/deploy/internal/ShardedRepositoryDeployer.scala +++ b/akka-runtime/src/main/scala/endless/runtime/akka/deploy/internal/ShardedRepositoryDeployer.scala @@ -35,16 +35,14 @@ trait ShardedRepositoryDeployer[F[_], RepositoryAlg[_[_]], Alg[_[_]], ID] { implicit val clusterSharding: ClusterSharding = akkaCluster.sharding implicit val commandSender: CommandSender[F, ID] = ShardingCommandSender.apply val repository = Sharding.apply[F, ID, Alg] - Resource - .eval(repositoryInterpreter(repository)) - .map(repository => { - implicit val dispatcher: Dispatcher[F] = akkaCluster.dispatcher - val akkaEntity = - akka.cluster.sharding.typed.scaladsl.Entity(entityTypeKey) { implicit context => - Behaviors.setup { implicit actor => createBehaviorFor(repository) } - } - (repository, akkaCluster.sharding.init(customizeEntity(akkaEntity))) - }) + repositoryInterpreter(repository).map(repository => { + implicit val dispatcher: Dispatcher[F] = akkaCluster.dispatcher + val akkaEntity = + akka.cluster.sharding.typed.scaladsl.Entity(entityTypeKey) { implicit context => + Behaviors.setup { implicit actor => createBehaviorFor(repository) } + } + (repository, akkaCluster.sharding.init(customizeEntity(akkaEntity))) + }) } protected def createBehaviorFor(repository: RepositoryAlg[F])(implicit diff --git a/core/src/main/scala/endless/core/interpret/RepositoryInterpreter.scala b/core/src/main/scala/endless/core/interpret/RepositoryInterpreter.scala index 1765e23a..ea366da3 100644 --- a/core/src/main/scala/endless/core/interpret/RepositoryInterpreter.scala +++ b/core/src/main/scala/endless/core/interpret/RepositoryInterpreter.scala @@ -1,6 +1,7 @@ package endless.core.interpret import cats.Applicative +import cats.effect.kernel.Resource import endless.core.entity.Sharding /** Interpret an algebra `RepositoryAlg` expressed using `Sharding` in context `F`, materializing @@ -18,7 +19,7 @@ import endless.core.entity.Sharding * interpreted repository algebra in context `F` */ trait RepositoryInterpreter[F[_], ID, Alg[_[_]], RepositoryAlg[_[_]]] { - def apply(sharding: Sharding[F, ID, Alg]): F[RepositoryAlg[F]] + def apply(sharding: Sharding[F, ID, Alg]): Resource[F, RepositoryAlg[F]] } object RepositoryInterpreter { @@ -40,5 +41,5 @@ object RepositoryInterpreter { def lift[F[_]: Applicative, ID, Alg[_[_]], RepositoryAlg[_[_]]]( pureInterpreter: Sharding[F, ID, Alg] => RepositoryAlg[F] ): RepositoryInterpreter[F, ID, Alg, RepositoryAlg] = (sharding: Sharding[F, ID, Alg]) => - Applicative[F].pure(pureInterpreter(sharding)) + Resource.pure(pureInterpreter(sharding)) } diff --git a/pekko-runtime/src/main/scala/endless/runtime/pekko/deploy/internal/ShardedRepositoryDeployer.scala b/pekko-runtime/src/main/scala/endless/runtime/pekko/deploy/internal/ShardedRepositoryDeployer.scala index 9f7589e8..19e55713 100644 --- a/pekko-runtime/src/main/scala/endless/runtime/pekko/deploy/internal/ShardedRepositoryDeployer.scala +++ b/pekko-runtime/src/main/scala/endless/runtime/pekko/deploy/internal/ShardedRepositoryDeployer.scala @@ -44,16 +44,14 @@ trait ShardedRepositoryDeployer[F[_], RepositoryAlg[_[_]], Alg[_[_]], ID] { implicit val clusterSharding: ClusterSharding = pekkoCluster.sharding implicit val commandSender: CommandSender[F, ID] = ShardingCommandSender.apply val repositoryT = Sharding.apply[F, ID, Alg] - Resource - .eval(repositoryInterpreter(repositoryT)) - .map(repository => { - implicit val dispatcher: Dispatcher[F] = pekkoCluster.dispatcher - val pekkoEntity = - org.apache.pekko.cluster.sharding.typed.scaladsl.Entity(entityTypeKey) { - implicit context => Behaviors.setup { implicit actor => createBehaviorFor(repository) } - } - (repository, pekkoCluster.sharding.init(customizeEntity(pekkoEntity))) - }) + repositoryInterpreter(repositoryT).map(repository => { + implicit val dispatcher: Dispatcher[F] = pekkoCluster.dispatcher + val pekkoEntity = + org.apache.pekko.cluster.sharding.typed.scaladsl.Entity(entityTypeKey) { implicit context => + Behaviors.setup { implicit actor => createBehaviorFor(repository) } + } + (repository, pekkoCluster.sharding.init(customizeEntity(pekkoEntity))) + }) } protected def createBehaviorFor(repositoryAlg: RepositoryAlg[F])(implicit From 1e2ae75f227abd78009f886fca2c77278586b4b3 Mon Sep 17 00:00:00 2001 From: Jonas Chapuis Date: Wed, 10 Jan 2024 11:17:07 +0100 Subject: [PATCH 2/2] Remove unnecessary log entry in application.conf --- example/src/main/resources/application.conf | 5 ----- 1 file changed, 5 deletions(-) diff --git a/example/src/main/resources/application.conf b/example/src/main/resources/application.conf index 14891f6c..9ae1b8d6 100644 --- a/example/src/main/resources/application.conf +++ b/example/src/main/resources/application.conf @@ -1,9 +1,4 @@ include "akka.conf" include "pekko.conf" -cluster { - system-name = "example-as" - host = "127.0.0.1" - port = 51000 -}