Schedule

value class Schedule<in Input, out Output>(val step: ScheduleStep<Input, Output>)(source)

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.

Constructors

Link copied to clipboard
constructor(step: ScheduleStep<Input, Output>)

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
infix fun <B> and(other: Schedule<@UnsafeVariance 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.

fun <B, C> and(other: Schedule<@UnsafeVariance 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.

fun <B, C> and(other: Schedule<@UnsafeVariance 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

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.

fun <A, B> andThen(other: Schedule<@UnsafeVariance 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
fun <A> contramap(transform: suspend (A) -> Input): Schedule<A, Output>

Transform the Schedule by mapping the Input's.

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

Modify Continue.delay by the given function transform.

Link copied to clipboard
fun doUntil(predicate: suspend (input: @UnsafeVariance 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
fun doWhile(predicate: suspend (@UnsafeVariance Input, Output) -> Boolean): Schedule<Input, Output>

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

Link copied to clipboard
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
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
fun log(action: suspend (input: @UnsafeVariance Input, output: Output) -> Unit): Schedule<Input, Output>

Adds a logging action to the Schedule.

Link copied to clipboard
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
fun <B, C> or(other: Schedule<@UnsafeVariance 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
suspend fun repeat(block: suspend () -> Input): Output

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

Link copied to clipboard
suspend fun repeatOrElse(block: suspend () -> Input, orElse: suspend (error: Throwable, output: Output?) -> @UnsafeVariance 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
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
suspend fun <A> Schedule<Throwable, *>.retry(action: suspend () -> A): A

Retries action using any Throwable 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
suspend fun <Input, Output> Schedule<Throwable, Output>.retryOrElse(action: suspend () -> Input, orElse: suspend (Throwable, Output) -> Input): Input

Retries action using any Throwable 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
suspend fun <Input, Output, A> Schedule<Throwable, Output>.retryOrElseEither(action: suspend () -> Input, orElse: suspend (Throwable, Output) -> A): Either<A, Input>

Retries action using any Throwable 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

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
infix fun <B> zipRight(other: Schedule<@UnsafeVariance 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.