Skip to content

Commit

Permalink
fix transactions templates
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Oct 30, 2024
1 parent 5c89595 commit 605cdad
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public T mapException(Exception exceptionThrown) {
Throwable exception = exceptionThrown;
var handler = mapper.getOrDefault(exception.getClass(), DEFAULT_HANDLER);

ExceptionBodyMessage message = handler.getContent(exceptionThrown);

Throwable rootCause = exception.getCause();
while (rootCause != null) {
if (mapper.containsKey(rootCause.getClass())) {
Expand All @@ -135,8 +137,8 @@ public T mapException(Exception exceptionThrown) {
}
// we invoked the error handlers
errorHandlers.forEach(e -> e.handle(exceptionThrown));
T response = handler.buildResponse(exception);
LOG.debug("mapping exception {} with response {}", exceptionThrown, response);
T response = handler.buildResponse(message);
LOG.debug("mapping exception {} with response {}", exceptionThrown, message.getBody());
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public Class<EXCEPTION> getType() {
return type;
}

public ExceptionBodyMessage getContent(EXCEPTION exception) {
return messageConverter.apply(exception);
public ExceptionBodyMessage getContent(Throwable exception) {
return messageConverter.apply(getType().cast(exception));
}

public RESPONSE buildResponse(Throwable exception) {
return responseConverter.apply(getContent(getType().cast(exception)));
public RESPONSE buildResponse(ExceptionBodyMessage exceptionBodyMessage) {
return responseConverter.apply(exceptionBodyMessage);
}

public static <TYPE extends Exception, RES> RestExceptionHandler<TYPE, RES> newExceptionHandler(Class<TYPE> type, Function<TYPE, ExceptionBodyMessage> contentGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ public final void trigger(KogitoNodeInstance from, String type) {
try {
internalTrigger(from, type);
} catch (Exception e) {
logger.error("Node instance causing process instance error in id {}", this.getStringId(), e);
if (!WORKFLOW_PARAM_TRANSACTIONS.get(getProcessInstance().getProcess())) {
logger.error("Node instance causing process instance error in id {} in a non transactional environment", this.getStringId());
captureError(e);
return;
} else {
logger.error("Node instance causing process instance error in id {} in a transactional environment (Wrapping)", this.getStringId());
throw new ProcessInstanceExecutionException(this.getProcessInstance().getId(), this.getNodeDefinitionId(), e.getMessage(), e);
}
// stop after capturing error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@
import org.kie.kogito.codegen.api.SourceFileCodegenBindEvent;
import org.kie.kogito.codegen.api.context.ContextAttributesConstants;
import org.kie.kogito.codegen.api.context.KogitoBuildContext;
import org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext;
import org.kie.kogito.codegen.api.io.CollectedResource;
import org.kie.kogito.codegen.api.template.TemplatedGenerator;
import org.kie.kogito.codegen.core.AbstractGenerator;
import org.kie.kogito.codegen.core.DashboardGeneratedFileUtils;
import org.kie.kogito.codegen.process.config.ProcessConfigGenerator;
import org.kie.kogito.codegen.process.events.ProcessCloudEventMeta;
import org.kie.kogito.codegen.process.events.ProcessCloudEventMetaFactoryGenerator;
import org.kie.kogito.codegen.process.util.CodegenUtil;
import org.kie.kogito.internal.SupportedExtensions;
import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess;
import org.kie.kogito.process.validation.ValidationException;
Expand All @@ -75,6 +78,7 @@
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;

import static java.lang.String.format;
Expand Down Expand Up @@ -299,9 +303,12 @@ protected Collection<GeneratedFile> internalGenerate() {

// first we generate all the data classes from variable declarations
for (WorkflowProcess workFlowProcess : processes.values()) {
if (isTransactionEnabled(this, context())) {
// transaction is disabled by default for SW types
boolean defaultTransactionEnabled = !KogitoWorkflowProcess.SW_TYPE.equals(workFlowProcess.getType());
if (isTransactionEnabled(this, context(), defaultTransactionEnabled)) {
((WorkflowProcessImpl) workFlowProcess).setMetaData(WorkflowProcessParameters.WORKFLOW_PARAM_TRANSACTIONS.getName(), "true");
}

if (!skipModelGeneration(workFlowProcess)) {
ModelClassGenerator mcg = new ModelClassGenerator(context(), workFlowProcess);
processIdToModelGenerator.put(workFlowProcess.getId(), mcg);
Expand All @@ -313,9 +320,10 @@ protected Collection<GeneratedFile> internalGenerate() {
processIdToOutputModelGenerator.put(workFlowProcess.getId(), omcg);
}
}

boolean isServerless = false;
// then we generate work items task inputs and outputs if any
for (WorkflowProcess workFlowProcess : processes.values()) {
isServerless |= KogitoWorkflowProcess.SW_TYPE.equals(workFlowProcess.getType());
if (KogitoWorkflowProcess.SW_TYPE.equals(workFlowProcess.getType())) {
continue;
}
Expand Down Expand Up @@ -344,6 +352,7 @@ protected Collection<GeneratedFile> internalGenerate() {
}

// generate Process, ProcessInstance classes and the REST resource

for (ProcessExecutableModelGenerator execModelGen : processExecutableModelGenerators) {
String classPrefix = sanitizeClassName(execModelGen.extractedProcessId());
KogitoWorkflowProcess workFlowProcess = execModelGen.process();
Expand Down Expand Up @@ -456,6 +465,17 @@ protected Collection<GeneratedFile> internalGenerate() {
.forEach((key, value) -> storeFile(PRODUCER_TYPE, key, value));
}

if (CodegenUtil.isTransactionEnabled(this, context()) && !isServerless) {
String template = "ExceptionHandlerTransaction";
TemplatedGenerator generator = TemplatedGenerator.builder()
.withTemplateBasePath("/class-templates/transaction/")
.withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME)
.withTargetTypeName(template)
.build(context(), template);
CompilationUnit handler = generator.compilationUnitOrThrow();
storeFile(MODEL_TYPE, generator.generatedFilePath(), handler.toString());
}

if (context().hasRESTForGenerator(this)) {
for (ProcessResourceGenerator resourceGenerator : rgs) {
storeFile(REST_TYPE, resourceGenerator.generatedFilePath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public static String globalProperty(String propertyName) {
* @see CodegenUtil#getProperty
*/
public static boolean isTransactionEnabled(Generator generator, KogitoBuildContext context) {
boolean propertyValue = getProperty(generator, context, TRANSACTION_ENABLED, Boolean::parseBoolean, true);
return isTransactionEnabled(generator, context, true);
}

public static boolean isTransactionEnabled(Generator generator, KogitoBuildContext context, boolean defaultValue) {
boolean propertyValue = getProperty(generator, context, TRANSACTION_ENABLED, Boolean::parseBoolean, defaultValue);
LOG.debug("Compute property {} for generator {} property with value {}", TRANSACTION_ENABLED, generator.name(), propertyValue);
// java implementation does not have transactions
return !JavaKogitoBuildContext.CONTEXT_NAME.equals(context.name()) && propertyValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
*/
package org.kie.kogito.quarkus.workflow.handler;

import java.util.Optional;

import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl;
import org.kie.kogito.Model;
import org.kie.kogito.handler.ExceptionHandler;
import org.kie.kogito.process.MutableProcessInstances;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstanceExecutionException;
import org.kie.kogito.process.Processes;
import org.kie.kogito.process.impl.AbstractProcessInstance;
Expand Down Expand Up @@ -56,11 +53,12 @@ public void handle(Exception th) {
if (!processes.isResolvable()) {
return;
}
if (th instanceof ProcessInstanceExecutionException processInstanceExecutionException) {
if (th instanceof ProcessInstanceExecutionException) {
ProcessInstanceExecutionException processInstanceExecutionException = (ProcessInstanceExecutionException) th;
LOG.info("handling exception {} by the handler {}", th, this.getClass().getName());
UnitOfWorkExecutor.executeInUnitOfWork(unitOfWorkManager, () -> {
String processInstanceId = processInstanceExecutionException.getProcessInstanceId();
Optional<Process<? extends Model>> processDefinition = processes.get().processByProcessInstanceId(processInstanceId);
var processDefinition = processes.get().processByProcessInstanceId(processInstanceId);
if (processDefinition.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
*/
package org.kie.kogito.process.handler;

import java.util.Optional;

import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl;
import org.kie.kogito.Model;
import org.kie.kogito.handler.ExceptionHandler;
import org.kie.kogito.process.MutableProcessInstances;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstanceExecutionException;
import org.kie.kogito.process.Processes;
import org.kie.kogito.process.impl.AbstractProcessInstance;
Expand Down Expand Up @@ -54,11 +51,12 @@ public void handle(Exception th) {
if (processes == null) {
return;
}
if (th instanceof ProcessInstanceExecutionException processInstanceExecutionException) {
if (th instanceof ProcessInstanceExecutionException) {
ProcessInstanceExecutionException processInstanceExecutionException = (ProcessInstanceExecutionException) th;
LOG.info("handling exception {} by the handler {}", th, this.getClass().getName());
UnitOfWorkExecutor.executeInUnitOfWork(unitOfWorkManager, () -> {
String processInstanceId = processInstanceExecutionException.getProcessInstanceId();
Optional<Process<? extends Model>> processDefinition = processes.processByProcessInstanceId(processInstanceId);
var processDefinition = processes.processByProcessInstanceId(processInstanceId);
if (processDefinition.isEmpty()) {
return null;
}
Expand Down

0 comments on commit 605cdad

Please sign in to comment.