ZIO Schedule: How to Repeat and Retry Effects?

zio
Reading Time: 3 minutes

What is schedule?

ZIO Schedule provides a possibility to execute the effects repeatedly for an infinite set of time intervals. Each interval represents a window in which the recurrence is possible. Technically, the schedule is an immutable value that describes the recurring effect. The ZIO schedule is of type Schedule[Env, In, Out] where –

Env : represents the ZIO environment in which the schedule runs

In : represents the input value that the schedule consumes while executing the effect

Out : represents the output value that is produced from the schedule

The schedule helps to repeat and retry the effects in ZIO. Between every subsequent recurrence, the system decides to halt or continue the schedule after some delay. The delay is determined on the basis of the scheduling scheme provided.

Repeat VS Retry: General Idea

Repetition and retrying are two similar concepts in the domain of scheduling. They are conceptually the same. While repeat executes the succeeding effect, again and again, retry executes the failing effect again to get success by means of recurrence(s). In a nutshell, one looks for success while the other looks for failure.

Repeat Effects:

Repeat mechanism allows to define and compose flexible recurrence schedules that execute an effect repeatedly or repeat certain actions. ZIO provides functions to create repeating effects. These functions accept a schedule that defines the repetition policy for the effect and returns an effect that describes the repetition schedule for that effect.

Using repeat Function:

This function is used to repeat the effect again until the schedule is done. Consider the code snippet below:

Code Snippet demonstrating use of repeat function to schedule recurring effect.

Using repeatOrElse Function:

ZIO#repeatOrElse is another version of ZIO#repeat. It helps us to provide a fallback in case of error. The else block executes only when there is an error while executing the effect or its recurrence.

Consider the code snippet below:

  val effect: ZIO[R, E, A] = ???
  val policy: Schedule[R1, A, B] = ???

  val fallback: (E, S) => ZIO[R1, E1, A1] = ???

  val repeated = effect.retryOrElse(policy, fallback)

Retry Effects:

The retry mechanism allows to define and compose recurrence schedules that execute an effect when an error occurs or any effect fails. Before handling the errors in effects, it would be wise to retry the failure. ZIO provides functions to retry the failed effects. These functions take a schedule as a repetition policy and return another effect that describes an effect with a repetition strategy that will retry following the failure of the original effect.

Using retry Function:

This function is used to retry a failed effect again until it succeeds. Consider the code snippet below:

Code Snippet demonstraing the use of schedule to retry failing effect.

Using retryOrElse Function:

ZIO#retryOrElse is another version of ZIO#repeat. It helps us to provide a fallback in case of error. This function adds an orElse callback that will run in case of failure of repetition failure.

Consider the code snippet below:

  val effect: ZIO[R, E, A] = ???
  val policy: Schedule[R1, A, B] = ???

  val fallback: (E, S) => ZIO[R1, E1, A1] = ???

  val repeated = effect.retryOrElse(policy, fallback)

Conclusion:

It is important to note that the recurrences discussed above are in addition to the initial action. For Example, if you repeat an effect once, there will be a total of two executions for that effect. It should be evident from the first code snippet in the above examples.

Also, there are various schemes provided by ZIO#Schedule to repeat/retry effects. These schemes can be used depending upon the requirement. However, discussing all those schemes is beyond the scope of this blog.

Further Reading:

Handling Errors in ZIO Effects: https://blog.knoldus.com/how-to-handle-errors-in-zio/

Different schedule policies (official documentation): https://zio.dev/version-1.x/datatypes/misc/schedule

ZIO full official documentation: https://zio.dev/version-1.x/overview/