JAL-3683 retrieve job status to output stream feature/JAL-3686_slivka_client_js_update
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 8 Jul 2020 14:27:01 +0000 (15:27 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 8 Jul 2020 14:27:01 +0000 (15:27 +0100)
src/jalview/ws/slivkaws/SlivkaWSInstance.java

index a21354e..33c1205 100644 (file)
@@ -13,10 +13,10 @@ import jalview.ws.params.ParamManager;
 import jalview.ws.params.WsParamSetI;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 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;
@@ -130,44 +130,37 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
   {
     List<RemoteFile> files = client.getJobResults(job.getJobId());
     Optional<RemoteFile> logFile = files.stream()
-        .filter(f -> f.getLabel().equals("log")).findFirst();
+            .filter(f -> f.getLabel().equals("log")).findFirst();
     boolean newContent = false;
     if (logFile.isPresent())
     {
-      InputStream stream = logFile.get().getContent();
-      long nextChunk = stream.skip(job.getNextChunk());
-      int len = appendJobStatus(job, stream);
-      job.setnextChunk(nextChunk + len);
-      newContent |= len > 0;
+      ByteArrayOutputStream output = new ByteArrayOutputStream();
+      logFile.get().writeTo(output);
+      if (output.size() > job.getNextChunk())
+      {
+        newContent = true;
+        job.setStatus(output.toString("UTF-8"));
+        job.setnextChunk(output.size());
+      }
     }
     if (failedStates.contains(job.getJobState()))
     {
       Optional<RemoteFile> errLogFile = files.stream()
-          .filter(f -> f.getLabel().equals("error-log")).findFirst();
+              .filter(f -> f.getLabel().equals("error-log")).findFirst();
       if (errLogFile.isPresent())
       {
-        newContent |= appendJobStatus(job, errLogFile.get().getContent()) > 0;
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        errLogFile.get().writeTo(output);
+        if (output.size() > 0)
+        {
+          newContent = true;
+          job.setStatus(job.getStatus() + "\n" + output.toString("UTF-8"));
+        }
       }
     }
     return newContent;
   }
 
-  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)
   {