From 252284d128ec3c5f2d633651168433698a4f8cf7 Mon Sep 17 00:00:00 2001 From: adamnfish Date: Wed, 20 Dec 2023 10:14:17 +0000 Subject: [PATCH] Better program execution in Lambda and DevServer Improve the ZIOv2 unsafe program run at the edge of the Lambda/DevServer programs. --- .github/workflows/main.yml | 2 +- buildspec.yml | 2 +- .../adamnfish/pokerdot/models/Failures.scala | 3 ++ .../io/adamnfish/pokerdot/DevServer.scala | 29 +++++++++-------- .../scala/io/adamnfish/pokerdot/Lambda.scala | 32 ++++++++++--------- 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e047711..b9cdbc1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,7 +18,7 @@ jobs: distribution: 'corretto' cache: 'sbt' - name: Run tests - run: sbt test lambda/universal:packageBin + run: sbt test lambda/Universal/packageBin test-frontend: runs-on: ubuntu-latest diff --git a/buildspec.yml b/buildspec.yml index a361da8..5973d26 100644 --- a/buildspec.yml +++ b/buildspec.yml @@ -37,7 +37,7 @@ phases: - echo "[Status] Build started at `date`" # test and package API - - sbt/bin/sbt -no-colors test lambda/universal:packageBin + - sbt/bin/sbt -no-colors test lambda/Universal/packageBin # frontend - cd frontend - npm run test diff --git a/core/src/main/scala/io/adamnfish/pokerdot/models/Failures.scala b/core/src/main/scala/io/adamnfish/pokerdot/models/Failures.scala index 50f0ac2..535e341 100644 --- a/core/src/main/scala/io/adamnfish/pokerdot/models/Failures.scala +++ b/core/src/main/scala/io/adamnfish/pokerdot/models/Failures.scala @@ -14,6 +14,9 @@ case class Failures(failures: List[Failure]) { }.mkString(", ") val externalFailures: List[Failure] = failures.filterNot(_.internal) + + def exception: Option[Throwable] = + failures.find(_.exception.isDefined).flatMap(_.exception) } object Failures { def apply(error: Failure): Failures = { diff --git a/devserver/src/main/scala/io/adamnfish/pokerdot/DevServer.scala b/devserver/src/main/scala/io/adamnfish/pokerdot/DevServer.scala index dcd15c0..6c10571 100644 --- a/devserver/src/main/scala/io/adamnfish/pokerdot/DevServer.scala +++ b/devserver/src/main/scala/io/adamnfish/pokerdot/DevServer.scala @@ -62,22 +62,23 @@ object DevServer { ws.onMessage { wctx => messagePrinter(Inbound)(wctx.getSessionId, wctx.message) val appContext = AppContext(PlayerAddress(wctx.getSessionId), db, messaging, Clock, rng) - val program = PokerDot.pokerdot(wctx.message, appContext).catchAll { failures => - ZIO.console.flatMap(_.printLine(s"[ERROR] Failures: ${failures.logString}")) - } Unsafe.unsafe { implicit unsafe => - runtime.unsafe.run(program) match { - case Exit.Success(operation) => - println(s"[INFO] $operation") - case Exit.Failure(cause) => - println(s"[ERROR] ${cause}") - cause.failures.foreach { e => - println(s"[ERROR] Unhandled exception: ${e.printStackTrace()}") - } - cause.defects.foreach { err => - println(s"[ERROR] Fatal error: ${err.toString}") - } + runtime.unsafe.run { + PokerDot.pokerdot(wctx.message, appContext) } + } match { + case Exit.Success(operation) => + println(s"[INFO] $operation") + case Exit.Failure(cause) => + cause.failures.foreach { fs => + println(s"[ERROR] error: ${fs.logString}") + fs.exception.foreach { e => + println(s"[ERROR] exception: ${e.printStackTrace()}") + } + } + cause.defects.foreach { err => + println(s"[ERROR] Fatal error: ${err.toString}") + } } } }) diff --git a/lambda/src/main/scala/io/adamnfish/pokerdot/Lambda.scala b/lambda/src/main/scala/io/adamnfish/pokerdot/Lambda.scala index 711f8fb..f214992 100644 --- a/lambda/src/main/scala/io/adamnfish/pokerdot/Lambda.scala +++ b/lambda/src/main/scala/io/adamnfish/pokerdot/Lambda.scala @@ -11,7 +11,7 @@ import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient import software.amazon.awssdk.regions.Region import software.amazon.awssdk.services.apigatewaymanagementapi.ApiGatewayManagementApiClient import software.amazon.awssdk.services.dynamodb.DynamoDbClient -import zio.{Runtime, Unsafe} +import zio.{Exit, Runtime, Unsafe, ZIO} import java.net.URI import scala.jdk.CollectionConverters._ @@ -61,9 +61,9 @@ class Lambda { } def handleRequest(event: APIGatewayV2WebSocketEvent, awsContext: AwsContext): APIGatewayV2WebSocketResponse = { - // Debugging for now - awsContext.getLogger.log(s"request body: ${event.getBody}") - awsContext.getLogger.log(s"connection ID: ${event.getRequestContext.getConnectionId}") + // Debugging +// awsContext.getLogger.log(s"request body: ${event.getBody}") +// awsContext.getLogger.log(s"connection ID: ${event.getRequestContext.getConnectionId}") awsContext.getLogger.log(s"route: ${event.getRequestContext.getRouteKey}") event.getRequestContext.getRouteKey match { @@ -78,18 +78,20 @@ class Lambda { Unsafe.unsafe { implicit unsafe => Runtime.default.unsafe.run( PokerDot.pokerdot(event.getBody, appContext) - ).fold( - { fs => - awsContext.getLogger.log( - s"Request failed: ${fs.logString}" - ) - }, - { operation => - awsContext.getLogger.log( - s"Request succeeded: $operation" - ) - } ) + } match { + case Exit.Success(operation) => + awsContext.getLogger.log(s"[INFO] $operation") + case Exit.Failure(cause) => + cause.failures.foreach { fs => + awsContext.getLogger.log(s"[ERROR] error: ${fs.logString}") + fs.exception.foreach { e => + awsContext.getLogger.log(s"[ERROR] exception: ${e.printStackTrace()}") + } + } + cause.defects.foreach { err => + awsContext.getLogger.log(s"[ERROR] Fatal error: ${err.toString}") + } } }