Skip to content

Commit

Permalink
Move acquiredAt creation to IO
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 19, 2023
1 parent 9666b84 commit 714b0d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ object Executor:
clearIfFull(m) + (move.id -> move)

def acquire(accquire: MoveDb.Acquire): IO[Option[Work.Move]] =
ref.modify: coll =>
coll.values
.foldLeft[Option[Move]](None):
case (found, m) if m.nonAcquired =>
Some(found.fold(m): a =>
if m.canAcquire(accquire.clientKey) && m.createdAt.isBefore(a.createdAt) then m else a)
case (found, _) => found
.map: m =>
val move = m.assignTo(accquire.clientKey)
(coll + (move.id -> move)) -> move.some
.getOrElse(coll -> none[Work.Move])
IO.realTimeInstant.flatMap: at =>
ref.modify: coll =>
coll.values
.foldLeft[Option[Move]](None):
case (found, m) if m.nonAcquired =>
Some(found.fold(m): a =>
if m.canAcquire(accquire.clientKey) && m.createdAt.isBefore(a.createdAt) then m else a)
case (found, _) => found
.map: m =>
val move = m.assignTo(accquire.clientKey, at)
(coll + (move.id -> move)) -> move.some
.getOrElse(coll -> none[Work.Move])

def move(workId: Work.Id, data: Fishnet.PostMove): IO[Unit] =
ref.flatModify: coll =>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/scala/Work.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ object Work:
def canAcquire(clientKey: ClientKey) = lastTryByKey.fold(true)(clientKey.!=)
def acquiredBefore(date: Instant) = acquiredAt.fold(false)(_.isBefore(date))

def assignTo(clientKey: ClientKey) =
def assignTo(clientKey: ClientKey, at: Instant) =
copy(
acquired = Some(
Acquired(
clientKey = clientKey,
date = Instant.now,
date = at,
)
),
lastTryByKey = Some(clientKey),
Expand Down

0 comments on commit 714b0d1

Please sign in to comment.