Last active
January 22, 2020 15:37
-
-
Save adamw/4290106c60854b69b49b74a7499ce753 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import zio._ | |
import zio.clock.Clock | |
import zio.duration._ | |
class Example4(emailService: EmailService) { | |
// Schedule an email to each address, but send at most 10 in parallel | |
def welcomeUsers(emails: List[String]): RIO[Clock, Unit] = | |
ZIO.foreachParN_(10)(emails)(sendWelcomeEmail) | |
// Try to send a welcome email at most 5 times, | |
// sleeping for 1 second between attempts | |
def sendWelcomeEmail(recipient: String): RIO[Clock, Unit] = | |
emailService | |
.send(recipient, "Welcome") | |
.retry( | |
Schedule.spaced(1.second).both(Schedule.recurs(5)) | |
) | |
} | |
trait EmailService { | |
def send(to: String, body: String): Task[Unit] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment