Last active
May 23, 2021 19:35
-
-
Save natansil/f274d223dc549c10e326b4c056ca91b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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