Skip to content

Commit

Permalink
Add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Mar 20, 2024
1 parent 9a612dc commit de38a70
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class FishnetApp(res: AppResources, config: AppConfig)(using Logger[IO]):
def run(): Resource[IO, Unit] =
for
executor <- createExcutor
httpApi = HttpApi(executor, HealthCheck(), config.server)
server <- MkHttpServer.apply.newEmber(config.server, httpApi.httpApp)
httpApp = HttpApi(executor, HealthCheck(), config.server).httpApp
server <- MkHttpServer.apply.newEmber(config.server, httpApp)
_ <- RedisSubscriberJob(executor, res.redisPubsub).run()
_ <- WorkCleaningJob(executor).run()
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ object Executor:
ref.flatModify: state =>
val (newState, effect) =
if state.isFull(config.maxSize) then
def ids = state.tasks.map(t => t.id -> t.request.id).mkString(", ")
AppState.empty ->
Logger[IO].warn(s"stateSize=${state.size} maxSize=${config.maxSize}. Dropping all!")
Logger[IO].warn(
s"stateSize=${state.size} maxSize=${config.maxSize}. Dropping all! tasks: $ids"
)
else state -> IO.unit
newState.add(task) -> effect *> monitor.updateSize(newState)

Expand Down Expand Up @@ -80,12 +83,13 @@ object Executor:
newState -> logs

private def invalidate(workId: WorkId, key: ClientKey): IO[Unit] =
ref.flatModify: state =>
state.remove(workId) ->
state
.get(workId)
.fold(Logger[IO].warn(s"unknown and invalid work from $key")): task =>
Logger[IO].warn(s"invalid lila work $task from $key")
Logger[IO].info(s"invalid work: $workId by $key") *>
ref.flatModify: state =>
state.remove(workId) ->
state
.get(workId)
.fold(Logger[IO].warn(s"unknown and invalid work from $key")): task =>
Logger[IO].warn(s"invalid lila work $task from $key")

def clean(since: Instant): IO[Unit] =
ref.flatModify: state =>
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/scala/http/FishnetRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import org.http4s.circe.CirceEntityDecoder.*
import org.http4s.circe.CirceEntityEncoder.*
import org.http4s.dsl.Http4sDsl
import org.http4s.server.Router
import org.typelevel.log4cats.Logger

final class FishnetRoutes(executor: Executor) extends Http4sDsl[IO]:
final class FishnetRoutes(executor: Executor)(using Logger[IO]) extends Http4sDsl[IO]:

private[http] val prefixPath = "/fishnet"
private val prefixPath = "/fishnet"

private val httpRoutes: HttpRoutes[IO] = HttpRoutes.of[IO]:
private val httpRoutes = HttpRoutes.of[IO]:

case req @ POST -> Root / "acquire" =>
req
Expand All @@ -27,13 +28,13 @@ final class FishnetRoutes(executor: Executor) extends Http4sDsl[IO]:
executor.move(id, move.fishnet.apikey, move.move.bestmove)
>> acquire(move.fishnet.apikey)

def acquire(key: ClientKey): IO[Response[IO]] =
private def acquire(key: ClientKey): IO[Response[IO]] =
executor
.acquire(key)
.map(_.map(_.toResponse))
.flatMap(_.fold(NoContent())(Ok(_)))
.flatMap(_.fold(NoContent())(task => Ok(task.toResponse)))
.recoverWith:
case x => InternalServerError(x.getMessage().nn)
case x =>
Logger[IO].error(x.getMessage) *> InternalServerError(x.getMessage.nn)

val routes: HttpRoutes[IO] = Router(prefixPath -> httpRoutes)

Expand Down

0 comments on commit de38a70

Please sign in to comment.