energi
energi

Reputation: 79

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 ?

Upvotes: 2

Views: 1880

Answers (2)

pme
pme

Reputation: 14803

Why not add another Schedule and compose them:

val delayedSchedule = Schedule.once.delayed(_ + 12.seconds)

val schedule = delayedSchedule *> repeatUntilTimeLimitReached *> untilTermination    

I played a bit with this and you need to flatMap the Schedules.

Here I use an example that uses zio.console:

import zio.console._
import zio.duration._
import zio.{App, Schedule, ZIO}

  val s = Schedule.spaced(1.second)
  for {
    _ <- putStrLn("Start")
    _ <- putStrLn("Initial Delay").delay(5.seconds)
    _ <- putStrLn("Repeated Delay").repeat(s)
  } yield ()

Upvotes: 1

energi
energi

Reputation: 79

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

Upvotes: 2

Related Questions