Clean up strategy when workflow failed or completed

Hi
We are planning to implement some cleanup when a workflow is failed(not retriable) and when the workflow is completed successfully. We are failing the workflow for any Application exceptions.

  WorkflowImplementationOptions wfio = WorkflowImplementationOptions.newBuilder()
            .setFailWorkflowExceptionTypes(Throwable.class)
            .build();

Workflow.execute(){
 try {
      result = compuetResult(flowInput);
      cleanUpWorkflow(...);
    }
    catch(TemporalFailure tEx) {
        temporalException = tEx;
        Throwable causeEx = tEx.getCause();
        boolean applicationFailure = false;
        while(causeEx != null){
            if(causeEx instanceof ApplicationFailure){
                ApplicationFailure appEx = (ApplicationFailure)causeEx;
              applicationFailure = true;
           }
          causeEx = causeEx.getCause();
       }
      if(applicationFailure) {
        cleanUpWorkflow(...);
      }
    }
    catch (MyCustomException) {
      cleanUpWorkflow(...);
    }
}

When workflows throw WorkflowTaskTimeout it goes to catch(TemporalFailure tEx) block and the workflow is replayed, so i had to add

      if(applicationFailure) {
        cleanUpWorkflow(...);
      }

I want to know if there is any better way, I mean can we know if an exception triggers replay or failure of workflow based on that i can trigger cleanup.

Any reason you don’t want to perform cleanup on activity timeout for example?

We want to clean up, if we get any runtime exception with our implementation code, or successful implementation code. Hence we added at the end of workflow. When we are doing performance testing, we are getting WorkflowTaskTimeout and the workflow is replayed, so added the check
if(applicationFailure) to avoid the cleanup as that is used in replay

Never think about replay when implementing workflow code besides ensuring that the code is deterministic.

Workflow Task Timeout is not visible to the application code at all, so it is not really possible or needed to handle it in the workflow code.

All your check is doing is not doing cleanup on failures which are not user failures. For example on activity timeouts.