Schedule

fun interface Schedule<in Input, out Output>

A Schedule describes how a suspend fun should retry or repeat.

It's defined by a step function that takes an Input and returns a Decision, the Decision determines if the suspend fun should be Continue to be retried or repeated (and if so, the delay until the next attempt), or if the Schedule is Done retrying or repeating.

Types

Link copied to clipboard
object Companion
Link copied to clipboard
sealed interface Decision<in Input, out Output>

Properties

Link copied to clipboard

Functions

Link copied to clipboard
open infix fun <B> and(other: Schedule<Input, B>): Schedule<Input, Pair<Output, B>>

Combines two Schedules into one by combining the output of both Schedules into a Pair. It chooses the longest delay between the two Schedules. If one of the Schedules is done, the other Schedule is not executed anymore.

open fun <B, C> and(other: Schedule<Input, B>, transform: suspend (output: Output, b: B) -> C): Schedule<Input, C>

Combines two Schedules into one by transforming the output of both Schedules using transform. It chooses the longest delay between the two Schedules. If one of the Schedules is done, the other Schedule is not executed anymore.

open fun <B, C> and(other: Schedule<Input, B>, transform: suspend (output: Output, b: B) -> C, combineDuration: suspend (left: Duration, right: Duration) -> Duration): Schedule<Input, C>

Combines two Schedules into one by transforming the output of both Schedules using transform. It combines the delay of both Schedules using combineDuration. If one of the Schedules is done, the other Schedule is not executed anymore.

Link copied to clipboard
open infix fun <A> andThen(other: Schedule<Input, A>): Schedule<Input, Either<Output, A>>

Runs this schedule until Done, and then runs other until Done. Wrapping the output of this in Either.Left, and the output of other in Either.Right.

open fun <A, B> andThen(other: Schedule<Input, A>, ifLeft: suspend (Output) -> B, ifRight: suspend (A) -> B): Schedule<Input, B>

Runs this schedule, and transforms the output of this schedule using ifLeft, When this schedule is Done, it runs other schedule, and transforms the output using ifRight.

Link copied to clipboard

Collects all the Output of the Schedule into a List. This is useful in combination with identity to collect all the inputs.

Link copied to clipboard
open fun <A> contramap(transform: suspend (A) -> Input): Schedule<A, Output>

Transform the Schedule by mapping the Input's.

Link copied to clipboard
open fun delayed(transform: suspend (Output, Duration) -> Duration): Schedule<Input, Output>

Modify Continue.delay by the given function transform.

Link copied to clipboard
open fun doUntil(predicate: suspend (input: Input, output: Output) -> Boolean): Schedule<Input, Output>

Runs the Schedule until the predicate of Input and Output returns true. Inverse version of doWhile.

Link copied to clipboard
open fun doWhile(predicate: suspend (Input, Output) -> Boolean): Schedule<Input, Output>

Runs this Schedule while the predicate of Input and Output returns false.

Link copied to clipboard
open fun <B> fold(b: B, f: suspend (B, Output) -> B): Schedule<Input, B>

Folds all the Output of the Schedule into a List. This is useful in combination with identity to fold all the Input into a final value B. If one of the Schedules is done, the other Schedule is not executed anymore.

Link copied to clipboard
abstract suspend operator fun invoke(input: Input): Schedule.Decision<Input, Output>
Link copied to clipboard
open fun jittered(min: Double = 0.0, max: Double = 1.0, random: Random = Random.Default): Schedule<Input, Output>

Adds a Random jitter to the delay of the Schedule.

Link copied to clipboard
open fun log(action: suspend (input: Input, output: Output) -> Unit): Schedule<Input, Output>

Adds a logging action to the Schedule.

Link copied to clipboard
open fun <A> map(transform: suspend (output: Output) -> A): Schedule<Input, A>

Transforms every Output'ed value of this schedule using transform.

Link copied to clipboard
Link copied to clipboard
open fun <B, C> or(other: Schedule<Input, B>, transform: suspend (output: Output?, b: B?) -> C, combineDuration: suspend (left: Duration?, right: Duration?) -> Duration): Schedule<Input, C>

Combines two Schedules into one by transforming the output of both Schedules using transform. It combines the delay of both Schedules using combineDuration. It continues to execute both Schedules until both are done, padding the output and duration with null if one of the Schedules is done.

Link copied to clipboard
open suspend fun repeat(block: suspend () -> Input): Output

Repeat the schedule, and uses block as Input for the step function.

Link copied to clipboard
open suspend fun repeatOrElse(block: suspend () -> Input, orElse: suspend (error: Throwable, output: Output?) -> Output): Output

Repeat the schedule, and uses block as Input for the step function. If the step function throws an exception, it will be caught and passed to orElse.

Link copied to clipboard
open suspend fun <A> repeatOrElseEither(block: suspend () -> Input, orElse: suspend (error: Throwable, output: Output?) -> A): Either<A, Output>

Repeat the schedule, and uses block as Input for the step function. If the step function throws an exception, it will be caught and passed to orElse. The resulting Either indicates if the step function threw an exception or not.

Link copied to clipboard
inline suspend fun <E : Throwable, A> Schedule<E, *>.retry(noinline action: suspend () -> A): A
suspend fun <E : Throwable, A> Schedule<E, *>.retry(exceptionClass: KClass<E>, action: suspend () -> A): A

Retries action using any E that occurred as the input to the Schedule. It will throw the last exception if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
inline suspend fun <Error, Result, Output> Schedule<Error, Output>.retryEither(action: () -> Either<Error, Result>): Either<Error, Result>

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
inline suspend fun <E : Throwable, Input, Output> Schedule<E, Output>.retryOrElse(noinline action: suspend () -> Input, noinline orElse: suspend (Throwable, Output) -> Input): Input
suspend fun <E : Throwable, Input, Output> Schedule<E, Output>.retryOrElse(exceptionClass: KClass<E>, action: suspend () -> Input, orElse: suspend (E, Output) -> Input): Input

Retries action using any E that occurred as the input to the Schedule. If the Schedule is exhausted, it will invoke orElse with the last exception and the output of the Schedule to produce a fallback Input value.

Link copied to clipboard
inline suspend fun <E : Throwable, Input, Output, A> Schedule<E, Output>.retryOrElseEither(noinline action: suspend () -> Input, noinline orElse: suspend (E, Output) -> A): Either<A, Input>
suspend fun <E : Throwable, Input, Output, A> Schedule<E, Output>.retryOrElseEither(exceptionClass: KClass<E>, action: suspend () -> Input, orElse: suspend (E, Output) -> A): Either<A, Input>

Retries action using any E that occurred as the input to the Schedule. If the Schedule is exhausted, it will invoke orElse with the last exception and the output of the Schedule to produce a fallback value of A. Returns Either with the fallback value if the Schedule is exhausted, or the successful result of action.

Link copied to clipboard
inline suspend fun <Error, Result, Output> Schedule<Error, Output>.retryRaise(action: Raise<Error>.() -> Result): Either<Error, Result>

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
open infix fun <B> zipLeft(other: Schedule<Input, B>): Schedule<Input, Output>

Combines two Schedules into one, ignoring the output of other. It chooses the longest delay between the two Schedules. If one of the Schedules is done, the other Schedule is not executed anymore.

Link copied to clipboard
open infix fun <B> zipRight(other: Schedule<Input, B>): Schedule<Input, B>

Combines two Schedules into one, ignoring the output of this Schedule. It chooses the longest delay between the two Schedules. If one of the Schedules is done, the other Schedule is not executed anymore.