Skip to content

Commit

Permalink
Add warning log when giving up a move
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 30, 2023
1 parent ffff58e commit c15604a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
22 changes: 14 additions & 8 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ object Executor:
)
))
case _ =>
updateOrGiveUp(coll, work.invalid) ->
monitor.failure(work, apikey, new Exception("Missing move"))
val (state, failedMove) = updateOrGiveUp(coll, work.invalid)
state -> (Logger[IO].warn(s"Give up move: $failedMove") >>
monitor.failure(work, apikey, new Exception("Missing move")))

case Some(move) =>
coll -> monitor.notAcquired(move, apikey)
Expand All @@ -84,20 +85,25 @@ object Executor:
Logger[IO].debug(s"cleaning ${timedOut.size} of ${coll.size} moves") >>
timedOut.traverse_(m => Logger[IO].info(s"Timeout move: $m"))
else IO.unit
val x = timedOut.foldLeft(coll): (coll, m) =>
updateOrGiveUp(coll, m.timeout)
x -> logIfTimedOut.as(x)
val (state, gavedUpMoves) = timedOut.foldLeft[(State, List[Work.Move])](coll -> Nil):
(x, m) =>
val (newState, move) = updateOrGiveUp(x._1, m.timeout)
(newState, move.fold(x._2)(_ :: x._2))

state -> (logIfTimedOut *> gavedUpMoves
.traverse_(m => Logger[IO].warn(s"Give up move: $m"))
.as(state))
.flatMap(monitor.updateSize)

def clearIfFull(coll: State): (State, IO[Unit]) =
if coll.size > maxSize then
Map.empty -> Logger[IO].warn(s"MoveDB collection is full! maxSize=$maxSize. Dropping all now!")
else coll -> IO.unit

def updateOrGiveUp(state: State, move: Work.Move): State =
def updateOrGiveUp(state: State, move: Work.Move): (State, Option[Work.Move]) =
val newState = state - move.id
if move.isOutOfTries then newState
else newState + (move.id -> move)
if move.isOutOfTries then (newState, move.some)
else (newState + (move.id -> move), none)

def fromRequest(req: Lila.Request): IO[Move] =
(IO.delay(Work.makeId), IO.realTimeInstant).mapN: (id, now) =>
Expand Down
8 changes: 6 additions & 2 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package lila.fishnet

import weaver.*
import cats.syntax.all.*
import cats.effect.IO
import cats.effect.kernel.Ref
import cats.syntax.all.*
import java.time.Instant
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import weaver.*

import Helper.*

object ExecutorTest extends SimpleIOSuite:

given Logger[IO] = NoOpLogger[IO]

val request: Lila.Request = Lila.Request(
id = GameId("1"),
initialFen = chess.variant.Standard.initialFen,
Expand Down

0 comments on commit c15604a

Please sign in to comment.