Safely aborting a workflow after a specific timeout interval

Hello!

I’m working on our first proof-of-concept workflow, it’s intended to be invoked by a user’s request and run synchronously until completion.

I’d like this workflow to have a timeout (say 5s), after which it should abort processing, clean up after itself by rolling back any mutations, and return an error to the user.

As I’d like to clean up, it sounds like the global workflow timeouts are not a good fit, and the docs indicate using a Timer (so sleep() in Typescript). But the documentation doesn’t give any more advice and I can’t find examples.

Would the approach be to wrap the entire workflow body in a CancellationScope and do a Promise.race() with the timeout timer? Or is there a better way?

I recommend using UpdateWithStart for this use case. This allows returning an error to the user and continuing with cleanup asynchronously. The exact mechanism of canceling the currently executing logic might differ, but CancellationScope is the best approach in most cases.