All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sss.openstar.tools.Retry.scala Maven / Gradle / Ivy

package sss.openstar.tools

import sss.ancillary.Logging

import scala.concurrent.duration.Duration
import scala.util.{Failure, Try}

object Retry extends Logging {
  def retry[T](n: Int, interval:Duration)(f: => T): Try[T] = {
    val tries = LazyList.fill(n)(Try {
      //todo basic impl for testing
      Thread.sleep(interval.toMillis)
      f
    } recoverWith {
      case e =>
        log.warn(e.getMessage)
        Failure(e)
    })
    tries
      .find(_.isSuccess)
      .getOrElse(tries.head)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy