Skip to content

Instantly share code, notes, and snippets.

@ioleo
Created September 18, 2018 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ioleo/bb281f9485471040fe23d379278748f9 to your computer and use it in GitHub Desktop.
Save ioleo/bb281f9485471040fe23d379278748f9 to your computer and use it in GitHub Desktop.
Scalaz Zio Schedule test
import scalaz.zio._
import scalaz.zio.interop.future._
import scala.concurrent.{ ExecutionContext, Future }
import scala.concurrent.duration._
object ZioScheduleTest extends App {
var counter = 0
def futureF(): Future[Int] = {
counter += 1
if (counter % 3 == 0) {
val message = s"failed for $counter"
println(message)
Future.failed(new Exception(message))
} else {
println(s"success for $counter")
Future.successful(counter)
}
}
val updateSchedule = Schedule.spaced(10.seconds)
val retryPolicy = Schedule.fibonacci(1.second) || updateSchedule
val ioF =
IO.fromFuture(futureF _)(ExecutionContext.global)
.repeat(updateSchedule) // should update every 10 seconds, until it fails
.retry(retryPolicy) // keep retrying program above ^, with fibonacci growing intervals, but at most 10 seconds
// This program will effectively run forever
def run(args: List[String]): IO[Nothing, ExitStatus] =
ioF.attempt.map(_.fold(_ => 1, _ => 0)).map(ExitStatus.ExitNow(_))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment