Search in sources :

Example 1 with JobsService

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

use of io.automatiko.engine.api.jobs.JobsService 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);
    }
}
Also used : NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) Action(io.automatiko.engine.workflow.base.instance.impl.Action) WorkItemNodeInstance(io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance) ActionExceptionHandler(io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext) JobsService(io.automatiko.engine.api.jobs.JobsService) WorkItemImpl(io.automatiko.engine.workflow.base.instance.impl.workitem.WorkItemImpl) ProcessInstance(io.automatiko.engine.workflow.base.instance.ProcessInstance) ContextInstanceContainer(io.automatiko.engine.workflow.base.instance.ContextInstanceContainer) DurationExpirationTime(io.automatiko.engine.api.jobs.DurationExpirationTime) WorkItemNodeInstance(io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance) NodeInstance(io.automatiko.engine.workflow.process.instance.NodeInstance)

Example 3 with JobsService

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

the class EventNodeInstance method cancelSlaTimer.

private void cancelSlaTimer() {
    if (this.slaTimerId != null && !this.slaTimerId.trim().isEmpty()) {
        JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
        jobService.cancelJob(this.slaTimerId);
        logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
    }
}
Also used : JobsService(io.automatiko.engine.api.jobs.JobsService)

Example 4 with JobsService

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

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

the class StateBasedNodeInstance method cancelTimers.

private void cancelTimers() {
    // deactivate still active timers
    if (timerInstances != null) {
        JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
        for (String id : timerInstances) {
            jobService.cancelJob(id);
        }
    }
    if (retryJobId != null) {
        JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
        jobService.cancelJob(retryJobId);
    }
}
Also used : JobsService(io.automatiko.engine.api.jobs.JobsService)

Aggregations

JobsService (io.automatiko.engine.api.jobs.JobsService)7 ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)3 DurationExpirationTime (io.automatiko.engine.api.jobs.DurationExpirationTime)2 ProcessContext (io.automatiko.engine.workflow.base.core.context.ProcessContext)2 Timer (io.automatiko.engine.workflow.base.core.timer.Timer)2 NodeInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl)2 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)2 Model (io.automatiko.engine.api.Model)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 ActionExceptionHandler (io.automatiko.engine.workflow.base.core.context.exception.ActionExceptionHandler)1 CronExpirationTime (io.automatiko.engine.workflow.base.core.timer.CronExpirationTime)1 ContextInstanceContainer (io.automatiko.engine.workflow.base.instance.ContextInstanceContainer)1 ProcessInstance (io.automatiko.engine.workflow.base.instance.ProcessInstance)1 Action (io.automatiko.engine.workflow.base.instance.impl.Action)1