Skip to content

Commit 95668dd

Browse files
authored
Record a delete reason when an error event sub-process interrupts. (#4258)
1 parent 627f574 commit 95668dd

5 files changed

Lines changed: 216 additions & 2 deletions

File tree

‎modules/flowable-engine/src/main/java/org/flowable/engine/impl/bpmn/helper/ErrorPropagation.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.flowable.engine.delegate.BpmnError;
4848
import org.flowable.engine.delegate.DelegateExecution;
4949
import org.flowable.engine.delegate.event.impl.FlowableEventBuilder;
50+
import org.flowable.engine.history.DeleteReason;
5051
import org.flowable.common.engine.impl.callback.CallbackData;
5152
import org.flowable.common.engine.impl.callback.RuntimeInstanceStateChangeCallback;
5253
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
@@ -298,14 +299,16 @@ protected static void executeEventHandler(Event event, ExecutionEntity parentExe
298299
LOGGER.debug(
299300
"Ending and deleting child executions for parent execution '{}'. Reason: Parent Execution is processIntanceType. Current Execution '{}'",
300301
parentExecution, currentExecution);
301-
executionEntityManager.deleteChildExecutions(parentExecution, null, true);
302+
executionEntityManager.deleteChildExecutions(parentExecution,
303+
DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + event.getId() + ")", true);
302304
} else if (!currentExecution.getParentId().equals(parentExecution.getId())) {
303305
LOGGER.debug("Planing destroyScopeOperation for execution {}. Reason: {}. Parent execution: {}", currentExecution,
304306
"Current execution parentId odes not match parentExecution id", parentExecution);
305307
CommandContextUtil.getAgenda().planDestroyScopeOperation(currentExecution);
306308
} else {
307309
LOGGER.debug("Deleting execution and related data for execution {}.", currentExecution);
308-
executionEntityManager.deleteExecutionAndRelatedData(currentExecution, null, false);
310+
executionEntityManager.deleteExecutionAndRelatedData(currentExecution,
311+
DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(" + event.getId() + ")", false);
309312
}
310313

311314
ExecutionEntity eventSubProcessExecution = executionEntityManager.createChildExecution(parentExecution);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.engine.test.api.deletereason;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
import java.util.List;
18+
19+
import org.flowable.engine.history.DeleteReason;
20+
import org.flowable.engine.history.HistoricActivityInstance;
21+
import org.flowable.engine.impl.test.PluggableFlowableTestCase;
22+
import org.flowable.engine.runtime.ProcessInstance;
23+
import org.flowable.engine.test.Deployment;
24+
import org.flowable.task.api.Task;
25+
import org.junit.jupiter.api.Test;
26+
27+
/**
28+
* Companion to {@link DeleteReasonTest#testInterruptingBoundaryEvent()}, for the
29+
* interrupting error event sub-process.
30+
*
31+
* An activity terminated by an interrupting start event is recorded with
32+
* DeleteReason.EVENT_SUBPROCESS_INTERRUPTING, so history can tell it apart from
33+
* one that completed normally.
34+
*/
35+
public class ErrorEventSubProcessDeleteReasonTest extends PluggableFlowableTestCase {
36+
37+
/**
38+
* A forked process: one branch parks on a user task inside a call activity,
39+
* the other throws a BpmnError from a start execution listener. The
40+
* interrupting error event sub-process catches it and terminates the whole
41+
* scope, including the parked branch.
42+
*/
43+
@Test
44+
@Deployment(resources = "org/flowable/engine/test/api/deletereason/ErrorEventSubProcessDeleteReasonTest.bpmn20.xml")
45+
public void testInterruptingErrorEventSubProcess() {
46+
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("errorEventSubProcessDeleteReason");
47+
48+
waitForHistoryJobExecutorToProcessAllJobs(7000, 100);
49+
50+
// The parked branch's user task was destroyed by the interruption.
51+
assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isZero();
52+
assertThat(runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).count()).isZero();
53+
54+
HistoricActivityInstance parkedBranch = historicActivity(processInstance.getId(), "waitingCall");
55+
56+
// An end time and a called process instance are also what a call
57+
// activity that ran to completion records, so the delete reason is the
58+
// only thing separating the two.
59+
assertThat(parkedBranch.getEndTime()).isNotNull();
60+
assertThat(parkedBranch.getCalledProcessInstanceId()).isNotNull();
61+
62+
assertThat(parkedBranch.getDeleteReason())
63+
.as("delete reason on an activity terminated by the interrupting error event sub-process")
64+
.isEqualTo(DeleteReason.EVENT_SUBPROCESS_INTERRUPTING + "(errorHandlerStart)");
65+
}
66+
67+
/**
68+
* The same BpmnError caught by an interrupting boundary event instead, which
69+
* records DeleteReason.BOUNDARY_EVENT_INTERRUPTING. The two paths differ only
70+
* in which vehicle catches the error.
71+
*/
72+
@Test
73+
@Deployment(resources = "org/flowable/engine/test/api/deletereason/ErrorEventSubProcessDeleteReasonTest.boundary.bpmn20.xml")
74+
public void testInterruptingBoundaryEvent() {
75+
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("boundaryDeleteReason");
76+
77+
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
78+
assertThat(task).isNotNull();
79+
taskService.complete(task.getId());
80+
81+
waitForHistoryJobExecutorToProcessAllJobs(7000, 100);
82+
83+
HistoricActivityInstance subProcess = historicActivity(processInstance.getId(), "theSubProcess");
84+
assertThat(subProcess.getDeleteReason()).contains(DeleteReason.BOUNDARY_EVENT_INTERRUPTING);
85+
}
86+
87+
private HistoricActivityInstance historicActivity(String processInstanceId, String activityId) {
88+
List<HistoricActivityInstance> instances = historyService.createHistoricActivityInstanceQuery()
89+
.processInstanceId(processInstanceId)
90+
.activityId(activityId)
91+
.list();
92+
assertThat(instances).hasSize(1);
93+
return instances.get(0);
94+
}
95+
96+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.engine.test.api.deletereason;
14+
15+
import org.flowable.engine.delegate.BpmnError;
16+
import org.flowable.engine.delegate.DelegateExecution;
17+
import org.flowable.engine.delegate.ExecutionListener;
18+
19+
/** Raises a BpmnError from a start execution listener. */
20+
public class ThrowBpmnErrorListener implements ExecutionListener {
21+
22+
@Override
23+
public void notify(DelegateExecution execution) {
24+
throw new BpmnError("someError", "raised from a start execution listener");
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
3+
xmlns:flowable="http://flowable.org/bpmn"
4+
targetNamespace="deleteReason">
5+
6+
<error id="someError" errorCode="someError"/>
7+
8+
<!--
9+
The same BpmnError as the event sub-process case, caught by a boundary event
10+
instead. BoundaryEventActivityBehavior records
11+
DeleteReason.BOUNDARY_EVENT_INTERRUPTING on the activity it interrupts, so
12+
the only difference between this file and the other one is which vehicle
13+
catches the error.
14+
-->
15+
<process id="boundaryDeleteReason" isExecutable="true">
16+
17+
<startEvent id="start"/>
18+
<sequenceFlow id="toSubProcess" sourceRef="start" targetRef="theSubProcess"/>
19+
20+
<subProcess id="theSubProcess" name="The Sub Process">
21+
<startEvent id="innerStart"/>
22+
<sequenceFlow id="innerToTask" sourceRef="innerStart" targetRef="innerTask"/>
23+
<userTask id="innerTask" name="Inner Task" flowable:assignee="kermit"/>
24+
<sequenceFlow id="innerTaskToError" sourceRef="innerTask" targetRef="innerErrorEnd"/>
25+
<endEvent id="innerErrorEnd">
26+
<errorEventDefinition errorRef="someError"/>
27+
</endEvent>
28+
</subProcess>
29+
30+
<sequenceFlow id="subProcessToEnd" sourceRef="theSubProcess" targetRef="endNormal"/>
31+
<endEvent id="endNormal"/>
32+
33+
<boundaryEvent id="errorBoundary" attachedToRef="theSubProcess" cancelActivity="true">
34+
<errorEventDefinition/>
35+
</boundaryEvent>
36+
<sequenceFlow id="boundaryToEnd" sourceRef="errorBoundary" targetRef="endCaught"/>
37+
<endEvent id="endCaught"/>
38+
39+
</process>
40+
</definitions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
3+
xmlns:flowable="http://flowable.org/bpmn"
4+
targetNamespace="deleteReason">
5+
6+
<process id="errorEventSubProcessDeleteReason" isExecutable="true">
7+
8+
<startEvent id="start"/>
9+
<sequenceFlow id="toFork" sourceRef="start" targetRef="fork"/>
10+
<parallelGateway id="fork"/>
11+
12+
<!-- Branch A: a call activity whose child parks on a user task. -->
13+
<sequenceFlow id="toWaiting" sourceRef="fork" targetRef="waitingCall"/>
14+
<callActivity id="waitingCall" name="Waiting Call" calledElement="waitingChild"/>
15+
<sequenceFlow id="waitingToEnd" sourceRef="waitingCall" targetRef="endA"/>
16+
<endEvent id="endA"/>
17+
18+
<!-- Branch B: a start execution listener raises a BpmnError. -->
19+
<sequenceFlow id="toThrowing" sourceRef="fork" targetRef="throwingTask"/>
20+
<serviceTask id="throwingTask" name="Throwing Task" flowable:expression="${true}">
21+
<extensionElements>
22+
<flowable:executionListener event="start"
23+
class="org.flowable.engine.test.api.deletereason.ThrowBpmnErrorListener"/>
24+
</extensionElements>
25+
</serviceTask>
26+
<sequenceFlow id="throwingToEnd" sourceRef="throwingTask" targetRef="endB"/>
27+
<endEvent id="endB"/>
28+
29+
<!-- Interrupting error event sub-process: catches, and terminates the scope. -->
30+
<subProcess id="errorHandler" triggeredByEvent="true">
31+
<startEvent id="errorHandlerStart" isInterrupting="true">
32+
<errorEventDefinition/>
33+
</startEvent>
34+
<sequenceFlow id="handlerToEnd" sourceRef="errorHandlerStart" targetRef="errorHandlerEnd"/>
35+
<endEvent id="errorHandlerEnd"/>
36+
</subProcess>
37+
38+
</process>
39+
40+
<!-- Branch A's called process: raises a user task and waits. -->
41+
<process id="waitingChild" isExecutable="true">
42+
<startEvent id="childStart"/>
43+
<sequenceFlow id="childToTask" sourceRef="childStart" targetRef="waitingTask"/>
44+
<userTask id="waitingTask" name="Waiting Task" flowable:assignee="kermit"/>
45+
<sequenceFlow id="childTaskToEnd" sourceRef="waitingTask" targetRef="childEnd"/>
46+
<endEvent id="childEnd"/>
47+
</process>
48+
</definitions>

0 commit comments

Comments
 (0)