Workflow Interceptor calling multiple times

Hello,

I have a use case where we need to publish events whenever a new workflow started and completed.
We used a class which extends WorkflowInboundCallsInterceptorBase to achieve this

Code as below

public class TestWorkflowInboundInterceptor extends WorkflowInboundCallsInterceptorBase {
private WorkflowInfo workflowInfo;
public TestWorkflowInboundInterceptor(WorkflowInboundCallsInterceptor next) {
super(next);
this.workflowInfo = Workflow.getInfo();
}

@Override
public WorkflowOutput execute(WorkflowInput input) {
        log.info("Workflow Started");
		//Publish Events saying workflow started 
        WorkflowOutput output = null;
        try {
            output = super.execute(input);
            log.info("Workflow Output Response: {} ", output.getResult());
			//Publish Events saying workflow Completed 
            log.info("Workflow Completed");

        } catch (Exception e) {
            log.info("Workflow Failed");
			//Publish Events saying workflow Failed 
            throw e;
        }
}

}

Our code is deployed in multinode architecture ( worker running in 8 nodes) .
Ideally whenever we send a new WF request i should be seeing only one Workflow Started and one Workflow Completed/Workflow Failed. but we are noticing the
interceptor is being called multiple times and the events are published multiple times , in some case multiple times in the same node

Workflow interceptor should be instantiated once per execution.
Can you overrider init method, something like

@Override
public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
  this.workflowInfo = Workflow.getInfo();
  // init workflow outbound interceptor if needed
}

and then in execute method log workflow runid from workflowInfo

It is executed in the workflow context. Send events from activities

@Maxim, i need to send events only when workflow initiated and completed. sending from activities wont create duplicate events ?

can you please share sample code.

I don’t understand why sending from activities will create duplicate events? Your activities must be idempotent or if you are OK with data loss you can disable retries.