use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method importProcessInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance importProcessInstance(ExportedProcessInstance<?> instance, Process process) {
List<Map<String, String>> timers = instance.convertTimers();
WorkflowProcessInstance wpi = importWorkflowProcessInstance((String) instance.getHeader(), (String) instance.getInstance(), timers, process);
Model model = ((AbstractProcess) process).createModel();
model.fromMap(wpi.getVariables());
ProcessInstance processInstance = ((AbstractProcess) process).createInstance(wpi, model, 1);
if (timers != null && !timers.isEmpty()) {
String parentProcessInstanceId = wpi.getParentProcessInstanceId();
if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
parentProcessInstanceId += ":";
} else {
parentProcessInstanceId = "";
}
JobsService jobService = ((WorkflowProcessInstanceImpl) wpi).getProcessRuntime().getJobsService();
Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) wpi).getNodeInstances(true);
// keeps iterator for statebased node instance timers
Map<String, Iterator<Timer>> nodeInstanceTimers = new LinkedHashMap<>();
for (Map<String, String> timer : timers) {
String nodeInstanceId = timer.get("nodeInstanceId");
for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
if (ni.getId().equals(nodeInstanceId)) {
ExpirationTime expirationTime = null;
long timerNodeId = 0;
if (((NodeInstanceImpl) ni).getRetryJobId() != null && ((NodeInstanceImpl) ni).getRetryJobId().equals(timer.get("timerId"))) {
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), ni.getNodeId(), "retry:" + ni.getId(), expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), ni.getProcessInstance().getRootProcessInstanceId(), ni.getProcessInstance().getProcessId(), ni.getProcessInstance().getProcess().getVersion(), ni.getProcessInstance().getRootProcessId()));
break;
} else if (ni instanceof TimerNodeInstance) {
TimerNodeInstance nodeInstance = (TimerNodeInstance) ni;
timerNodeId = nodeInstance.getTimerNode().getTimer().getId();
expirationTime = nodeInstance.createTimerInstance(nodeInstance.getTimerNode().getTimer());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
} else if (ni instanceof StateBasedNodeInstance) {
StateBasedNodeInstance nodeInstance = (StateBasedNodeInstance) ni;
if (nodeInstance.getTimerInstances().contains(timer.get("timerId"))) {
Iterator<Timer> it = nodeInstanceTimers.computeIfAbsent(nodeInstanceId, k -> nodeInstance.getEventBasedNode().getTimers().keySet().iterator());
expirationTime = nodeInstance.createTimerInstance(it.next());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
}
}
// lastly schedule timer on calculated expiration time
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), timerNodeId, expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), wpi.getRootProcessInstanceId(), wpi.getProcessId(), wpi.getProcess().getVersion(), wpi.getRootProcessId()));
break;
}
}
}
}
return processInstance;
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class CancelNodeInstanceAction method execute.
public void execute(ProcessContext context) throws Exception {
WorkflowProcessInstance pi = context.getNodeInstance().getProcessInstance();
NodeInstance nodeInstance = findNodeByUniqueId(pi.getNodeInstances(), attachedToNodeId);
if (nodeInstance != null) {
((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).cancel();
}
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceEventBatch method createUserTask.
protected UserTaskInstanceEventBody createUserTask(ProcessWorkItemTransitionEvent workItemTransitionEvent) {
WorkflowProcessInstance pi = (WorkflowProcessInstance) workItemTransitionEvent.getProcessInstance();
HumanTaskWorkItem workItem = (HumanTaskWorkItem) workItemTransitionEvent.getWorkItem();
String parentProcessInstanceId = pi.getParentProcessInstanceId();
if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
parentProcessInstanceId += ":";
} else {
parentProcessInstanceId = "";
}
String version = version(pi.getProcess().getVersion());
String encoded = Base64.getEncoder().encodeToString((pi.getProcessId() + version + "|" + parentProcessInstanceId + pi.getId() + "|" + workItem.getId() + "|").getBytes(StandardCharsets.UTF_8));
String link = "/management/tasks/link/" + encoded;
return UserTaskInstanceEventBody.create().id(workItem.getId()).state(workItem.getPhaseStatus()).taskName(workItem.getTaskName()).taskDescription(workItem.getTaskDescription()).taskPriority(workItem.getTaskPriority()).referenceName(workItem.getReferenceName()).referenceId(workItem.getReferenceId()).formLink(link).actualOwner(workItem.getActualOwner()).startDate(workItem.getStartDate()).completeDate(workItem.getCompleteDate()).adminGroups(workItem.getAdminGroups()).adminUsers(workItem.getAdminUsers()).excludedUsers(workItem.getExcludedUsers()).potentialGroups(workItem.getPotentialGroups()).potentialUsers(workItem.getPotentialUsers()).processInstanceId(pi.getId()).rootProcessInstanceId(pi.getRootProcessInstanceId()).processId(pi.getProcessId()).rootProcessId(pi.getRootProcessId()).inputs(workItem.getParameters()).outputs(workItem.getResults()).instance(workItem).build();
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class TracingAdds method addTags.
public void addTags(io.automatiko.engine.api.runtime.process.ProcessInstance instance) {
if (tracer.isResolvable()) {
Tracer tracerInstance = tracer.get();
tracerInstance.activeSpan().setTag("workflow.instance.id", instance.getId());
tracerInstance.activeSpan().setTag("workflow.root.instance.id", instance.getRootProcessInstanceId() == null ? instance.getId() : instance.getRootProcessInstanceId());
if (instance.getCorrelationKey() != null) {
tracerInstance.activeSpan().setTag("workflow.business.key", instance.getCorrelationKey());
}
Collection<Tag> tags = ((WorkflowProcessInstance) instance).getTags();
if (!tags.isEmpty()) {
tracerInstance.activeSpan().setTag("workflow.instance.tags", tags.stream().map(t -> t.getValue()).collect(Collectors.joining(",")));
}
}
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method disconnect.
protected void disconnect(ProcessInstance<ProcessInstanceEntity> instance) {
((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
try {
ProcessInstanceEntity entity = (ProcessInstanceEntity) JpaOperations.INSTANCE.findById(type, resolveId(instance.id(), instance));
byte[] reloaded = codec.decode(entity.content);
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(reloaded, process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
return wpi;
} catch (RuntimeException e) {
LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
return null;
}
});
}
Aggregations