I’m trying to simplify some Workflow code with regards to cleanup logic:
try {
// run some activities
} catch (ActivityFailure e) {
// error handling
}
catch (CanceledFailure e) {
// run activity to cleanup a record in DB, using NewDetachedCancellationScope
} finally {
// run activity to cleanup a record in DB
}
Basically, I want to run an activity to cleanup a record in DB, whether there was an Activity failure, or if Workflow was canceled, or Workflow completed successfully. This is done in finally.
Is there any drawback to using NewDetachedCancellationScope in the finally clause?
Activity invocations through activity stub throw only ActivityFailure exception. So In case of wf cancellation that happens during activity execution, you will catch ActivityFailure which will have CanceledFailure as its cause. So imo you dont need to catch both ActivityFailure and CanceledFailure failures separately, but for example just catch ActivityFailure e and you can check if its CanceledFailure if you need:
if(e.getCause() instanceof CanceledFailure) {
// do some cancellation cleanup here
}
For your example code you can just catch TemporalFailure which is going to be either ActivityFailure (with the CanceledFailure as cause) or CanceledFailure. If it’s ActivityFailure you can perform some compensation logic if needed for the activities (all in detached scope)