Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mapAccumulateFilter #287

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/src/main/scala/AppState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ object AppState:
case Some(task) => GetTaskResult.AcquiredByOther(task)

def updateOrGiveUp(candidates: List[Work.Task]): (AppState, List[Work.Task]) =
candidates.foldLeft(state -> Nil) { case ((state, xs), task) =>
task.clearAssignedKey match
case None => (state - task.id, task :: xs)
case Some(unAssignedTask) => (state.updated(task.id, unAssignedTask), xs)
}
candidates.mapAccumulateFilter(state)(_.clearOrGiveUp(_))

def clearOrGiveUp(task: Work.Task): (AppState, Option[Work.Task]) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this refactor is great, and the name as you mentioned can be improved. I think both updateOrGiveUp and clearOrGiveUp can be renamed to unassignOrGiveup.

task.clearAssignedKey match
case None => (state - task.id, Some(task))
case Some(unAssignedTask) => (state.updated(task.id, unAssignedTask), None)

def acquiredBefore(since: Instant): List[Work.Task] =
state.values.filter(_.acquiredBefore(since)).toList
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ object Executor:
state.remove(task.id) -> (monitor.success(task) >>
client.send(Lila.Response(task.request.id, task.request.moves, uci)))
case _ =>
val (newState, io) = task.clearAssignedKey match
case None =>
state.remove(workId) -> Logger[IO].warn(
s"Give up move due to invalid move $response by $key for $task"
)
case Some(updated) => state.add(updated) -> IO.unit
val (newState, maybeGivenUp) = state.clearOrGiveUp(task)
val io = maybeGivenUp.traverse(task => Logger[IO].warn(
s"Give up move due to invalid move $response by $key for $task"
))
newState -> io *> failure(task, key)

def clean(since: Instant): IO[Unit] =
Expand Down
Loading