Skip to content

Commit 1d062c9

Browse files
authored
Add log when server start and abnormally stop(series/1.x) (#21)
* Add log * Apply feedback
1 parent bd4ce52 commit 1d062c9

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

examples/src/test/scala/example/EndToEndSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import scala.util.Try
2828

2929
object EndToEndSpec extends DefaultRunnableSpec {
3030

31-
val shardManagerServer
32-
: ZLayer[Has[ShardManager] with Has[ManagerConfig] with Clock with Console, Throwable, Has[Unit]] =
31+
val shardManagerServer: ZLayer[Has[ShardManager] with Has[ManagerConfig] with Has[
32+
Logging
33+
] with Clock with Console, Throwable, Has[Unit]] =
3334
(Server.run.forkDaemon *> Clock.Service.live.sleep(3 seconds).unit).toLayer
3435

3536
val container: ZLayer[Blocking, Nothing, Has[GenericContainer]] =
@@ -77,7 +78,7 @@ object EndToEndSpec extends DefaultRunnableSpec {
7778
val service = config ++ sharding ++ clock >+> GrpcShardingService.live
7879
val health = pods >>> PodsHealth.local
7980
val manager = health ++ pods ++ storage ++ managerConfig ++ logging ++ clock >>> ShardManager.live
80-
val managerServer = manager ++ managerConfig ++ clock ++ Console.live >>> shardManagerServer
81+
val managerServer = manager ++ managerConfig ++ logging ++ clock ++ Console.live >>> shardManagerServer
8182
val layer = sharding ++ service ++ redisContainer ++ managerServer
8283

8384
def spec =

manager/src/main/scala/com/devsisters/shardcake/Server.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.devsisters.shardcake
22

33
import caliban.ZHttpAdapter
44
import caliban.wrappers.Wrappers.printErrors
5+
import com.devsisters.shardcake.interfaces.Logging
56
import zhttp.http.Middleware.cors
67
import zhttp.http._
7-
import zhttp.service.{ Server => ZServer }
8+
import zhttp.service.{ EventLoopGroup, Server => ZServer }
9+
import zhttp.service.server.ServerChannelFactory
810
import zio._
911
import zio.clock.Clock
1012
import zio.console.Console
@@ -14,18 +16,23 @@ object Server {
1416
/**
1517
* Start an HTTP server that exposes the Shard Manager GraphQL API
1618
*/
17-
val run: RIO[Has[ShardManager] with Has[ManagerConfig] with Clock with Console, Nothing] =
19+
val run: RIO[Has[ShardManager] with Has[ManagerConfig] with Has[Logging] with Clock with Console, Nothing] =
1820
for {
1921
config <- ZIO.service[ManagerConfig]
22+
logger <- ZIO.service[Logging]
2023
interpreter <- (GraphQLApi.api @@ printErrors).interpreter
21-
nothing <- ZServer
22-
.start(
23-
config.apiPort,
24-
Http.collectHttp[Request] {
25-
case _ -> !! / "health" => Http.succeed(Response.ok)
26-
case _ -> !! / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
27-
case _ -> !! / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
28-
} @@ cors()
24+
server = ZServer.port(config.apiPort) ++ ZServer.app(Http.collectHttp[Request] {
25+
case _ -> !! / "health" => Http.succeed(Response.ok)
26+
case _ -> !! / "api" / "graphql" => ZHttpAdapter.makeHttpService(interpreter)
27+
case _ -> !! / "ws" / "graphql" => ZHttpAdapter.makeWebSocketService(interpreter)
28+
} @@ cors())
29+
nothing <- server.make
30+
.use(start =>
31+
logger.logInfo(s"Server Manager started on port ${start.port}")
32+
*> ZIO.never
33+
)
34+
.provideSomeLayer[Console with Has[ShardManager] with Clock](
35+
ServerChannelFactory.auto ++ EventLoopGroup.auto(0)
2936
)
3037
.forever
3138
} yield nothing

0 commit comments

Comments
 (0)