Setting local variables in multi-instance subprocess with Zeebe API

Hello everyone,

I am not sure if this belongs to the Workflow Execution or Zeebe Client category. I have the following use case:

A multi-instance subprocess is used to process a list of elements. The first task within the subprocess can throw different exceptions in the worker implementation. For most exceptions, the job is failed, using the built-in retry mechanism of Zeebe.

However, for a specific exception, id like to have my own retry mechanism. Therefore, if this specific exception occurs, a business error is thrown with the Zeebe client to trigger the alternate workflow. Each of the instances needs its own retries counter that is decreased by one each time the business error is thrown so that the handler is called when no retries are left.
To ensure that each of the instances has its own retry counter, the subprocess has an input mapping that sets the initial value of retries. If i get the documentation right, this ensures that each instance has its own value.

My question is: How can i decrease the retries value for a specific subprocess instance? My idea was to use the Zeebe client before throwing the business error to set the retries process variable to a lower value (using ZeebeClient.newSetVariablesCommand), but i can only get the elementInstanceKey and processInstanceKey of the current job. It seems like both are not able to set the variable for the subprocess instance.

Simplified code:

public void handle(JobClient client, ActivatedJob job) {
    try {
      ...
    } catch(SpecificException se) {
      client.newThrowErrorCommand(job).errorCode("my-code").send().join();
      // Decrease the 'retries' variable here
    } catch(Exception exception) {
      client.newFailCommand(job.getKey())/...
    }
}

Is there any way to get the key of the scope of the subprocess instance to set its retries variable? Or should i model my BPMN differently?

Thanks in advance,
Janek

Hi @janekberg

This is an interesting case. I don’t think that this is supported at the moment. You will have to work around this limitation with an alternative modelling approach.

One way you could do this is to complete the job with success, but set a “businessFailure: true” variable as you complete it (and decrement businessFailureRetries), and then examine this in a conditional gateway directly after the Task with potential failure.

As well, you might like to open this as a feature request in the Zeebe GitHub repo - setVariables in specific subprocess instance scope.

Josh

1 Like

Hey @jwulf ,
thanks for your reply! I was already suspecting that this would not be possible with the current implementation. Your suggestion seems to be a good fit for me - although it couples the task with logic that does not belong to it directly, but I can live with that :slight_smile:
I think the only alternative would be to have a separate task for decreasing the counter, but I think this less suitable because it creates communication overhead for a simple decrease.

Anyway, I now know how to proceed, so thank you again. I will consider creating a feature request once I find time to think through what exactly the client could offer for this. After all, I think this could be useful for others, too.

Janek

1 Like