Are there any neat ways to combine Schedule and Ei...
# arrow
e
Are there any neat ways to combine Schedule and Either? I would like to repeat an action until success, or until the maximum attempts are exhausted. If attempts are exhausted, I would like to perform another action.
So far I got something like
Copy code
private val connectRetryPolicy = Schedule
      .spaced<Either<Throwable, Unit>>(retryDelay)
      .and(Schedule.recurs(maxAttempts))
      .doUntil { input, _ -> input.isRight() }
which repeats until we get an
Either.Right
. Then I just use
.repeat(primaryAction)
to attempt until success.. But I'm not sure how to add the failure action 🙂
I changed it to
Copy code
val policy = Schedule.identity<Either<Throwable, Unit>>
  .zipLeft(Schedule.spaced(retryDelay))
  .zipLeft(Schedule.recurs(maxAttempts))
  .doUntil { _, output -> output.isRight() }

policy.repeat(primaryAction)
  .fold(
    ifLeft = { failureAction() }
    ifRight = { <http://log.info|log.info>("Success") }
  )