How can I cancel workflow execution within activityInboundInterceptors?

hi, i’m new to temporal. I am working on a project where i need to do some extra logic each time the workflow execution / activity is about to execute. I plan to use interceptor.
In my CustomWorkflowInboundCallsInterceptor, i can directly throw a CanceledFailure, and the workflow will be canceled. However, if i did the similar thing in my CustomActivityInboundCallsInterceptor, it simply doesn’t work. I also tried to throw a ApplicationFailure? but adding try-catch in @workflowMethod still not working. (i guess the reason this workflow and activity are running async on different threads? so simply throw an exception from activity won’t affect workflow… )
but how can i solve this problem. Is there any good solutions for my scenario?

By definition, workflow cancellation is an operation external to the workflow or activity. So, a workflow or activity shouldn’t cancel itself. It can either complete or fail.

thanks for response.
Just for clarifying, i want the workflow to do pre-stop logic and then ‘fail’. I now achieve this through this strategy:

  1. in CustomWorkflowInboundCallsInterceptor, if the workflow cannot execute, i just throw FailureException.
  2. in CustomActivityInboundCallsInterceptor, if the workflow should stop, I throw ApplicationFailure with specified types, no retry. In workflowMethod, i use a try-catch for this exception and then do pre-stop logic.

I got some question here:

  1. is there any advice on my logic? how to implement this in a better way?
  2. in Interceptors, if I am calling any other services, is it better to use activity (in demo, i just injected services using spring)

Thanks

Your approach is sound. Failing workflows and activities is fully supported.

You must use activities to call external services from the workflow code. And workflow interceptors operate with the same constraints.

1 Like

Thanks again for kindly replying:)

Do you have any samples for using activity in interceptors?

In my code, I create AcitivityStub with options in my customWorkflowInboundsCallsInterceptor, it does call the activity. But later there raises a exception with a conflict which says ‘the next activity is not the activity defined in my workflow’… Also I also override the activity interceptor’s execute method.

How can I solve this?

Again, I’m calling an external services that would decide whether my workflow should proceed or not. Should I put all this inside a SideEffect?

Thanks!

‘the next activity is not the activity defined in my workflow

Could you post the whole exception? I don’t think this comes from Temporal SDK.

This problem is solved. I now learned that i have to carefully set the options when creating these workers and component.

I have some other questions. The first is that

  1. when I need to use external services to decide whether my workflow or activity should proceed, is it better to use SideEffect or just call in activity?
  2. can i send signals to other workflow and wait for response in interceptor? Is this a good design?

Thanks.

  1. The best approach is to make those services notify workflow through a signal. But you can implement a polling activity if needed. This post gives options for polling.
  2. Yes, you can. Here is an example where an interceptor is listening to a signal. It is hard to recommend the design without understanding the use case.
1 Like