Jal-3066 Retrieve job log and error log from the server.
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 7 Oct 2019 15:14:58 +0000 (16:14 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 7 Oct 2019 15:14:58 +0000 (16:14 +0100)
j11lib/slivka-client.jar
src/jalview/ws/slivkaws/SlivkaWSInstance.java

index a8e499a..0ee279e 100644 (file)
Binary files a/j11lib/slivka-client.jar and b/j11lib/slivka-client.jar differ
index 9aae388..a75046d 100644 (file)
@@ -10,10 +10,17 @@ import jalview.ws.params.ParamManager;
 
 import java.io.IOError;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
 import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
 
 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
 import uk.ac.dundee.compbio.slivkaclient.JobState;
+import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
 import uk.ac.dundee.compbio.slivkaclient.ValidationException;
@@ -37,6 +44,10 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     stateMap.put(JobState.ERROR, WsJob.JobState.SERVERERROR);
     stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN);
   }
+  protected final Set<WsJob.JobState> failedStates = new HashSet<>(Arrays.asList(
+      WsJob.JobState.INVALID, WsJob.JobState.BROKEN, WsJob.JobState.FAILED,
+      WsJob.JobState.SERVERERROR, WsJob.JobState.CANCELLED
+  ));
 
   public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
   {
@@ -58,11 +69,44 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
   }
 
   @Override
-  public final boolean updateJobProgress(WsJob job)
+  public final boolean updateJobProgress(WsJob job) throws IOException
   {
+    Optional<RemoteFile> logFile = client.getJobResults(job.getJobId()).stream()
+        .filter(f -> f.getLabel() == "log").findFirst();
+    if (logFile.isPresent())
+    {
+      InputStream stream = logFile.get().getContent();
+      long nextChunk = stream.skip(job.getNextChunk());
+      job.setnextChunk(nextChunk + appendJobStatus(job, stream));
+    }
+    if (failedStates.contains(job.getJobState()))
+    {
+      Optional<RemoteFile> errLogFile = client.getJobResults(job.getJobId()).stream()
+          .filter(f -> f.getLabel() == "error-log").findFirst();
+      if (errLogFile.isPresent())
+      {
+        appendJobStatus(job, errLogFile.get().getContent());
+      }
+    }
     return false;
   }
 
+  private int appendJobStatus(WsJob job, InputStream stream) throws IOException
+  {
+    StringBuilder builder = new StringBuilder(job.getStatus());
+    InputStreamReader reader = new InputStreamReader(stream);
+    char[] buffer = new char[4096];
+    int chunkLen = 0;
+    int len = 0;
+    while ((len = reader.read(buffer)) != -1)
+    {
+      chunkLen += len;
+      builder.append(buffer, 0, len);
+    }
+    job.setStatus(builder.toString());
+    return chunkLen;
+  }
+
   @Override
   public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
   {