Identify process instance is in progress

I have a issue is like, a process is triggered with two identical requests, both instance are in progress hence there is no process-instance id, how can i identify that there is already a request is in progress and cancel any one of instance

Thanks in advance

Hi @gowtham_m,

if you start every process by message and use a unique business key for correlation, you can implement your requirements with this pattern:
grafik

Both message event have to use the same message definition. The event subprocess will cancel all following requests when a process instance is running.

Hope this helps, Ingo

2 Likes

@Ingo_Richtsmeier thanks,

Is there any other way, assume my workflow has a start event , service task and end event.

In this scenario how can I avoid duplicate calls , with the same request.

We have a service which will invoke workflow with rest API… due to some error scenario that service is invoking workflow multiple times within a fraction of second, and due to this request is getting processed twice as from workflow point of view it is two independent calls, two process instance will be there.

Is there any other way… even though there is duplicate only once it should process with the same request

@Niall any help

Seems like you have two new processes started with different process-instance ids for same requests.

Associating a unique ProcessInstanceBusinessKey for each request when starting the process instances.

RuntimeService runtimeService = ..;
String orderId = ..;

ProcessInstance shipmentInstance = runtimeService
  .createProcessInstanceQuery()
  .processInstanceBusinessKey(orderId)
  .singleResult();

Refer the use cases for businesskeys: https://blog.camunda.com/post/2018/10/business-key/

And the below post:

It can be solved by below approaches:

  • By enabling uniqueness of the businessKey field in camunda tables for business-key configuration

  • Configure a ExecutionListener in StartEvent of the process and query by the businessKey in runtime tables and if process already exists throw the exception if already same entity is in process.

      long processCount = execution.getProcessEngineServices().getRuntimeService()
        .createExecutionQuery().processDefinitionKey("aProcessDefinitionKey")
        .tenantIdIn("aTenantId").processInstanceBusinessKey("aBusinessKey")
        .active().count();
          if (processCount > 0) {
             throw new RuntimeException("Process already exists for businessKey: " 
              + execution.getBusinessKey());
           }

@aravindhrs thanks,
i tried above thing, when process in active state i mean still is in progress(as per example i mentioned it is still in service delegate) the above queries returns null.

i am verify with two instances locally with diff port and with the same request , with a debugger: in both the instance it returns process instance as null. so duplicate process happens :frowning:

@gowtham_m can you upload your bpm design and how you’re validating for the process duplication?

  • What is your process instance query looks like?
  • Have you associated businesskeys for each process? If yes, what are those businesskeys?
  • Both process has same businesskey or unique?
  • Are your processes are tenant specific?
  • Have you configured the ExecutionListener to check for process duplication? If yes, in which event type you hooked the listener?

ProcessInstanceQuery query = camunda.getRuntimeService().createProcessInstanceQuery()
.processDefinitionKey(“PROCESS_WORKFLOW”)
.processInstanceBusinessKey(execution.getProcessBusinessKey());

	ProcessInstance instance=query.singleResult();

{
“variables”: {
“refId”: {
“value”: “5e7efa5ea05acc0009eb2430”,
“type”: “String”
},
“refId2”: {
“value”: “5e7efa5ea05acc0009se242b”,
“type”: “String”
}, “refId3”: {
“value”: “5e7efa5f702fea00092049361”,
“type”: “String”
}
},
“businessKey” : “5e7efa5f702fdc0009204961-1”

}

Both the process have same businessKey

for testing purpose i added this in a service delegate

You’re starting the process instances with businessKey?

(or)

After starting the process instance, you are populating the businesskey for each process in delegate?

i am starting the process instance with the business key,

in which event type of start event activity you hooked the listener?

process has a normal start event, Capture

i am checking in callrecordservice delegate

Process instance will get created in start event itself. There’s no use of checking process duplication in service task. Handling in service task won’t prevent process duplication. Handle it in createvent in the Start event activity. You can reuse the delegates as listeners in start event execution listener. Start the process instance asynchronously by marking async:before=“true” for the StartEvent in your bpmn.

@aravindhrs let me try that, i have question here, when process instance is created and if i try to check with above queries with run time process instance id , it should return a single result right

@gowtham_m it will return only if the process instance is active. To query for completed process instances you need to query history tables.

@aravindhrs ya right., in my case still process is not active , it is still in execution phase right, i see above query returns null .
i have seen query returns true, i process is active and pending at manual test or time event like that.

Let me check, using execution listener

if process is not active means, it can be suspended, not started or already completed.

Hi @gowtham_m

In this scenario, a query beforehand will always return null, as I assume that the first process instance is not committed in the database when the second process instance is started. Both transactions will overlap and commit sucessfully.

To avoid duplication here, you have to make the business key a unique index the database. Then the second (and all other subsequence processs instances) can not get commited in the database and will throw an exception.

Hope this helps, Ingo

We can start the process instance asynchronously and have a check in the execution listener of the start activity in the “create” event itself.

it will affect globally other processes too.

Hi @aravindhrs,

yes it will. But you can add a new table using a unique index in the camunda database and use this for synchronizing, if the unique business key is to restrictive.

The new table needs more effort to setup and fill with data, but should be possible.

Cheers, Ingo