2017-09-29 17:14:43 -06:00
|
|
|
package lila.relay
|
|
|
|
|
|
|
|
import org.joda.time.DateTime
|
|
|
|
|
2017-10-01 13:55:20 -06:00
|
|
|
case class SyncLog(events: Vector[SyncLog.Event]) extends AnyVal {
|
2017-09-29 17:14:43 -06:00
|
|
|
|
2018-09-28 05:53:40 -06:00
|
|
|
def isOk = events.lastOption.exists(_.isOk)
|
2017-10-01 14:49:10 -06:00
|
|
|
|
2020-08-17 16:10:52 -06:00
|
|
|
def alwaysFails = events.sizeIs == SyncLog.historySize && events.forall(_.isKo)
|
2017-10-01 14:49:10 -06:00
|
|
|
|
2018-09-28 05:53:40 -06:00
|
|
|
def justTimedOut = events.lastOption.exists(_.isTimeout)
|
|
|
|
|
2017-10-01 14:49:10 -06:00
|
|
|
def updatedAt = events.lastOption.map(_.at)
|
2017-10-16 13:08:08 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def add(event: SyncLog.Event) =
|
|
|
|
copy(
|
2021-11-28 07:36:17 -07:00
|
|
|
events = {
|
|
|
|
if (events.sizeIs > SyncLog.historySize) events drop 1
|
|
|
|
else events
|
|
|
|
} :+ event
|
2020-05-05 22:11:15 -06:00
|
|
|
)
|
2017-09-29 17:14:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
object SyncLog {
|
|
|
|
|
2017-09-30 17:34:07 -06:00
|
|
|
val historySize = 5
|
|
|
|
|
2018-09-06 10:32:35 -06:00
|
|
|
val empty = SyncLog(Vector.empty)
|
|
|
|
|
2017-09-29 17:14:43 -06:00
|
|
|
case class Event(
|
2017-10-04 23:59:35 -06:00
|
|
|
moves: Int,
|
2017-10-01 13:55:20 -06:00
|
|
|
error: Option[String],
|
2017-09-29 17:14:43 -06:00
|
|
|
at: DateTime
|
2017-10-01 13:55:20 -06:00
|
|
|
) {
|
2019-12-13 07:30:20 -07:00
|
|
|
def isOk = error.isEmpty
|
|
|
|
def isKo = error.nonEmpty
|
2018-09-28 05:53:40 -06:00
|
|
|
def isTimeout = error has SyncResult.Timeout.getMessage
|
2017-10-01 13:55:20 -06:00
|
|
|
}
|
2017-10-01 16:07:55 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def event(moves: Int, e: Option[Exception]) =
|
|
|
|
Event(
|
|
|
|
moves = moves,
|
|
|
|
error = e map {
|
|
|
|
case _: java.util.concurrent.TimeoutException => "Request timeout"
|
|
|
|
case e: Exception => e.getMessage take 100
|
|
|
|
},
|
|
|
|
at = DateTime.now
|
|
|
|
)
|
2017-09-29 17:14:43 -06:00
|
|
|
}
|