Skip to content

Commit

Permalink
Fix batch migration transaction + add stacktrace info to failing batc…
Browse files Browse the repository at this point in the history
…h part
  • Loading branch information
tijsrademakers committed Apr 12, 2024
1 parent b800785 commit fbe7b65
Show file tree
Hide file tree
Showing 19 changed files with 391 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CaseInstanceBatchMigrationPartResult {
protected String sourceCaseDefinitionId;
protected String targetCaseDefinitionId;
protected String migrationMessage;
protected String migrationStacktrace;

public String getBatchId() {
return batchId;
Expand Down Expand Up @@ -78,4 +79,12 @@ public String getMigrationMessage() {
public void setMigrationMessage(String migrationMessage) {
this.migrationMessage = migrationMessage;
}

public String getMigrationStacktrace() {
return migrationStacktrace;
}

public void setMigrationStacktrace(String migrationStacktrace) {
this.migrationStacktrace = migrationStacktrace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public class GetCaseInstanceMigrationBatchResultCmd implements Command<CaseInstanceBatchMigrationResult> {

protected static final String BATCH_RESULT_MESSAGE_LABEL = "resultMessage";
protected static final String BATCH_RESULT_STACKTRACE_LABEL = "resultStacktrace";
protected String migrationBatchId;

public GetCaseInstanceMigrationBatchResultCmd(String migrationBatchId) {
Expand Down Expand Up @@ -98,6 +99,11 @@ protected CaseInstanceBatchMigrationPartResult convertFromBatchPart(BatchPart ba
String resultMessage = resultNode.get(BATCH_RESULT_MESSAGE_LABEL).asText();
partResult.setMigrationMessage(resultMessage);
}

if (resultNode.has(BATCH_RESULT_STACKTRACE_LABEL)) {
String resultStacktrace = resultNode.get(BATCH_RESULT_STACKTRACE_LABEL).asText();
partResult.setMigrationStacktrace(resultStacktrace);
}

} catch (IOException e) {
throw new FlowableException("Error reading batch part " + batchPart.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class AbstractCaseInstanceMigrationJobHandler implements JobHand

public static final String BATCH_RESULT_STATUS_LABEL = "resultStatus";
public static final String BATCH_RESULT_MESSAGE_LABEL = "resultMessage";
public static final String BATCH_RESULT_STACKTRACE_LABEL = "resultStacktrace";

protected static final String CFG_LABEL_BATCH_ID = "batchId";
protected static final String CFG_LABEL_BATCH_PART_ID = "batchPartId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.flowable.cmmn.engine.impl.job;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.flowable.batch.api.Batch;
import org.flowable.batch.api.BatchPart;
import org.flowable.batch.api.BatchService;
Expand All @@ -21,7 +24,9 @@
import org.flowable.cmmn.engine.impl.migration.CaseInstanceMigrationDocumentImpl;
import org.flowable.cmmn.engine.impl.migration.CaseInstanceMigrationManager;
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableBatchPartMigrationException;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandConfig;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;
Expand Down Expand Up @@ -49,31 +54,53 @@ public void execute(JobEntity job, String configuration, VariableScope variableS
CaseInstanceMigrationDocument migrationDocument = CaseInstanceMigrationDocumentImpl.fromJson(
batch.getBatchDocumentJson(engineConfiguration.getEngineCfgKey()));

String exceptionMessage = null;
try {
migrationManager.migrateCaseInstance(batchPart.getScopeId(), migrationDocument, commandContext);
} catch (FlowableException e) {
exceptionMessage = e.getMessage();
}
} catch (Exception e) {
String exceptionMessage = e.getMessage();

engineConfiguration.getCommandExecutor().execute(new Command<>() {
@Override
public Void execute(CommandContext commandContext) {
CommandConfig commandConfig = engineConfiguration.getCommandExecutor().getDefaultConfig().transactionRequiresNew();
return engineConfiguration.getCommandExecutor().execute(commandConfig, new Command<>() {
@Override
public Void execute(CommandContext commandContext2) {
String resultAsJsonString = prepareResultAsJsonString(exceptionMessage, e);
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_FAIL, resultAsJsonString);

String resultAsJsonString = prepareResultAsJsonString(exceptionMessage);

if (exceptionMessage != null) {
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_FAIL, resultAsJsonString);
} else {
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_SUCCESS, resultAsJsonString);
return null;
}
});
}
});

FlowableBatchPartMigrationException wrappedException = new FlowableBatchPartMigrationException(e.getMessage(), e);
wrappedException.setIgnoreFailedJob(true);
throw wrappedException;
}

String resultAsJsonString = prepareResultAsJsonString();
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_SUCCESS, resultAsJsonString);
}

protected static String prepareResultAsJsonString(String exceptionMessage) {
protected String prepareResultAsJsonString(String exceptionMessage, Exception e) {
ObjectNode objectNode = getObjectMapper().createObjectNode();
if (exceptionMessage == null) {
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_SUCCESS);
} else {
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_FAIL);
objectNode.put(BATCH_RESULT_MESSAGE_LABEL, exceptionMessage);
}
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_FAIL);
objectNode.put(BATCH_RESULT_MESSAGE_LABEL, exceptionMessage);
objectNode.put(BATCH_RESULT_STACKTRACE_LABEL, getExceptionStacktrace(e));
return objectNode.toString();
}

protected String prepareResultAsJsonString() {
ObjectNode objectNode = getObjectMapper().createObjectNode();
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_SUCCESS);
return objectNode.toString();
}

