Advice on Managing Workflow Lifecycle or Interceptors

Hello! I had a question about workflow lifecycles.
This is what I know so far: it seems like interceptors is the supported feature to help with “hooking into the lifecycle of workflows”?

  • Middleware/Interceptors came up (/t/workflow-activity-middleware/1142).
  • There was this forum documented feature request (/t/execute-some-code-immediately-after-workflow-completion/298), maybe the Temporal folks have since done it (this was back in 2020).
  • Workflows also have Events (Workflows | Temporal Documentation) that we could maybe hook on to?

Our use case is having branching logic in which we would like to call the same activity. The big one is at each workflow completion, we would like to do X. Hooking into the lifecycle would help reduce duplication and ensure we don’t forget to do X. Are interceptors the right way to go? If so, can I get help/documentation understand the differences in the Workflow offered ones? Or at least some pointers on which one is more appropriate for the use case I described above. Thank you!

P.S. – apologies for the shortened references to community.temporal posts. I’m too new to post more links in one go!

If you want to run an activity at every workflow completion independently of its type, then interceptor is the way to go. Note that interceptors don’t work for terminated workflows, but this is by design.

Having never used interceptors, I will give this a try and post back if I come across any more questions for simple cases.

Ah, one other thing I forgot to mention that we do in situations like this is a try...{WorkflowMethod logic}...finally.

Are there any gotchas to consider when doing so in Temporal?

This should work. Just ensure you run any cleanup logic in a detached cancellation scope.