269 lines
6.0 KiB
Java
269 lines
6.0 KiB
Java
package eu.dnetlib.manager.wf.workflows.procs;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.function.Consumer;
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
import eu.dnetlib.is.model.resource.SimpleResource;
|
|
import eu.dnetlib.manager.history.model.WfProcessExecution;
|
|
import eu.dnetlib.manager.wf.model.WorkflowInstance;
|
|
import eu.dnetlib.manager.wf.workflows.graph.Graph;
|
|
import eu.dnetlib.manager.wf.workflows.util.WorkflowsConstants;
|
|
|
|
/**
|
|
* Created by michele on 19/11/15.
|
|
*/
|
|
public class WorkflowProcess implements Comparable<WorkflowProcess> {
|
|
|
|
public enum Status {
|
|
CREATED,
|
|
EXECUTING,
|
|
SUCCESS,
|
|
FAILURE,
|
|
KILLED;
|
|
}
|
|
|
|
public enum StartMode {
|
|
AUTO,
|
|
MANUAL,
|
|
DISABLED
|
|
}
|
|
|
|
private final String id;
|
|
private final SimpleResource wfMetadata;
|
|
private final WorkflowInstance wfInstance;
|
|
private final Graph graph;
|
|
private final Consumer<WorkflowProcess> onSuccess;
|
|
private final Consumer<WorkflowProcess> onFail;
|
|
private final Env env;
|
|
private final List<Token> tokens = new CopyOnWriteArrayList<>();
|
|
private LocalDateTime lastActivityDate;
|
|
private Status status;
|
|
private LocalDateTime startDate = LocalDateTime.MIN;
|
|
private LocalDateTime endDate = LocalDateTime.MIN;
|
|
private final Map<String, List<Token>> pausedJoinNodeTokens = new HashMap<>();
|
|
private final Map<String, String> globalParams;
|
|
private String error;
|
|
private String errorStacktrace;
|
|
private final Map<String, String> outputParams = new HashMap<>();
|
|
private final String parentProfileId;
|
|
|
|
public WorkflowProcess(
|
|
final String id,
|
|
final SimpleResource wfMetadata,
|
|
final WorkflowInstance wfInstance,
|
|
final Graph graph,
|
|
final Map<String, String> globalParams,
|
|
final String parentProfileId,
|
|
final Consumer<WorkflowProcess> onSuccess,
|
|
final Consumer<WorkflowProcess> onFail) {
|
|
this.id = id;
|
|
this.wfMetadata = wfMetadata;
|
|
this.wfInstance = wfInstance;
|
|
this.graph = graph;
|
|
this.onSuccess = onSuccess;
|
|
this.onFail = onFail;
|
|
this.status = Status.CREATED;
|
|
this.env = new Env();
|
|
this.globalParams = globalParams;
|
|
this.lastActivityDate = LocalDateTime.now();
|
|
this.parentProfileId = parentProfileId;
|
|
}
|
|
|
|
public String getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getName() {
|
|
return wfMetadata.getName();
|
|
}
|
|
|
|
public String getFamily() {
|
|
return wfMetadata.getSubtype();
|
|
}
|
|
|
|
public String getWfId() {
|
|
return wfMetadata.getId();
|
|
}
|
|
|
|
public String getWfInstanceId() {
|
|
return wfInstance.getId();
|
|
}
|
|
|
|
public int getPriority() {
|
|
return wfInstance.getPriority();
|
|
}
|
|
|
|
public String getDsId() {
|
|
return wfInstance.getId();
|
|
}
|
|
|
|
public String getDsName() {
|
|
return wfInstance.getDsName();
|
|
}
|
|
|
|
public String getDsInterface() {
|
|
return wfInstance.getApiId();
|
|
}
|
|
|
|
public Map<String, List<Token>> getPausedJoinNodeTokens() {
|
|
return pausedJoinNodeTokens;
|
|
}
|
|
|
|
public Env getEnv() {
|
|
return env;
|
|
}
|
|
|
|
public Status getStatus() {
|
|
return status;
|
|
}
|
|
|
|
public void setStatus(final Status status) {
|
|
this.status = status;
|
|
}
|
|
|
|
public Graph getGraph() {
|
|
return graph;
|
|
}
|
|
|
|
public List<Token> getTokens() {
|
|
return tokens;
|
|
}
|
|
|
|
public void kill() {
|
|
setStatus(Status.KILLED);
|
|
}
|
|
|
|
public boolean isTerminated() {
|
|
switch (status) {
|
|
case SUCCESS:
|
|
case FAILURE:
|
|
case KILLED:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public LocalDateTime getLastActivityDate() {
|
|
return lastActivityDate;
|
|
}
|
|
|
|
public void setLastActivityDate(final LocalDateTime lastActivityDate) {
|
|
this.lastActivityDate = lastActivityDate;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("[process id='%s' name='%s']", id, wfMetadata.getName());
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(final WorkflowProcess wp) {
|
|
return NumberUtils.compare(getPriority(), wp.getPriority());
|
|
}
|
|
|
|
public Map<String, String> getGlobalParams() {
|
|
return globalParams;
|
|
}
|
|
|
|
public void setStartDate(final LocalDateTime startDate) {
|
|
this.startDate = startDate;
|
|
}
|
|
|
|
public void setEndDate(final LocalDateTime endDate) {
|
|
this.endDate = endDate;
|
|
}
|
|
|
|
public LocalDateTime getStartDate() {
|
|
return startDate;
|
|
}
|
|
|
|
public LocalDateTime getEndDate() {
|
|
return endDate;
|
|
}
|
|
|
|
public String getError() {
|
|
return error;
|
|
}
|
|
|
|
public void setError(final String error) {
|
|
this.error = error;
|
|
}
|
|
|
|
public String getErrorStacktrace() {
|
|
return errorStacktrace;
|
|
}
|
|
|
|
public void setErrorStacktrace(final String errorStacktrace) {
|
|
this.errorStacktrace = errorStacktrace;
|
|
}
|
|
|
|
public Map<String, String> getOutputParams() {
|
|
return outputParams;
|
|
}
|
|
|
|
public String getParentProfileId() {
|
|
return parentProfileId;
|
|
}
|
|
|
|
public void complete(final Token token) {
|
|
final LocalDateTime now = token.getEndDate();
|
|
setLastActivityDate(now);
|
|
setEndDate(now);
|
|
setStatus(token.isFailed() ? WorkflowProcess.Status.FAILURE : WorkflowProcess.Status.SUCCESS);
|
|
|
|
if (token.isFailed()) {
|
|
setStatus(Status.FAILURE);
|
|
setError(token.getError());
|
|
setErrorStacktrace(token.getErrorStackTrace());
|
|
setLastActivityDate(LocalDateTime.now());
|
|
}
|
|
|
|
if (token.isFailed() && onFail != null) {
|
|
onFail.accept(this);
|
|
}
|
|
if (!token.isFailed() && onSuccess != null) {
|
|
onSuccess.accept(this);;
|
|
}
|
|
|
|
}
|
|
|
|
public WfProcessExecution asLog() {
|
|
final Map<String, String> details = new LinkedHashMap<>();
|
|
details.putAll(getOutputParams());
|
|
details.put(WorkflowsConstants.LOG_WF_PRIORITY, "" + getPriority());
|
|
details.put(WorkflowsConstants.LOG_WF_ID, getWfId());
|
|
details.put(WorkflowsConstants.LOG_WF_ID, getWfInstanceId());
|
|
|
|
if (getError() != null) {
|
|
details.put(WorkflowsConstants.LOG_SYSTEM_ERROR, getError());
|
|
details.put(WorkflowsConstants.LOG_SYSTEM_ERROR_STACKTRACE, getErrorStacktrace());
|
|
}
|
|
|
|
final WfProcessExecution pe = new WfProcessExecution();
|
|
pe.setProcessId(getId());
|
|
pe.setName(getName());
|
|
pe.setFamily(getFamily());
|
|
|
|
pe.setDsId(getDsId());
|
|
pe.setDsName(getDsName());
|
|
pe.setDsApi(getDsInterface());
|
|
|
|
pe.setStartDate(getStartDate());
|
|
pe.setEndDate(getEndDate());
|
|
pe.setStatus(getStatus().toString());
|
|
|
|
pe.setDetails(details);
|
|
|
|
return pe;
|
|
}
|
|
|
|
}
|