use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.
the class HandleEscalationAction method execute.
public void execute(ProcessContext context) throws Exception {
ExceptionScopeInstance scopeInstance = (ExceptionScopeInstance) ((NodeInstance) context.getNodeInstance()).resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, faultName);
if (scopeInstance != null) {
Object tVariable = variableName == null ? null : context.getVariable(variableName);
io.automatiko.engine.workflow.process.core.node.Transformation transformation = (io.automatiko.engine.workflow.process.core.node.Transformation) context.getNodeInstance().getNode().getMetaData().get("Transformation");
if (transformation != null) {
tVariable = new EventTransformerImpl(transformation).transformEvent(context.getProcessInstance().getVariables());
}
scopeInstance.handleException(context.getNodeInstance(), faultName, tVariable);
} else {
((ProcessInstance) context.getProcessInstance()).setState(STATE_ABORTED);
}
}
use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.
the class DefaultExceptionScopeInstance method handleException.
public void handleException(io.automatiko.engine.api.runtime.process.NodeInstance nodeInstance, ExceptionHandler handler, String exception, Object params) {
if (handler instanceof ActionExceptionHandler) {
ActionExceptionHandler exceptionHandler = (ActionExceptionHandler) handler;
if (retryAvailable(nodeInstance, exceptionHandler)) {
Integer retryAttempts = ((NodeInstanceImpl) nodeInstance).getRetryAttempts();
if (retryAttempts == null) {
retryAttempts = 1;
} else {
retryAttempts = retryAttempts + 1;
}
long delay = calculateDelay(exceptionHandler.getRetryAfter().longValue(), retryAttempts, exceptionHandler.getRetryIncrement(), exceptionHandler.getRetryIncrementMultiplier());
DurationExpirationTime expirationTime = DurationExpirationTime.after(delay);
JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
String jobId = jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(nodeInstance.getNodeId(), "retry:" + nodeInstance.getId(), expirationTime, ((NodeInstanceImpl) nodeInstance).getProcessInstanceIdWithParent(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getRootProcessId()));
((NodeInstanceImpl) nodeInstance).internalSetRetryJobId(jobId);
((NodeInstanceImpl) nodeInstance).internalSetRetryAttempts(retryAttempts);
((NodeInstanceImpl) nodeInstance).registerRetryEventListener();
if (nodeInstance instanceof WorkItemNodeInstance) {
((WorkItemImpl) ((WorkItemNodeInstance) nodeInstance).getWorkItem()).setState(WorkItem.RETRYING);
}
} else {
Action action = (Action) exceptionHandler.getAction().getMetaData("Action");
try {
ProcessInstance processInstance = getProcessInstance();
ProcessContext processContext = new ProcessContext(processInstance.getProcessRuntime());
ContextInstanceContainer contextInstanceContainer = getContextInstanceContainer();
if (contextInstanceContainer instanceof NodeInstance) {
processContext.setNodeInstance((NodeInstance) contextInstanceContainer);
} else {
processContext.setProcessInstance(processInstance);
}
String faultVariable = exceptionHandler.getFaultVariable();
if (faultVariable != null) {
processContext.setVariable(faultVariable, params);
}
action.execute(processContext);
} catch (Exception e) {
throw new RuntimeException("unable to execute Action", e);
}
}
} else {
throw new IllegalArgumentException("Unknown exception handler " + handler);
}
}
use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.
the class CompositeContextNodeInstance method getContextInstance.
public ContextInstance getContextInstance(final Context context) {
ContextInstanceFactory conf = ContextInstanceFactoryRegistry.INSTANCE.getContextInstanceFactory(context);
if (conf == null) {
throw new IllegalArgumentException("Illegal context type (registry not found): " + context.getClass());
}
ContextInstance contextInstance = (ContextInstance) conf.getContextInstance(context, this, (ProcessInstance) getProcessInstance());
if (contextInstance == null) {
throw new IllegalArgumentException("Illegal context type (instance not found): " + context.getClass());
}
return contextInstance;
}
use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.
the class EndNodeInstance method internalTrigger.
public void internalTrigger(final NodeInstance from, String type) {
super.internalTrigger(from, type);
if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("An EndNode only accepts default incoming connections!");
}
leaveTime = new Date();
boolean hidden = false;
if (getNode().getMetaData().get(HIDDEN) != null) {
hidden = true;
}
if (getProcessInstance().isFunctionFlow(this) && getNodeInstanceContainer() instanceof ProcessInstance) {
// only when running as function flow and node is in the top level node container meaning process instance
// and not subprocesses
getProcessInstance().getMetaData().compute("ATK_FUNC_FLOW_NEXT", (k, v) -> {
if (v == null) {
v = new ArrayList<String>();
}
Process process = getProcessInstance().getProcess();
String version = "";
if (process.getVersion() != null && !process.getVersion().trim().isEmpty()) {
version = ".v" + process.getVersion().replaceAll("\\.", "_");
}
String defaultNextNode = process.getPackageName() + "." + process.getId() + version + "." + getNodeName().toLowerCase();
((List<String>) v).add((String) getNode().getMetaData().getOrDefault("functionType", defaultNextNode));
return v;
});
}
InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
if (!hidden) {
runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
}
((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
if (getEndNode().isTerminate()) {
if (getNodeInstanceContainer() instanceof CompositeNodeInstance) {
if (getEndNode().getScope() == PROCESS_SCOPE) {
getProcessInstance().setState(STATE_COMPLETED);
} else {
while (!getNodeInstanceContainer().getNodeInstances().isEmpty()) {
((io.automatiko.engine.workflow.process.instance.NodeInstance) getNodeInstanceContainer().getNodeInstances().iterator().next()).cancel();
}
((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
}
} else {
((NodeInstanceContainer) getNodeInstanceContainer()).setState(STATE_COMPLETED);
}
} else {
((NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, null);
}
if (!hidden) {
runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
}
String uniqueId = (String) getNode().getMetaData().get(UNIQUE_ID);
if (uniqueId == null) {
uniqueId = ((NodeImpl) getNode()).getUniqueId();
}
((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
}
use of io.automatiko.engine.workflow.base.instance.ProcessInstance in project automatiko-engine by automatiko-io.
the class WorkItemNodeInstance method cancel.
@Override
public void cancel() {
WorkItem item = getWorkItem();
if (item != null && item.getState() != COMPLETED && item.getState() != ABORTED) {
try {
((DefaultWorkItemManager) getProcessInstance().getProcessRuntime().getWorkItemManager()).internalAbortWorkItem(item.getId());
} catch (WorkItemHandlerNotFoundException wihnfe) {
getProcessInstance().setState(STATE_ABORTED);
throw wihnfe;
}
}
if (exceptionHandlingProcessInstanceId != null) {
ProcessInstance processInstance = (ProcessInstance) getProcessInstance().getProcessRuntime().getProcessInstance(exceptionHandlingProcessInstanceId);
if (processInstance != null) {
processInstance.setState(STATE_ABORTED);
}
}
super.cancel();
}
Aggregations