diff --git a/akka-persistence/src/main/scala/iw/akka/AkkaActorSystem.scala b/akka-persistence/src/main/scala/iw/akka/AkkaActorSystem.scala index 393bfe5..f61fe0f 100644 --- a/akka-persistence/src/main/scala/iw/akka/AkkaActorSystem.scala +++ b/akka-persistence/src/main/scala/iw/akka/AkkaActorSystem.scala @@ -13,16 +13,22 @@ cluster.manager ! Join(cluster.selfMember.address) } + val terminate: UIO[Unit] = ZIO.attempt(system.terminate()).orDie + given ActorSystem[?] = system object AkkaActorSystem: - def empty(name: String): ZLayer[Scope, Throwable, AkkaActorSystem] = - ZLayer( + private def makeEmpty(name: String): ZIO[Scope, Throwable, AkkaActorSystem] = + ZIO.acquireRelease( ZIO - .acquireRelease( - ZIO.attempt(ActorSystem(Behaviors.empty[NotUsed], name)) - )(system => ZIO.attempt(system.terminate()).orDie) - .map( - AkkaActorSystem(_) - ) - ) + .attempt(ActorSystem(Behaviors.empty[NotUsed], name)) + .map(AkkaActorSystem(_)) + )(_.terminate) + + def empty(name: String): ZLayer[Scope, Throwable, AkkaActorSystem] = + ZLayer(makeEmpty(name)) + + def emptySingleNodeCluster( + name: String + ): ZLayer[Scope, Throwable, AkkaActorSystem] = + ZLayer(makeEmpty(name).tap(_.joinSelf))