我有一个图表接受一系列文件,逐个处理它们然后在执行结束时,如果所有执行成功或失败,程序应该返回成功(0)或失败(-1) 。
如何才能实现最后一步?接收器如何知道它何时收到最后一个文件的结果?
val graph = createGraph("path-to-list-of-files")
val result = graph.run()
def createGraph(fileOrPath: String): RunnableGraph[NotUsed] = {
printStage("PREPARING") {
val producer: Source[ProducerFile, NotUsed] = Producer(fileOrPath).toSource()
val validator: Flow[ProducerFile, ProducerFile, NotUsed] = Validator().toFlow()
val provisioner: Flow[ProducerFile, PrivisionerResult, NotUsed] = Provisioner().toFlow()
val executor: Flow[PrivisionerResult, ExecutorResult, NotUsed] = Executor().toFlow()
val evaluator: Flow[ExecutorResult, EvaluatorResult, NotUsed] = Evaluator().toFlow()
val reporter: Sink[EvaluatorResult, Future[Done]] = Reporter().toSink()
val graphResult = RunnableGraph.fromGraph(GraphDSL.create() { implicit builder: GraphDSL.Builder[NotUsed] =>
import GraphDSL.Implicits._
producer ~> validator ~> provisioner ~> executor ~> evaluator ~> reporter
ClosedShape
})
printLine("The graph pipeline was created")
graphResult
}
答案 0 :(得分:1)
您的Future[Done]
接收器已实现为 val graphResult: RunnableGraph[Future[Done]] = producer
.via(validator)
.via(provisioner)
.via(executor)
.via(evaluator)
.toMat(reporter)(Keep.right)
,如果您希望在处理完所有元素后运行某些代码,则可以挂钩。
但是,目前您没有在图表中公开它。虽然有一种方法可以使用图形DSL公开它,但在您的情况下,使用流畅的DSL更容易实现这一点:
Future[Done]
这会在您运行图表时返回val result: Future[Done] = graph.run()
result.onComplete {
case Success(_) => println("Success!")
case Failure(_) => println("Failure..")
}
然后你可以挂钩 - 例如
{{1}}