JAL-629 Small buglet with getting --seqid
[jalview.git] / build.gradle
index 3284b5b..7ad6f99 100644 (file)
@@ -10,6 +10,10 @@ import org.gradle.plugins.ide.eclipse.model.Output
 import org.gradle.plugins.ide.eclipse.model.Library
 import java.security.MessageDigest
 import java.util.regex.Matcher
+import java.util.concurrent.Executors
+import java.util.concurrent.Future
+import java.util.concurrent.ScheduledExecutorService
+import java.util.concurrent.TimeUnit
 import groovy.transform.ExternalizeMethods
 import groovy.util.XmlParser
 import groovy.xml.XmlUtil
@@ -574,7 +578,10 @@ ext {
   eclipseBinary = string("")
   eclipseVersion = string("")
   eclipseDebug = false
-  
+
+  jalviewjsChromiumUserDir = "${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}"
+  jalviewjsChromiumProfileDir = "${ext.jalviewjsChromiumUserDir}/${jalviewjs_chromium_profile_name}"
+
   // ENDEXT
 }
 
@@ -4211,26 +4218,32 @@ task jalviewjsCopyStderrLaunchFile(type: Copy) {
   outputs.file jalviewjsStderrLaunchFilename
 }
 
+task cleanJalviewjsChromiumUserDir {
+  doFirst {
+    delete jalviewjsChromiumUserDir
+  }
+  outputs.dir jalviewjsChromiumUserDir
+  // always run when depended on
+  outputs.upToDateWhen { !file(jalviewjsChromiumUserDir).exists() }
+}
+
 task jalviewjsChromiumProfile {
-  def userDir = "${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}"
-  def profileDir = "${userDir}/${jalviewjs_chromium_profile_name}"
-  def firstRun = file("${userDir}/First Run")
+  dependsOn cleanJalviewjsChromiumUserDir
+  mustRunAfter cleanJalviewjsChromiumUserDir
 
-  doFirst {
-    // clear out old profile
-    delete profileDir
+  def firstRun = file("${jalviewjsChromiumUserDir}/First Run")
 
-    mkdir profileDir
+  doFirst {
+    mkdir jalviewjsChromiumProfileDir
     firstRun.text = ""
   }
-  
   outputs.file firstRun
 }
 
-task jalviewjsLaunchTest(type: Exec) {
+task jalviewjsLaunchTest {
   group "Test"
   description "Check JalviewJS opens in a browser"
-//  dependsOn jalviewjsBuildSite
+  dependsOn jalviewjsBuildSite
   dependsOn jalviewjsCopyStderrLaunchFile
   dependsOn jalviewjsChromiumProfile
 
@@ -4243,41 +4256,91 @@ task jalviewjsLaunchTest(type: Exec) {
   def stdout
   def stderr
   doFirst {
+    def timeoutms = Integer.valueOf(jalviewjs_chromium_overall_timeout) * 1000
+    
     def binary = file(chromiumBinary)
     if (!binary.exists()) {
       throw new StopExecutionException("Could not find chromium binary '${chromiumBinary}'. Cannot run task ${name}.")
     }
-    
     stdout = new ByteArrayOutputStream()
     stderr = new ByteArrayOutputStream()
+    def execStdout
+    def execStderr
     if (jalviewjs_j2s_to_console.equals("true")) {
-      standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
+      execStdout = new org.apache.tools.ant.util.TeeOutputStream(
         stdout,
         System.out)
-      errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
+      execStderr = new org.apache.tools.ant.util.TeeOutputStream(
         stderr,
         System.err)
     } else {
-      standardOutput = stdout
-      errorOutput = stderr
+      execStdout = stdout
+      execStderr = stderr
     }
-  }
+    def execArgs = [
+      "--no-sandbox", // --no-sandbox IS USED BY THE THORIUM APPIMAGE ON THE BUILDSERVER
+      "--headless=new",
+      "--disable-gpu",
+      "--timeout=${timeoutms}",
+      "--virtual-time-budget=${timeoutms}",
+      "--user-data-dir=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}",
+      "--profile-directory=${jalviewjs_chromium_profile_name}",
+      "--allow-file-access-from-files",
+      "--enable-logging=stderr",
+      "file://${jalviewDirAbsolutePath}/${jalviewjsStderrLaunchFilename}"
+    ]
+    
+    if (true || macOS) {
+      ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
+      Future f1 = executor.submit(
+        () -> {
+          exec {
+            standardOutput = execStdout
+            errorOutput = execStderr
+            executable(chromiumBinary)
+            args(execArgs)
+            println "COMMAND: '"+commandLine.join(" ")+"'"
+          }
+          executor.shutdownNow()
+        }
+      )
 
-  def timeoutms = Integer.valueOf(jalviewjs_chromium_timeout) * 1000
-
-  executable(chromiumBinary)
-  args([
-    "--no-sandbox", // --no-sandbox IS USED BY THE THORIUM APPIMAGE ON THE BUILDSERVER
-    "--headless=new",
-    "--disable-gpu",
-    "--timeout=${timeoutms}",
-    "--virtual-time-budget=${timeoutms}",
-    "--user-data-dir=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}",
-    "--profile-directory=${jalviewjs_chromium_profile_name}",
-    "--allow-file-access-from-files",
-    "--enable-logging=stderr",
-    "file://${jalviewDirAbsolutePath}/${jalviewjsStderrLaunchFilename}"
-  ])
+      def noChangeBytes = 0
+      def noChangeIterations = 0
+      executor.scheduleAtFixedRate(
+        () -> {
+          String stderrString = stderr.toString()
+          // shutdown the task if we have a success string
+          if (stderrString.contains(jalviewjs_desktop_init_string)) {
+            f1.cancel()
+            Thread.sleep(1000)
+            executor.shutdownNow()
+          }
+          // if no change in stderr for 10s then also end
+          if (noChangeIterations >= jalviewjs_chromium_idle_timeout) {
+            executor.shutdownNow()
+          }
+          if (stderrString.length() == noChangeBytes) {
+            noChangeIterations++
+          } else {
+            noChangeBytes = stderrString.length()
+            noChangeIterations = 0
+          }
+        },
+        1, 1, TimeUnit.SECONDS)
+
+      executor.schedule(new Runnable(){
+        public void run(){
+          f1.cancel()
+          executor.shutdownNow()
+        }
+      }, timeoutms, TimeUnit.MILLISECONDS)
+
+      executor.awaitTermination(timeoutms+10000, TimeUnit.MILLISECONDS)
+      executor.shutdownNow()
+    }
+
+  }
   
   doLast {
     def found = false