Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active January 22, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/4290106c60854b69b49b74a7499ce753 to your computer and use it in GitHub Desktop.
Save adamw/4290106c60854b69b49b74a7499ce753 to your computer and use it in GitHub Desktop.
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