Search in sources :

Example 1 with ExpirationTime

use of io.automatiko.engine.api.jobs.ExpirationTime 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 ExpirationTime

use of io.automatiko.engine.api.jobs.ExpirationTime in project automatiko-engine by automatiko-io.

the class TimerNodeInstance method internalTrigger.

@Override
public void internalTrigger(NodeInstance from, String type) {
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("A TimerNode only accepts default incoming connections!");
    }
    triggerTime = new Date();
    ExpirationTime expirationTime = createTimerInstance(getTimerNode().getTimer());
    if (getTimerInstances() == null) {
        addTimerListener();
    }
    JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
    timerId = jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(getTimerNode().getTimer().getId(), expirationTime, getProcessInstanceIdWithParent(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getRootProcessId()));
    logger.debug("Scheduled timer with id {} for node {} with fire date {}", timerId, getNodeName(), expirationTime.get());
}
Also used : JobsService(io.automatiko.engine.api.jobs.JobsService) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) Date(java.util.Date)

Example 3 with ExpirationTime

use of io.automatiko.engine.api.jobs.ExpirationTime in project automatiko-engine by automatiko-io.

the class StateBasedNodeInstance method internalTrigger.

@Override
public void internalTrigger(NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    addCompletionListeners();
    // activate timers
    Map<Timer, ProcessAction> timers = getEventBasedNode().getTimers();
    if (timers != null) {
        addTimerListener();
        timerInstances = new ArrayList<>(timers.size());
        JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
        for (Timer timer : timers.keySet()) {
            ExpirationTime expirationTime = createTimerInstance(timer);
            String jobId = jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.getId(), expirationTime, getProcessInstanceIdWithParent(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getRootProcessId()));
            timerInstances.add(jobId);
        }
    }
    ((WorkflowProcessInstanceImpl) getProcessInstance()).addActivatingNodeId((String) getNode().getMetaData().get("UniqueId"));
    if (getExtendedNode().hasCondition()) {
        ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
        context.setProcessInstance(getProcessInstance());
        if (getExtendedNode().getMetaData("ConditionEventType") != null && getExtendedNode().isMet(context)) {
            getProcessInstance().signalEvent((String) getExtendedNode().getMetaData("ConditionEventType"), null);
        } else {
            getProcessInstance().addEventListener("variableChanged", this, false);
        }
    }
}
Also used : ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) JobsService(io.automatiko.engine.api.jobs.JobsService) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExactExpirationTime(io.automatiko.engine.api.jobs.ExactExpirationTime) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) DurationExpirationTime(io.automatiko.engine.api.jobs.DurationExpirationTime) CronExpirationTime(io.automatiko.engine.workflow.base.core.timer.CronExpirationTime) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext)

Aggregations

ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)3 JobsService (io.automatiko.engine.api.jobs.JobsService)3 Timer (io.automatiko.engine.workflow.base.core.timer.Timer)2 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)2 Model (io.automatiko.engine.api.Model)1 DurationExpirationTime (io.automatiko.engine.api.jobs.DurationExpirationTime)1 ExactExpirationTime (io.automatiko.engine.api.jobs.ExactExpirationTime)1 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)1 ExportedProcessInstance (io.automatiko.engine.api.workflow.ExportedProcessInstance)1 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)1 AbstractProcess (io.automatiko.engine.workflow.AbstractProcess)1 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)1 StringExportedProcessInstance (io.automatiko.engine.workflow.StringExportedProcessInstance)1 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)1 CronExpirationTime (io.automatiko.engine.workflow.base.core.timer.CronExpirationTime)1 ProcessAction (io.automatiko.engine.workflow.process.core.ProcessAction)1 NodeInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl)1 StateBasedNodeInstance (io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance)1 TimerNodeInstance (io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance)1 Date (java.util.Date)1