lila/modules/relay/src/main/RelayPush.scala

46 lines
1.2 KiB
Scala
Raw Normal View History

package lila.relay
import akka.actor._
import scala.concurrent.duration._
import lila.study.MultiPgn
2020-05-05 22:11:15 -06:00
final class RelayPush(sync: RelaySync, api: RelayApi)(implicit
system: ActorSystem,
ec: scala.concurrent.ExecutionContext
) {
2021-10-26 02:01:10 -06:00
private val throttler = new lila.hub.EarlyMultiThrottler(logger)
2021-04-23 13:22:10 -06:00
def apply(rt: RelayRound.WithTour, pgn: String): Fu[Option[String]] =
2021-04-25 04:30:46 -06:00
if (rt.round.sync.hasUpstream)
fuccess("The relay has an upstream URL, and cannot be pushed to.".some)
else
fuccess {
2021-10-26 02:01:10 -06:00
throttler(rt.round.id.value, if (rt.tour.official) 3.seconds else 7.seconds) {
pushNow(rt, pgn)
}
none
}
2021-04-23 13:22:10 -06:00
private def pushNow(rt: RelayRound.WithTour, pgn: String): Funit =
RelayFetch
2021-04-23 00:50:15 -06:00
.multiPgnToGames(MultiPgn.split(pgn, RelayFetch.maxChapters(rt.tour)))
2021-04-27 03:53:45 -06:00
.flatMap { games =>
sync(rt, games)
.map { res =>
2021-09-23 02:18:10 -06:00
SyncLog.event(res.nbMoves, none)
2021-04-27 03:53:45 -06:00
}
.recover { case e: Exception =>
SyncLog.event(0, e.some)
}
.flatMap { event =>
api
.update(rt.round)(
_.withSync(_ addLog event).copy(finished = games.forall(_.end.isDefined))
)
.void
}
}
}