Overarching boolean to exit/complete the workflow

Hello guys,

I have a workflow which has a lot of nested if statements that are doing a whole load of operations based on other factors, now, you should be able to exit all of these if the boolean value for “complete” is true through a Signal method, and complete the workflow, which can happen at any time.

is there a way to have an overarching, higher level check on bool “complete” without needing to check(complete) it on every stage of the if statement.

Thanks!

public String process() {
    check(paymentReceived);

        if (conditionA) {
            check(complete);
            // code logic
            if (conditionB) {
                check(complete);
                // code logic
                if (conditionC) {
                    check(complete);
                    // code logic
                      
                         etc...
                }
            }
        }

        return "FINAL RESULT";
}

BUMPING up as this is valid case for me as well

Hi,

is there a way to have an overarching, higher level check on bool “complete” without needing to check(complete) it on every stage of the if statement.

No exactly this, but you can create a cancellation scope and wrap everything inside, and cancel the scope when a signal comes

And use workflowInit to initialize the scope (see Handling Signals, Queries, & Updates | Temporal Platform Documentation) , or check the scope is not null in your signal handler

something like


  @WorkflowInit
  public HelloWorkflowImpl() {
    scope = Workflow.newCancellationScope(() -> {
      //your code
    });
  }
  
  //signal
  public void setComplete() {
    scope.cancel(); // cancel the scope when signal is received
  }

  //main workflow method
  public String process() {
    try {
        scope.run();
    } catch (TemporalFailure e) {

      Workflow.newDetachedCancellationScope(() -> {
        // cleanup activities if needed
      }).run();
    }

    return ... ;
  }

Thank you Antonio, this is exactly what i was looking for