Loops
Loops are essential for workflows that need to repeat actions until a condition is met. This operation enables iterative processing, such as retrying operations, processing collections of items, or implementing polling mechanisms that continue until specific criteria are satisfied.
warning
Loops without timers or signals will be effectively busy loops, possibly causing journal growth and other problems. Consider adding a short timer to slow it down. In the future this will be detected by the linter.
val step1 = WIO.pure(MyState(1)).autoNamed
val step2 = WIO.pure(MyState(1)).autoNamed
val loop: WIO[MyState, Nothing, MyState] = WIO
  .repeat(step1)
  .untilSome(state => Some(state))
  .onRestart(step2)
  .named(
    conditionName = "Is everything done?",
    releaseBranchName = "Yes!",
    restartBranchName = "No",
  )
Rendering Outputs
- Flowchart
- BPMN
- Model
{
  "base" : {
    "meta" : {
      "name" : "Step1",
      "error" : null
    },
    "_type" : "Pure"
  },
  "onRestart" : {
    "meta" : {
      "name" : "Step2",
      "error" : null
    },
    "_type" : "Pure"
  },
  "meta" : {
    "conditionName" : "Is everything done?",
    "exitBranchName" : "Yes!",
    "restartBranchName" : "No"
  },
  "_type" : "Loop"
}
Drafting Support
Loops come with drafting support.
// Create a draft loop with a timer to avoid busy loops
val processStep = WIO.draft.step("Process Item")
val waitStep    = WIO.draft.timer("Wait before retry", duration = 1.minute)
val loop = WIO.draft.repeat(
  conditionName = "Is processing complete?",
  releaseBranchName = "Yes",
  restartBranchName = "No",
)(
  body = processStep >>> waitStep,
  onRestart = WIO.draft.step("Reset for retry"),
)
Rendering Outputs
- Flowchart
- BPMN
- Model
{
  "base" : {
    "steps" : [
      {
        "meta" : {
          "name" : "Process Item",
          "error" : null,
          "description" : null
        },
        "_type" : "RunIO"
      },
      {
        "meta" : {
          "duration" : "PT1M",
          "releaseAt" : null,
          "name" : "Wait before retry"
        },
        "_type" : "Timer"
      }
    ],
    "_type" : "Sequence"
  },
  "onRestart" : {
    "meta" : {
      "name" : "Reset for retry",
      "error" : null,
      "description" : null
    },
    "_type" : "RunIO"
  },
  "meta" : {
    "conditionName" : "Is processing complete?",
    "exitBranchName" : "Yes",
    "restartBranchName" : "No"
  },
  "_type" : "Loop"
}