Search in sources :

Example 1 with Timer

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;
}
Also used : WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) LinkedHashMap(java.util.LinkedHashMap) JobsService(io.automatiko.engine.api.jobs.JobsService) Iterator(java.util.Iterator) NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) Model(io.automatiko.engine.api.Model) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) StringExportedProcessInstance(io.automatiko.engine.workflow.StringExportedProcessInstance) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 2 with Timer

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();
    }
}
Also used : Timer(io.automatiko.engine.workflow.base.core.timer.Timer) Element(org.w3c.dom.Element) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode)

Example 3 with Timer

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;
}
Also used : Timer(io.automatiko.engine.workflow.base.core.timer.Timer) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode)

Example 4 with Timer

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)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr)

Example 5 with Timer

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;
}
Also used : Timer(io.automatiko.engine.workflow.base.core.timer.Timer)

Aggregations

Timer (io.automatiko.engine.workflow.base.core.timer.Timer)28 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)9 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 EventNode (io.automatiko.engine.workflow.process.core.node.EventNode)6 EventSubProcessNode (io.automatiko.engine.workflow.process.core.node.EventSubProcessNode)6 EventTrigger (io.automatiko.engine.workflow.process.core.node.EventTrigger)6 StateBasedNode (io.automatiko.engine.workflow.process.core.node.StateBasedNode)6 TimerNode (io.automatiko.engine.workflow.process.core.node.TimerNode)6 List (java.util.List)6 Node (io.automatiko.engine.api.definition.process.Node)5 ConsequenceAction (io.automatiko.engine.workflow.process.core.impl.ConsequenceAction)5 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)5 BoundaryEventNode (io.automatiko.engine.workflow.process.core.node.BoundaryEventNode)5 CompositeNode (io.automatiko.engine.workflow.process.core.node.CompositeNode)5 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)5 StateNode (io.automatiko.engine.workflow.process.core.node.StateNode)5 ArrayList (java.util.ArrayList)5 WorkflowProcess (io.automatiko.engine.api.definition.process.WorkflowProcess)4