Skip to content

Commit

Permalink
Better program execution in Lambda and DevServer
Browse files Browse the repository at this point in the history
Improve the ZIOv2 unsafe program run at the edge of the
Lambda/DevServer programs.
  • Loading branch information
adamnfish committed Dec 20, 2023
1 parent 5886f7e commit 252284d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
29 changes: 15 additions & 14 deletions devserver/src/main/scala/io/adamnfish/pokerdot/DevServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
}
}
})
Expand Down
32 changes: 17 additions & 15 deletions lambda/src/main/scala/io/adamnfish/pokerdot/Lambda.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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 {
Expand All @@ -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}")
}
}
}

Expand Down

0 comments on commit 252284d

Please sign in to comment.