Last active
February 15, 2016 08:50
-
-
Save mathieuancelin/1422dde39fd85991992f 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
package batches.test | |
import scala.concurrent.{Promise, ExecutionContext, Future} | |
import scala.util.{Failure, Success} | |
object impl { | |
case class Step[T](step: T, e: Exception) extends RuntimeException() | |
implicit final class StepableFuture[A](future: Future[A]) { | |
def withStep[T](name: T)(implicit ec: ExecutionContext): Future[A] = { | |
val p = Promise[A] | |
future.onComplete { | |
case Success(a) => p.trySuccess(a) | |
case Failure(err) => p.tryFailure(err) | |
} | |
p.future | |
} | |
} | |
} | |
object test { | |
import impl._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
sealed trait BatchStep | |
object BatchStep1 extends BatchStep | |
object BatchStep2 extends BatchStep | |
object BatchStep3 extends BatchStep | |
def work1(): Future[Boolean] = ??? | |
def work2(i: Boolean): Future[Int] = ??? | |
def work3(i: Int): Future[String] = ??? | |
def work(): Unit = { | |
val truc: Future[String] = for { | |
a <- work1().withStep[BatchStep](BatchStep1) | |
b <- work2(a).withStep[BatchStep](BatchStep2) | |
c <- work3(b).withStep[BatchStep](BatchStep3) | |
} yield c | |
truc.recover { | |
case impl.Step(BatchStep1, e) => | |
case impl.Step(BatchStep2, e) => | |
case impl.Step(_, e) => | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment