277 lines
6.0 KiB
Java
277 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 org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
import eu.dnetlib.is.model.resource.SimpleResource;
|
|
import eu.dnetlib.manager.history.model.WfHistoryEntry;
|
|
import eu.dnetlib.manager.wf.model.WorkflowConfiguration;
|
|
import eu.dnetlib.manager.wf.workflows.graph.Graph;
|
|
import eu.dnetlib.manager.wf.workflows.util.ProcessCallback;
|
|
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,
|
|
running,
|
|
success,
|
|
failure,
|
|
killed;
|
|
}
|
|
|
|
public enum StartMode {
|
|
AUTO,
|
|
MANUAL,
|
|
DISABLED
|
|
}
|
|
|
|
private final String id;
|
|
private final SimpleResource wfMetadata;
|
|
private final WorkflowConfiguration wfConf;
|
|
private final Graph graph;
|
|
private final ProcessCallback callback;
|
|
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<>();
|
|
|
|
public WorkflowProcess(
|
|
final String id,
|
|
final SimpleResource wfMetadata,
|
|
final WorkflowConfiguration wfConf,
|
|
final Graph graph,
|
|
final Map<String, String> globalParams,
|
|
final ProcessCallback callback) {
|
|
this.id = id;
|
|
this.wfMetadata = wfMetadata;
|
|
this.wfConf = wfConf;
|
|
this.graph = graph;
|
|
this.callback = callback;
|
|
this.status = Status.created;
|
|
this.env = new Env();
|
|
this.globalParams = globalParams;
|
|
this.lastActivityDate = LocalDateTime.now();
|
|
}
|
|
|
|
public String getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getName() {
|
|
return wfConf.getName();
|
|
}
|
|
|
|
public String getFamily() {
|
|
return wfMetadata.getSubtype();
|
|
}
|
|
|
|
public String getWfId() {
|
|
return wfMetadata.getId();
|
|
}
|
|
|
|
public String getWfConfId() {
|
|
return wfConf.getId();
|
|
}
|
|
|
|
public String getParentId() {
|
|
return wfConf.getParentId();
|
|
}
|
|
|
|
public int getPriority() {
|
|
return wfConf.getPriority();
|
|
}
|
|
|
|
public String getDsId() {
|
|
return wfConf.getId();
|
|
}
|
|
|
|
public String getDsName() {
|
|
return wfConf.getDsName();
|
|
}
|
|
|
|
public String getDsInterface() {
|
|
return wfConf.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 Token newToken(final Map<String, Object> attrs) {
|
|
final Token token = new Token();
|
|
token.getEnv().addAttributes(attrs);
|
|
tokens.add(token);
|
|
return token;
|
|
}
|
|
|
|
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 (callback != null) {
|
|
if (token.isFailed()) {
|
|
callback.onFail(this);
|
|
} else {
|
|
callback.onSuccess(this);;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public WfHistoryEntry 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_CONF_ID, getWfConfId());
|
|
|
|
if (getError() != null) {
|
|
details.put(WorkflowsConstants.LOG_SYSTEM_ERROR, getError());
|
|
details.put(WorkflowsConstants.LOG_SYSTEM_ERROR_STACKTRACE, getErrorStacktrace());
|
|
}
|
|
|
|
final WfHistoryEntry pe = new WfHistoryEntry();
|
|
pe.setProcessId(getId());
|
|
pe.setWfConfigurationId(getWfConfId());
|
|
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;
|
|
}
|
|
|
|
public ExecutionStatus getExecutionStatus() {
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
}
|