Multi-instance Compensation

I’m not sure if this is a Process Engine or Modeler question, and I’ve read a few posts about this in the forum archives, but I’m struggling with multi-instance compensation. I have a the attached model, but I can’t seem to get the compensating task to fire.

TestMultiComp.bpmn (6.9 KB)

I’m running the following JavaScript external task processor:

const { Variables, Client, logger } = require('camunda-external-task-client-js');
const open = require('open');

const config = { baseUrl: 'http://localhost:8080/engine-rest', use: logger, asyncResponseTimeout: 10000 };

// create a Client instance with custom configuration
const client = new Client(config);

// subscribe to the topic: 'do-something-topic'
client.subscribe('do-something-topic', async function({ task, taskService }) {

  // Get a process variable
  const item = task.variables.get('item');

  // set a local variable 'item'
  const localVariables = new Variables();
  localVariables.set("item", item);  

  console.log(`Do something with '${item}'...`);

  if (item == 'three') {
      // Raise Error
      console.log(`Error '${item}'...`);
      await taskService.handleBpmnError(task, "ERR_001", "Error Message");
  } else {
      // Complete the task
      console.log(`Complete '${item}'...`);
      await taskService.complete(task, null, localVariables);
  }
});

// subscribe to the topic: 'compensate-topic'
client.subscribe('compensate-topic', async function({ task, taskService }) {
  
    console.log(`Rollback...`);

    const values = task.variables.getAll();
    console.log(values);

    // Complete the task
    await taskService.complete(task);

});

The goal is to compensate any tasks that were started by the multi-instance subprocess.

Any help understanding how I should be modeling this or handling the compensation with external task would be would be appreciated.

1 Like

So this issue here is actually a BPMN one.
Compensation can only be done on tasks that have been successfully completed. Which means you probably trigger the compensation correctly but because the task is cancelled by an error event it’s not considered to have been successfully completed and so it’s not going to trigger.

If you’d like to handle thing kind of compensation for external tasks, you have a lot of possibilities from a BPMN perspective, one idea close to your own is to complete the task with a variable that can be used to trigger the compensation. Haven’t tested it, but it should work

Niall,

I finally had a chance to circle back on this. I was able to experiment with this and got the following working, so thank you for the pointer there.

Now I’m trying to access local variables set in the muli-instance activity from the compensation activity, but they are not showing up. I’ve read and it seems there are some issues around this. Is there a pattern I should follow that allows me to track what activity I’m compensating from the compensation task?

After some more research, it seems like I’m hitting this issue: https://jira.camunda.com/browse/CAM-4268.