Search code examples
scalaschedulezio

Initial delay for ZIO ZSchedule


The schedule I'm trying to make would have to:

  1. Start after a specified delay
  2. Repeat at a fixed rate
  3. Terminate if it reaches given time limit or encounters terminating state

So what I have is (2.) and (3.):

val repeatUntilTimeLimitReached =
  ZSchedule
    .fixed(config.pollingConfig.pollInterval)
    .untilOutput(pollingTimeLimitReached)

val untilTermination = Schedule.doUntil[RebootState](_.terminatesPolling)

val schedule = repeatUntilTimeLimitReached *> untilTermination                                           

I tried ZSchedule.delayed(), but it seems to add delay to subsequent schedules too.

So is there any way to add intial delay to ZSchedule ?


Solution

  • After having a small discussion with community it seems that expected behaviour is not achievable with Schedule (ZIO version: 1.0.0-RC15), since it is used to repeat after the first execution.

    I've ended up using ZIO.sleep() and schedule I made in the original post:

    val schedule = repeatUntilTimeLimitReached *> untilTermination       
    
    for {
      _     <- ZIO.sleep(initialDelay)
      state <- doStuff().repeat(schedule)
    } yield state