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

beaker.common.util.Retry.scala Maven / Gradle / Ivy

The newest version!
package beaker.common.util

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

/**
 * A retry scheduler.
 */
trait Retry {

  /**
   * Retries until the specified task completes successfully.
   *
   * @param f Task.
   * @param ec Implicit execution context.
   * @return Successful result.
   */
  def ensure[T](f: => Future[T])(implicit ec: ExecutionContext): Future[T] =
    f recoverWith { case _ => ensure(f) }

  /**
   * Retries until the specified task completes successfully.
   *
   * @param f Task.
   * @return Successful result.
   */
  def ensure[T](f: => Try[T]): Try[T] =
    f recoverWith { case _ => ensure(f) }

  /**
   * Retries until the specified condition is true.
   *
   * @param f Condition.
   */
  def ensure(f: => Boolean): Unit =
    if (!f) ensure(f)

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy