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;
}
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);
}
}
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);
}
}
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());
}
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);
}
}
Aggregations