added fields to ScriptStatus

This commit is contained in:
Lucio Lelii 2022-12-15 15:24:30 +01:00
parent 2033a4b79f
commit 0420e2ba3e
2 changed files with 57 additions and 15 deletions

View File

@ -40,6 +40,8 @@ import org.gcube.data.access.storagehub.scripting.AbstractScript;
import org.gcube.data.access.storagehub.scripting.ScriptUtil;
import org.gcube.data.access.storagehub.services.RepositoryInitializer;
import org.gcube.data.access.storagehub.services.admin.ScriptStatus.Status;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.utils.InnerMethodName;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@ -69,7 +71,7 @@ public class ScriptManager {
@Inject
PathUtil pathUtil;
HashMap<String, ScriptStatus> scriptStatusMap = new HashMap<String, ScriptStatus>();
private static HashMap<String, ScriptStatus> scriptStatusMap = new HashMap<String, ScriptStatus>();
@POST
@Path("execute")
@ -98,8 +100,10 @@ public class ScriptManager {
@Produces(MediaType.APPLICATION_JSON)
public ScriptStatus getStatus(@PathParam("id") String runningId) {
InnerMethodName.instance.set("getScriptStatus");
if (!scriptStatusMap.containsKey(runningId))
throw new WebApplicationException("id "+runningId+" not found");
if (!scriptStatusMap.containsKey(runningId)) {
log.error("script with id {} not found",runningId);
throw new WebApplicationException("id "+runningId+" not found", 404);
}
ScriptStatus status = scriptStatusMap.get(runningId);
if (status.getStatus()!= Status.Running)
scriptStatusMap.remove(runningId);
@ -131,20 +135,24 @@ public class ScriptManager {
String parentId = destinationFolderId!=null ? destinationFolderId : ses.getNode(pathUtil.getWorkspacePath(login).toPath()).getIdentifier();
Node parentNode = ses.getNodeByIdentifier(parentId);
String parentPath = parentNode.getPath();
if (AbstractScript.class.isAssignableFrom(clazz)) {
AbstractScript scriptInstance = (AbstractScript) clazz.newInstance();
ApplicationContext appContext = ContextProvider.get();
String serverHost = appContext.container().configuration().hostname();
String resultPath = Paths.append(Paths.getPath(parentNode.getPath()), name).toPath();
String runningId = UUID.randomUUID().toString();
ScriptStatus status = new ScriptStatus(runningId);
ScriptStatus status = new ScriptStatus(runningId, resultPath, serverHost);
RealRun realRun = new RealRun(ses, scriptInstance, login, parentNode, name, true, status);
if (asynch) {
scriptStatusMap.put(runningId, status);
new Thread(AuthorizedTasks.bind(realRun)).start();
return status;
}else {
realRun.run();
status = scriptStatusMap.remove(runningId);
return status;
}
@ -190,7 +198,7 @@ public class ScriptManager {
String result ="";
try {
result = instance.run(ses, null, scriptUtil);
status.setSuccess(Paths.append(Paths.getPath(parentNode.getPath()), name).toPath());
status.setSuccess();
}catch(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);

View File

@ -1,11 +1,17 @@
package org.gcube.data.access.storagehub.services.admin;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ScriptStatus {
enum Status {
Running, Success, Failed
}
private static final DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z") ;
private Status status;
private String errorMessage;
@ -14,24 +20,30 @@ public class ScriptStatus {
private long start;
private long finished = 0;
private long finished = -1;
private String runningId;
public ScriptStatus(String runningId) {
private String executionServer;
public ScriptStatus(String runningId, String resultPath, String executionServer) {
super();
this.status = Status.Running;
this.start = System.currentTimeMillis();
this.runningId = runningId;
this.resultPath = resultPath;
this.executionServer = executionServer;
}
public void setFailed(String error) {
this.status = Status.Failed;
this.errorMessage = error;
this.finished = System.currentTimeMillis();
}
public void setSuccess(String resultPath) {
public void setSuccess() {
this.status = Status.Success;
this.resultPath = resultPath;
this.finished = System.currentTimeMillis();
}
public Status getStatus() {
@ -42,8 +54,27 @@ public class ScriptStatus {
return errorMessage;
}
public long getDuration() {
return finished-start;
public String getStartDate() {
Date date = new Date(this.start);
return dateFormat.format(date);
}
public long getDurationInMillis() {
long toUse = finished;
if (finished < 0)
toUse = System.currentTimeMillis();
return toUse-start;
}
public String getHumanReadableDuration() {
long toUse = finished;
if (finished < 0)
toUse = System.currentTimeMillis();
long minutes = (toUse/1000)/60;
long seconds = (toUse/1000)%60;
return String.format("%d minutes %d seconds", minutes, seconds);
}
public String getResultPath() {
@ -53,6 +84,9 @@ public class ScriptStatus {
public String getRunningId() {
return runningId;
}
public String getExecutionServer() {
return executionServer;
}
}