Skip to content

Commit

Permalink
[Fix #3674] Signalling compensation when abortion
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Sep 30, 2024
1 parent 7d7b406 commit c9acf7c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.drools.core.common.InternalKnowledgeRuntime;
import org.jbpm.process.core.Context;
import org.jbpm.process.core.ContextContainer;
import org.jbpm.process.core.context.exception.CompensationScope;
import org.jbpm.process.core.impl.XmlProcessDumper;
import org.jbpm.process.core.impl.XmlProcessDumperFactory;
import org.jbpm.process.instance.ContextInstance;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.workflow.core.WorkflowProcess;
import org.kie.api.definition.process.Process;
import org.kie.api.runtime.rule.Agenda;
Expand Down Expand Up @@ -152,9 +154,16 @@ public void setState(final int state, String outcome) {
}

public void internalSetState(final int state) {
if (state == KogitoProcessInstance.STATE_ABORTED && automaticCompensation()) {
signalEvent(Metadata.COMPENSATION, CompensationScope.IMPLICIT_COMPENSATION_PREFIX + process.getId());
}
this.state = state;
}

private boolean automaticCompensation() {
return process.getMetaData().containsKey(Metadata.COMPENSATE_WHEN_ABORTED);
}

@Override
public int getState() {
return this.state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class Metadata {
public static final String LINK_NAME = "LinkName";
public static final String CONDITION = "Condition";
public static final String IS_FOR_COMPENSATION = "isForCompensation";
public static final String COMPENSATE_WHEN_ABORTED = "compensateIfAborted";
public static final String CORRELATION_KEY = "CorrelationKey";
public static final String CUSTOM_ASYNC = "customAsync";
public static final String CUSTOM_AUTO_START = "customAutoStart";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private GeneratedInfo<KogitoWorkflowProcess> parseProcess() {
handlers.forEach(StateHandler::handleConnections);
if (parserContext.isCompensation()) {
factory.metaData(Metadata.COMPENSATION, true);
factory.metaData(Metadata.COMPENSATE_WHEN_ABORTED, true);
factory.addCompensationContext(workflow.getId());
}
TimeoutsDefinition timeouts = workflow.getTimeouts();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "compensation",
"version": "1.0",
"name": "Workflow Error example",
"description": "An example of how compensation works",
"name": "Workflow compensation",
"description": "Test compensation works",
"start": "printStatus",
"errors": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"id": "automatic_compensation",
"version": "1.0",
"name": "Workflow abortion compensation",
"description": "Testing automatic compensation on abort",
"start": "double",
"functions": [
{
"name": "double",
"type": "expression",
"operation": ".value*=2"
},
{
"name": "half",
"type": "expression",
"operation": ".value/=2"
}],
"events": [
{
"name": "never",
"source": "",
"type": "never"
}
],
"states": [
{
"name": "double",
"type": "operation",
"compensatedBy" : "half",
"actions" : [{
"functionRef" : "double"
}],
"transition": "waitEvent"
},
{
"name": "waitEvent",
"type": "event",
"onEvents": [
{
"eventRefs": [
"never"
],
"actions": [
]
}
],
"end" : true
},
{
"name": "half",
"usedForCompensation" : true,
"type": "operation",
"actions" : [{
"functionRef" : "half"
}],
"end": true
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void testErrorRest2() {
.statusCode(201)
.body("workflowdata.compensated", is(true))
.body("workflowdata.isEven", is(false));

}

@Test
Expand All @@ -66,4 +65,23 @@ public void testErrorRest3() {
.statusCode(201)
.body("workflowdata.compensated", is(false));
}

@Test
public void testCompensationOnAbort() {
String pid = given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.body("{\"value\" : 2}").when()
.post("/automatic_compensation")
.then()
.statusCode(201).extract().path("id");
given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.when()
.delete("/automatic_compensation/" + pid)
.then()
.statusCode(200)
.body("workflowdata.value", is(2));
}
}

0 comments on commit c9acf7c

Please sign in to comment.