How to define cleanup for workflow cancellations (typescript-sdk)


There are some Workflows we intentionally cancel mid-execution when we detect additional work is not needed. Normally at the end of these Workflows we run a cleanup activity to get rid of tmp files created by the workflow. However, our cleanup activity is never run when Workflows are cancelled mid-exec. Is there a way to configure this activity to also be run when a workflow is cancelled?

background info

If it makes any difference, Cancellation is triggered from within an activity with a slightly more graceful version of:

let workflowId = Context.current().info.workflowExecution.workflowId
let handle = wfClient.getHandle(workflowId);
await handle.cancel();

Looked at the typescript-sdk-docs on failure handling and the part on cancelled-failure links to a 404.

Helle @Llama_D_Attore

Cancellation is delivered to your activities through heartbeat, do your activities heartbeat?

if so, in your workflow code you should be able to catch the CancelledFailure error thrown by your activity when it is cancelled and run the cleanup activity.

Can you paste here the text that contains the broken link, I am not able to find it.

Thank you.

To ensure the activity will be run, it should be executed in a non cancellable cancellation scope.
See this example for reference: Cancellation Scopes | Legacy documentation for Temporal SDKs