Skip to content

Instantly share code, notes, and snippets.

@natansil
Last active May 23, 2021 19:35
Show Gist options
  • Save natansil/f274d223dc549c10e326b4c056ca91b6 to your computer and use it in GitHub Desktop.
Save natansil/f274d223dc549c10e326b4c056ca91b6 to your computer and use it in GitHub Desktop.
object InterruptExample extends App {
import zio._
import zio.duration._
import java.time.LocalTime
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
for {
fiber <- runSomeLongJob
.ensuring(printWithTime("finalizing job!"))
.forkDaemon
_ <- printWithTime("do stuff").repeat(Schedule.spaced(1.seconds) && Schedule.recurs(2))
_ <- fiber.interrupt
_ <- printWithTime("do other stuff").repeat(Schedule.spaced(1.seconds))
} yield ExitCode(0)
private def runSomeLongJob = {
printWithTime("long running job...") *> clock.sleep(100.seconds)
}
private def printWithTime(msg: String) =
console.putStrLn(s"${LocalTime.now().toString} $msg")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment