JAL-3599 Cater for macOS. ScheduledExecutor timeout implemented
authorBen Soares <b.soares@dundee.ac.uk>
Sun, 11 Jun 2023 18:11:35 +0000 (19:11 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Sun, 11 Jun 2023 18:11:35 +0000 (19:11 +0100)
build.gradle

index 3284b5b..9b173d7 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,23 +4218,29 @@ 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
@@ -4243,41 +4256,70 @@ task jalviewjsLaunchTest(type: Exec) {
   def stdout
   def stderr
   doFirst {
-    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()
-    if (jalviewjs_j2s_to_console.equals("true")) {
-      standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
-        stdout,
-        System.out)
-      errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
-        stderr,
-        System.err)
-    } else {
-      standardOutput = stdout
-      errorOutput = stderr
-    }
-  }
+    def timeoutms = Integer.valueOf(jalviewjs_chromium_timeout) * 1000
+
+    ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
+    final Future handler = executor.submit(
+      () -> {
+
+        exec {
+          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()
+          if (jalviewjs_j2s_to_console.equals("true")) {
+            standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
+              stdout,
+              System.out)
+            errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
+              stderr,
+              System.err)
+          } else {
+            standardOutput = stdout
+            errorOutput = stderr
+          }
+
+
+          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 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}"
-  ])
+      }
+
+    )
+
+    executor.schedule(new Runnable(){
+      public void run(){
+         executor.shutdownNow()
+      }
+    }, timeoutms, TimeUnit.MILLISECONDS);
+
+    executor.scheduleAtFixedRate(
+      () -> {
+        if (stderr.toString().contains(jalviewjs_desktop_init_string)) {
+          Thread.sleep(1000)
+          executor.shutdownNow()
+        }
+      },
+      1, 1, TimeUnit.SECONDS)
+
+    executor.awaitTermination(timeoutms, TimeUnit.MILLISECONDS)
+
+  }
   
   doLast {
     def found = false