Skip to content

Commit

Permalink
refactor: Simplify ActorSystem layer (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Mar 15, 2024
1 parent 564bdcf commit 292f7eb
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 62 deletions.
Expand Up @@ -5,11 +5,9 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import zio.*

object ActorSystemTest {

def layer(sys: pekko.actor.ActorSystem): ZLayer[Any, Nothing, ActorSystem] =
ZLayer.scoped(ZIO.succeed(new ActorSystem { override val system: pekko.actor.ActorSystem = sys }))
def layer(sys: ActorSystem): ZLayer[Any, Nothing, ActorSystem] = ZLayer.succeed(sys)
}
Expand Up @@ -85,7 +85,11 @@ object LayersTest {
with DspIngestTestContainer
with SharedVolumes.Images

type CommonR0 = ActorSystem with AppConfigurationsTest with JwtService with SipiService with StringFormatter
type CommonR0 = pekko.actor.ActorSystem
with AppConfigurationsTest
with JwtService
with SipiService
with StringFormatter
type CommonR =
ApiRoutes
with AdminApiEndpoints
Expand Down
Expand Up @@ -19,6 +19,7 @@ import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.util.EntityUtils
import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import spray.json.JsObject
import spray.json.*
import zio.*
Expand All @@ -33,7 +34,6 @@ import dsp.errors.AssertionException
import dsp.errors.BadRequestException
import dsp.errors.NotFoundException
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.ActorSystem
import org.knora.webapi.messages.store.sipimessages.SipiUploadResponse
import org.knora.webapi.messages.store.sipimessages.SipiUploadResponseJsonProtocol.*
import org.knora.webapi.messages.store.sipimessages.SipiUploadWithoutProcessingResponse
Expand Down Expand Up @@ -65,11 +65,10 @@ final case class FileToUpload(path: Path, mimeType: ContentType)
*/
final case class InputFile(fileToUpload: FileToUpload, width: Int, height: Int)

final case class TestClientService(config: AppConfig, httpClient: CloseableHttpClient, sys: pekko.actor.ActorSystem)
final case class TestClientService(config: AppConfig, httpClient: CloseableHttpClient)(implicit system: ActorSystem)
extends TriplestoreJsonProtocol
with RequestBuilding {

implicit val system: pekko.actor.ActorSystem = sys
implicit val executionContext: ExecutionContext = system.dispatchers.lookup(KnoraDispatchers.KnoraBlockingDispatcher)

case class TestClientTimeoutException(msg: String) extends Exception
Expand Down Expand Up @@ -356,7 +355,7 @@ object TestClientService {
/**
* Releases the httpClient, freeing all resources.
*/
private def release(httpClient: CloseableHttpClient)(implicit system: pekko.actor.ActorSystem) = ZIO.attemptBlocking {
private def release(httpClient: CloseableHttpClient)(implicit system: ActorSystem) = ZIO.attemptBlocking {
pekko.http.scaladsl.Http().shutdownAllConnectionPools()
httpClient.close()
}.tap(_ => ZIO.logDebug(">>> Release Test Client Service <<<")).orDie
Expand All @@ -366,8 +365,8 @@ object TestClientService {
for {
sys <- ZIO.service[ActorSystem]
config <- ZIO.service[AppConfig]
httpClient <- ZIO.acquireRelease(acquire)(release(_)(sys.system))
} yield TestClientService(config, httpClient, sys.system)
httpClient <- ZIO.acquireRelease(acquire)(release(_)(sys))
} yield TestClientService(config, httpClient)(sys)
}

}
40 changes: 10 additions & 30 deletions webapi/src/main/scala/org/knora/webapi/core/ActorSystem.scala
Expand Up @@ -5,47 +5,27 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor
import zio.*
import zio.macros.accessible

import scala.concurrent.ExecutionContext

import pekko.actor

@accessible
trait ActorSystem {
val system: pekko.actor.ActorSystem
}

object ActorSystem {

private def acquire(executionContext: ExecutionContext): URIO[Any, actor.ActorSystem] =
ZIO
.attempt(
pekko.actor.ActorSystem(
name = "webapi",
config = None,
classLoader = None,
defaultExecutionContext = Some(executionContext),
),
)
.attempt(actor.ActorSystem("webapi", None, None, Some(executionContext)))
.zipLeft(ZIO.logInfo(">>> Acquire Actor System <<<"))
.orDie

private def release(system: pekko.actor.ActorSystem): URIO[Any, actor.Terminated] =
ZIO
.fromFuture(_ => system.terminate())
.zipLeft(ZIO.logInfo(">>> Release Actor System <<<"))
.orDie
private def release(system: actor.ActorSystem): URIO[Any, actor.Terminated] =
ZIO.fromFuture(_ => system.terminate()).zipLeft(ZIO.logInfo(">>> Release Actor System <<<")).orDie

val layer: ZLayer[Any, Nothing, ActorSystem] =
ZLayer.scoped {
val layer: ZLayer[Any, Nothing, actor.ActorSystem] =
ZLayer.scoped(
for {
context <- ZIO.executor.map(_.asExecutionContext)
actorSystem <- ZIO.acquireRelease(acquire(context))(release)
} yield new ActorSystem {
override val system: pekko.actor.ActorSystem = actorSystem
}
}
context <- ZIO.executor.map(_.asExecutionContext)
system <- ZIO.acquireRelease(acquire(context))(release)
} yield system,
)
}
6 changes: 3 additions & 3 deletions webapi/src/main/scala/org/knora/webapi/core/AppRouter.scala
Expand Up @@ -32,22 +32,22 @@ trait AppRouter {

object AppRouter {
val layer: ZLayer[
core.ActorSystem & CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & MessageRelay &
pekko.actor.ActorSystem & CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & MessageRelay &
OntologyCache & OntologyHelpers & OntologyRepo & PermissionUtilADM & ResourceUtilV2 & StandoffTagUtilV2,
Nothing,
AppRouter,
] =
ZLayer {
for {
as <- ZIO.service[core.ActorSystem]
as <- ZIO.service[pekko.actor.ActorSystem]
messageRelay <- ZIO.service[MessageRelay]
runtime <-
ZIO.runtime[
CardinalityHandler & CardinalityService & ConstructResponseUtilV2 & OntologyCache & OntologyHelpers &
OntologyRepo & PermissionUtilADM & ResourceUtilV2 & StandoffTagUtilV2,
]
} yield new AppRouter {
implicit val system: org.apache.pekko.actor.ActorSystem = as.system
implicit val system: org.apache.pekko.actor.ActorSystem = as

val ref: ActorRef = system.actorOf(
Props(
Expand Down
7 changes: 4 additions & 3 deletions webapi/src/main/scala/org/knora/webapi/core/AppServer.scala
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.core

import org.apache.pekko.actor
import zio.*

import org.knora.webapi.config.AppConfig
Expand All @@ -26,7 +27,7 @@ final case class AppServer(
state: State,
ts: TriplestoreService,
ru: RepositoryUpdater,
as: ActorSystem,
as: actor.ActorSystem,
ontologyCache: OntologyCache,
sipiService: SipiService,
hs: HttpServer,
Expand Down Expand Up @@ -136,7 +137,7 @@ final case class AppServer(
object AppServer {

private type AppServerEnvironment =
State & TriplestoreService & RepositoryUpdater & ActorSystem & OntologyCache & SipiService & HttpServer & AppConfig
State & TriplestoreService & RepositoryUpdater & actor.ActorSystem & OntologyCache & SipiService & HttpServer & AppConfig

/**
* Initializes the AppServer instance with the required services
Expand All @@ -146,7 +147,7 @@ object AppServer {
state <- ZIO.service[State]
ts <- ZIO.service[TriplestoreService]
ru <- ZIO.service[RepositoryUpdater]
as <- ZIO.service[ActorSystem]
as <- ZIO.service[actor.ActorSystem]
oc <- ZIO.service[OntologyCache]
iiifs <- ZIO.service[SipiService]
hs <- ZIO.service[HttpServer]
Expand Down
10 changes: 4 additions & 6 deletions webapi/src/main/scala/org/knora/webapi/core/HttpServer.scala
Expand Up @@ -5,15 +5,13 @@

package org.knora.webapi.core

import org.apache.pekko
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.Http
import zio.*

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.routing.ApiRoutes

import pekko.http.scaladsl.Http

/**
* The Akka based HTTP server
*/
Expand All @@ -25,11 +23,11 @@ object HttpServer {
val layer: ZLayer[ActorSystem & AppConfig & ApiRoutes, Nothing, HttpServer] =
ZLayer.scoped {
for {
as <- ZIO.service[core.ActorSystem]
as <- ZIO.service[ActorSystem]
config <- ZIO.service[AppConfig]
apiRoutes <- ZIO.service[ApiRoutes]
binding <- {
implicit val system: pekko.actor.ActorSystem = as.system
implicit val system: ActorSystem = as

ZIO.acquireRelease {
ZIO
Expand Down
3 changes: 2 additions & 1 deletion webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.core

import org.apache.pekko.actor.ActorSystem
import zio.ULayer
import zio.ZLayer

Expand Down Expand Up @@ -93,7 +94,7 @@ object LayersLive {
*/
val dspLayersLive: ULayer[DspEnvironmentLive] =
ZLayer.make[DspEnvironmentLive](
ActorSystem.layer,
org.knora.webapi.core.ActorSystem.layer,
AdminApiEndpoints.layer,
AdminApiRoutes.layer,
ApiRoutes.layer,
Expand Down
Expand Up @@ -5,7 +5,7 @@

package org.knora.webapi.routing

import org.apache.pekko.actor
import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.cors.scaladsl.CorsDirectives
import org.apache.pekko.http.cors.scaladsl.settings.CorsSettings
import org.apache.pekko.http.scaladsl.model.HttpMethods.*
Expand All @@ -15,7 +15,6 @@ import zio.*

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.core.ActorSystem
import org.knora.webapi.core.AppRouter
import org.knora.webapi.core.MessageRelay
import org.knora.webapi.http.directives.DSPApiDirectives
Expand Down Expand Up @@ -61,7 +60,7 @@ object ApiRoutes {
resourceInfoRoutes <- ZIO.service[ResourceInfoRoutes]
searchApiRoutes <- ZIO.service[SearchApiRoutes]
managementRoutes <- ZIO.service[ManagementRoutes]
routeData <- ZIO.succeed(KnoraRouteData(sys.system, router.ref, appConfig))
routeData <- ZIO.succeed(KnoraRouteData(sys, router.ref, appConfig))
runtime <-
ZIO.runtime[
AppConfig & AuthorizationRestService & core.State & IriConverter & KnoraProjectRepo & MessageRelay & ProjectADMRestService & RestCardinalityService & RestResourceInfoService & routing.Authenticator & SearchApiRoutes & SearchResponderV2 & SipiService & StringFormatter & UserService & ValuesResponderV2,
Expand Down Expand Up @@ -98,7 +97,7 @@ private final case class ApiRoutesImpl(
) extends ApiRoutes
with AroundDirectives {

private implicit val system: actor.ActorSystem = routeData.system
private implicit val system: ActorSystem = routeData.system

val routes: Route =
logDuration {
Expand Down
Expand Up @@ -5,6 +5,7 @@

package org.knora.webapi.slice.common.api

import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.server.Route
import sttp.capabilities.WebSockets
import sttp.capabilities.pekko.PekkoStreams
Expand All @@ -22,10 +23,8 @@ import zio.json.JsonCodec
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import org.knora.webapi.core.ActorSystem

final case class TapirToPekkoInterpreter()(actorSystem: ActorSystem) {
implicit val executionContext: ExecutionContext = actorSystem.system.dispatcher
final case class TapirToPekkoInterpreter()(system: ActorSystem) {
implicit val executionContext: ExecutionContext = system.dispatcher
private case class GenericErrorResponse(error: String)
private object GenericErrorResponse {
implicit val codec: JsonCodec[GenericErrorResponse] = DeriveJsonCodec.gen[GenericErrorResponse]
Expand Down

0 comments on commit 292f7eb

Please sign in to comment.