use a partition to decide if a flow should go to next flow and if not have the stream pick it up next tick from the beginning (Akka)

怎甘沉沦 提交于 2020-12-13 04:57:05

问题


I want to create a Flow that have 4 main steps (FlowShapes), and after the first and the second I want to have partition that will decide if there is a reason to go to the next, and if not to sink it so the stream will pick it up later and start from beginning, but im not sure this is the way, cause I just used Sink.ignore, it looks like this:

  def mainFlow: Flow[MyGraphElement, MyGraphElement, NotUsed] = Flow.fromGraph(GraphDSL.create() { implicit builder =>

    // FlowShape's
    // those flow shapes preforming api calls + updating the db with result
    val firstShape = builder.add(firstFlowShape)
    val secondShape = builder.add(secondFlowShape)
    val thirdShape = builder.add(thirdFlowShape)
    //  this flow shape will set the task to beginning mode so it will be picked up again by the stream
    val clearTaskShape = builder.add(clearTaskFlowShape)
    //  this flow shape will decide if this element needs to move to final status so it wont be pickd up by this stream anymore, otherwise it will also clear the task so it will be picked up again by the stream
    val evaluatorShape = builder.add(evaluatorFlowShape)

    //  UniformFanOutShape's
    // based on certain fields I want to decide here if there is need to go to the next step, if no need I want to sink the element and have the stream pick it up next time
    val shouldGoToSecondShape = builder.add(shouldGoToSecondShapePart)
    val shouldGoToThirdSahpe = builder.add(shouldGoToThirdShapePart)

    firstShape ~> shouldGoToSecondShape
          shouldGoToSecondShape.out(1) ~> clearTaskShape ~> Sink.ignore
          shouldGoToSecondShape.out(0) ~> secondShape ~> shouldGoToThirdSahpe
                shouldGoToThirdSahpe.out(1) ~> clearTaskShape ~> Sink.ignore
                shouldGoToThirdSahpe.out(0) ~> thirdShape ~> evaluatorShape


    FlowShape(firstShape.in, evaluatorShape.out)
  })

  def shouldGoToSecondShapePart: Partition[MyGraphElement] = {
    val portMapper = (elem: MyGraphElement) =>
      if (elem.myElement.someField == ProcessingStatus.Done ) 0 else 1
    Partition[MyGraphElement](2, portMapper)
  }

  def shouldGoToThirdShapePart Partition[MyGraphElement] = {
    val portMapper = (elem: MyGraphElement) =>
      if (elem.myElement.someOtherField == ProcessingStatus.Done ) 0 else 1
    Partition[MyGraphElement](2, portMapper)
  }

what is the right way todo so?

来源:https://stackoverflow.com/questions/65115427/use-a-partition-to-decide-if-a-flow-should-go-to-next-flow-and-if-not-have-the-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!