129 lines
2.5 KiB
Java
129 lines
2.5 KiB
Java
package eu.dnetlib.manager.wf.workflows.procs;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import com.google.common.base.Throwables;
|
|
|
|
/**
|
|
* Created by michele on 19/11/15.
|
|
*/
|
|
public class Token {
|
|
|
|
private final String id;
|
|
private final Env env = new Env();
|
|
private final LocalDateTime startDate;
|
|
private LocalDateTime endDate = LocalDateTime.MIN;
|
|
|
|
private final Consumer<Token> onSuccess;
|
|
private final Consumer<Token> onFail;
|
|
|
|
private boolean failed = false;
|
|
|
|
private boolean active = true;
|
|
private String error = "";
|
|
private String errorStackTrace = "";
|
|
|
|
public Token(final Consumer<Token> onSuccess, final Consumer<Token> onFail) {
|
|
this.id = "token-" + UUID.randomUUID();
|
|
this.startDate = LocalDateTime.now();
|
|
this.onSuccess = onSuccess;
|
|
this.onFail = onFail;
|
|
}
|
|
|
|
public String getId() {
|
|
return this.id;
|
|
}
|
|
|
|
public Env getEnv() {
|
|
return this.env;
|
|
}
|
|
|
|
public LocalDateTime getStartDate() {
|
|
return this.startDate;
|
|
}
|
|
|
|
public LocalDateTime getEndDate() {
|
|
return this.endDate;
|
|
}
|
|
|
|
public void setEndDate(final LocalDateTime endDate) {
|
|
this.endDate = endDate;
|
|
}
|
|
|
|
public boolean isActive() {
|
|
return this.active;
|
|
}
|
|
|
|
public void setActive(final boolean active) {
|
|
this.active = active;
|
|
}
|
|
|
|
public boolean isFailed() {
|
|
return this.failed;
|
|
}
|
|
|
|
public void setFailed(final boolean failed) {
|
|
this.failed = failed;
|
|
}
|
|
|
|
public void release() {
|
|
setEndDate(LocalDateTime.now());
|
|
setActive(false);
|
|
if (this.onSuccess != null) {
|
|
this.onSuccess.accept(this);
|
|
}
|
|
}
|
|
|
|
public void releaseAsFailed(final Throwable e) {
|
|
setEndDate(LocalDateTime.now());
|
|
setActive(false);
|
|
setFailed(true);
|
|
setError(e.getMessage());
|
|
setErrorStackTrace(Throwables.getStackTraceAsString(e));
|
|
if (this.onFail != null) {
|
|
this.onFail.accept(this);
|
|
}
|
|
}
|
|
|
|
public void releaseAsFailed(final String error) {
|
|
setEndDate(LocalDateTime.now());
|
|
setActive(false);
|
|
setFailed(true);
|
|
setError(error);
|
|
if (this.onFail != null) {
|
|
this.onFail.accept(this);
|
|
}
|
|
}
|
|
|
|
public void checkStatus() {
|
|
if (isActive()) {
|
|
if (StringUtils.isNotBlank(error)) {
|
|
releaseAsFailed(error);
|
|
} else {
|
|
release();
|
|
}
|
|
}
|
|
}
|
|
|
|
public String getError() {
|
|
return this.error;
|
|
}
|
|
|
|
public void setError(final String error) {
|
|
this.error = error;
|
|
}
|
|
|
|
public String getErrorStackTrace() {
|
|
return this.errorStackTrace;
|
|
}
|
|
|
|
public void setErrorStackTrace(final String errorStackTrace) {
|
|
this.errorStackTrace = errorStackTrace;
|
|
}
|
|
|
|
}
|