Skip to content

Instantly share code, notes, and snippets.

@lu4nm3
Last active May 14, 2020 02:06
Show Gist options
  • Save lu4nm3/83a8579262d9683aa9ff359d2c21d7ac to your computer and use it in GitHub Desktop.
Save lu4nm3/83a8579262d9683aa9ff359d2c21d7ac to your computer and use it in GitHub Desktop.
def program[F[_]](implicit M: Monad[F]): IndexedReaderWriterStateT[F, Env, Vector[SystemState], SystemState, SystemState, SystemState] = {
for {
// retrieve environment input
env <- IndexedReaderWriterStateT.ask[F, Env, Vector[SystemState], SystemState]
// get server status as F[Status] and lift into transformer context
statusF = getServerStatus[F]().apply(env)
status <- IndexedReaderWriterStateT.liftF[F, Env, Vector[SystemState], SystemState, Status](statusF)
// get current state
state <- IndexedReaderWriterStateT.get[F, Env, Vector[SystemState], SystemState]
// create new state and update it
updatedState = SystemState(state.value + (env -> status))
_ <- IndexedReaderWriterStateT.set[F, Env, Vector[SystemState], SystemState](updatedState)
// log updated state
_ <- IndexedReaderWriterStateT.tell[F, Env, Vector[SystemState], SystemState](Vector(updatedState))
// alert system admin if needed
_ <- status match {
case Degraded | Unavailable =>
val alertAdminF = alertAdmin[F](status).apply(env)
IndexedReaderWriterStateT.liftF[F, Env, Vector[SystemState], SystemState, Unit](alertAdminF)
case _ =>
IndexedReaderWriterStateT.liftF[F, Env, Vector[SystemState], SystemState, Unit](M.unit)
}
// retrieve logs and current state
finalState <- IndexedReaderWriterStateT.get[F, Env, Vector[SystemState], SystemState]
} yield finalState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment