use of io.automatiko.engine.workflow.base.core.timer.Timer 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.workflow.base.core.timer.Timer in project automatiko-engine by automatiko-io.
the class IntermediateCatchEventHandler method handleTimerNode.
protected void handleTimerNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
super.handleNode(node, element, uri, localName, parser);
TimerNode timerNode = (TimerNode) node;
org.w3c.dom.Node xmlNode = element.getFirstChild();
while (xmlNode != null) {
String nodeName = xmlNode.getNodeName();
if ("timerEventDefinition".equals(nodeName)) {
Timer timer = new Timer();
org.w3c.dom.Node subNode = xmlNode.getFirstChild();
while (subNode instanceof Element) {
String subNodeName = subNode.getNodeName();
if ("timeCycle".equals(subNodeName)) {
String delay = subNode.getTextContent();
int index = delay.indexOf("###");
if (index != -1) {
String period = delay.substring(index + 3);
delay = delay.substring(0, index);
timer.setPeriod(period);
}
timer.setTimeType(Timer.TIME_CYCLE);
timer.setDelay(delay);
break;
} else if ("timeDuration".equals(subNodeName)) {
String delay = subNode.getTextContent();
timer.setTimeType(Timer.TIME_DURATION);
timer.setDelay(delay);
break;
} else if ("timeDate".equals(subNodeName)) {
String date = subNode.getTextContent();
timer.setTimeType(Timer.TIME_DATE);
timer.setDate(date);
break;
}
subNode = subNode.getNextSibling();
}
timerNode.setTimer(timer);
}
xmlNode = xmlNode.getNextSibling();
}
}
use of io.automatiko.engine.workflow.base.core.timer.Timer in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowFactory method timerNode.
public TimerNode timerNode(long id, String name, String delay, NodeContainer nodeContainer) {
TimerNode timerNode = new TimerNode();
timerNode.setId(id);
timerNode.setName(name);
timerNode.setMetaData(Metadata.EVENT_TYPE, "timer");
Timer timer = new Timer();
timer.setTimeType(Timer.TIME_DURATION);
timer.setDelay(delay);
timerNode.setTimer(timer);
nodeContainer.addNode(timerNode);
return timerNode;
}
use of io.automatiko.engine.workflow.base.core.timer.Timer in project automatiko-engine by automatiko-io.
the class TimerNodeVisitor method visitNode.
@Override
public void visitNode(WorkflowProcess process, String factoryField, TimerNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, TimerNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Timer"));
Timer timer = node.getTimer();
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_TYPE, new IntegerLiteralExpr(timer.getTimeType())));
if (timer.getTimeType() == Timer.TIME_CYCLE) {
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_DELAY, new StringLiteralExpr(timer.getDelay())));
if (timer.getPeriod() != null && !timer.getPeriod().isEmpty()) {
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_PERIOD, new StringLiteralExpr(timer.getPeriod())));
}
} else if (timer.getTimeType() == Timer.TIME_DURATION) {
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_DELAY, new StringLiteralExpr(timer.getDelay())));
} else if (timer.getTimeType() == Timer.TIME_DATE) {
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_DATE, new StringLiteralExpr(timer.getDate())));
}
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getDoneMethod(getNodeId(node)));
}
use of io.automatiko.engine.workflow.base.core.timer.Timer in project automatiko-engine by automatiko-io.
the class TimerNodeFactory method period.
public TimerNodeFactory period(String period) {
Timer timer = getTimerNode().getTimer();
if (timer == null) {
timer = new Timer();
getTimerNode().setTimer(timer);
}
timer.setPeriod(period);
return this;
}
Aggregations