protected String getExceptionStacktrace(Throwable exception) {
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.flowable.cmmn.engine.impl.job;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.flowable.batch.api.Batch;
import org.flowable.batch.api.BatchPart;
import org.flowable.batch.api.BatchService;
Expand All @@ -21,7 +24,9 @@
import org.flowable.cmmn.engine.impl.migration.CaseInstanceMigrationManager;
import org.flowable.cmmn.engine.impl.migration.HistoricCaseInstanceMigrationDocumentImpl;
import org.flowable.cmmn.engine.impl.util.CommandContextUtil;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableBatchPartMigrationException;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandConfig;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;
Expand Down Expand Up @@ -49,31 +54,53 @@ public void execute(JobEntity job, String configuration, VariableScope variableS
HistoricCaseInstanceMigrationDocument migrationDocument = HistoricCaseInstanceMigrationDocumentImpl.fromJson(
batch.getBatchDocumentJson(engineConfiguration.getEngineCfgKey()));

String exceptionMessage = null;
try {
migrationManager.migrateHistoricCaseInstance(batchPart.getScopeId(), migrationDocument, commandContext);
} catch (FlowableException e) {
exceptionMessage = e.getMessage();
}
} catch (Exception e) {
String exceptionMessage = e.getMessage();

engineConfiguration.getCommandExecutor().execute(new Command<>() {
@Override
public Void execute(CommandContext commandContext) {
CommandConfig commandConfig = engineConfiguration.getCommandExecutor().getDefaultConfig().transactionRequiresNew();
return engineConfiguration.getCommandExecutor().execute(commandConfig, new Command<>() {
@Override
public Void execute(CommandContext commandContext2) {
String resultAsJsonString = prepareResultAsJsonString(exceptionMessage, e);
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_FAIL, resultAsJsonString);

String resultAsJsonString = prepareResultAsJsonString(exceptionMessage);

if (exceptionMessage != null) {
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_FAIL, resultAsJsonString);
} else {
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_SUCCESS, resultAsJsonString);
return null;
}
});
}
});

FlowableBatchPartMigrationException wrappedException = new FlowableBatchPartMigrationException(e.getMessage(), e);
wrappedException.setIgnoreFailedJob(true);
throw wrappedException;
}

String resultAsJsonString = prepareResultAsJsonString();
batchService.completeBatchPart(batchPartId, CaseInstanceBatchMigrationResult.RESULT_SUCCESS, resultAsJsonString);
}

protected static String prepareResultAsJsonString(String exceptionMessage) {
protected String prepareResultAsJsonString(String exceptionMessage, Exception e) {
ObjectNode objectNode = getObjectMapper().createObjectNode();
if (exceptionMessage == null) {
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_SUCCESS);
} else {
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_FAIL);
objectNode.put(BATCH_RESULT_MESSAGE_LABEL, exceptionMessage);
}
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_FAIL);
objectNode.put(BATCH_RESULT_MESSAGE_LABEL, exceptionMessage);
objectNode.put(BATCH_RESULT_STACKTRACE_LABEL, getExceptionStacktrace(e));
return objectNode.toString();
}

protected String prepareResultAsJsonString() {
ObjectNode objectNode = getObjectMapper().createObjectNode();
objectNode.put(BATCH_RESULT_STATUS_LABEL, CaseInstanceBatchMigrationResult.RESULT_SUCCESS);
return objectNode.toString();
}

protected String getExceptionStacktrace(Throwable exception) {
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
package org.flowable.cmmn.engine.impl.migration;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.flowable.batch.api.Batch;
import org.flowable.batch.api.BatchPart;
Expand Down Expand Up @@ -480,6 +478,7 @@ public Batch batchMigrateCaseInstancesOfCaseDefinition(String caseDefinitionId,
job.setScopeType(ScopeTypes.CMMN);
job.setJobHandlerConfiguration(CaseInstanceMigrationJobHandler.getHandlerCfgForBatchPartId(batchPart.getId()));
jobService.createAsyncJob(job, false);
job.setRetries(0);
jobService.scheduleAsyncJob(job);
}

Expand All @@ -488,6 +487,7 @@ public Batch batchMigrateCaseInstancesOfCaseDefinition(String caseDefinitionId,
TimerJobEntity timerJob = timerJobService.createTimerJob();
timerJob.setJobType(JobEntity.JOB_TYPE_TIMER);
timerJob.setRevision(1);
timerJob.setRetries(0);
timerJob.setJobHandlerType(CaseInstanceMigrationStatusJobHandler.TYPE);
timerJob.setJobHandlerConfiguration(CaseInstanceMigrationJobHandler.getHandlerCfgForBatchId(batch.getId()));
timerJob.setScopeType(ScopeTypes.CMMN);
Expand Down
Loading

0 comments on commit fbe7b65

Please sign in to comment.