Class ProcessSupport


  • public class ProcessSupport
    extends java.lang.Object
    Process execution support functions. Process scripts or binaries shall be packaged using a Maven assembly descriptor into a ZIP file in the "root" of the Jar/Service artifact (fallback for testing can be defined, e.g., src/main/python/...). Here, the name is free, but shall not collide with the default process artifacts of generated services.
    Author:
    Holger Eichelberger, SSE
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ProcessSupport.ScriptOwner
      Holds the script context.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.function.Consumer<java.lang.ProcessBuilder> INHERIT_IO
      A simple default customizer always requesting to inherit the process IO (standard in/out/err).
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessSupport()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void callPython​(ProcessSupport.ScriptOwner owner, java.lang.String script, java.util.function.Consumer<java.lang.String> cmdResult, java.lang.String... args)
      Call python scripts, waiting for and killing finally.
      static int callPythonWaitForAndKill​(java.io.File dir, java.lang.String script, java.util.function.Consumer<java.lang.String> cmdResult, java.lang.String resultFile, java.util.function.Consumer<java.lang.ProcessBuilder> cust, java.lang.String... args)
      Call python, wait for ending and kill it if needed.
      static java.lang.Process createPythonProcess​(java.io.File dir, java.lang.String script, java.util.function.Consumer<java.lang.ProcessBuilder> procCustomizer, java.lang.String... args)
      Creates and starts a Python process.
      static int waitForAndKill​(java.lang.Process proc, java.lang.String script, java.util.function.Consumer<java.lang.String> cmdResult, java.lang.String resultFile)
      Wait for a (scripted) process until it is really dead.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INHERIT_IO

        public static final java.util.function.Consumer<java.lang.ProcessBuilder> INHERIT_IO
        A simple default customizer always requesting to inherit the process IO (standard in/out/err). May particularly be helpful for debugging.
    • Constructor Detail

      • ProcessSupport

        public ProcessSupport()
    • Method Detail

      • callPython

        public static void callPython​(ProcessSupport.ScriptOwner owner,
                                      java.lang.String script,
                                      java.util.function.Consumer<java.lang.String> cmdResult,
                                      java.lang.String... args)
        Call python scripts, waiting for and killing finally. Scripts shall be located in a Maven packaged ZIP represented by owner (including unpacking state).
        Parameters:
        owner - the script owner and data instance
        script - the script file name to execute in the context of owner
        cmdResult - a consumer for the result, may be null for none
        args - the process arguments for the script including python arguments (first), script and script arguments
      • createPythonProcess

        public static java.lang.Process createPythonProcess​(java.io.File dir,
                                                            java.lang.String script,
                                                            java.util.function.Consumer<java.lang.ProcessBuilder> procCustomizer,
                                                            java.lang.String... args)
                                                     throws java.io.IOException
        Creates and starts a Python process.
        Parameters:
        dir - the home dir where to find the script/run it within
        script - the script to execute
        procCustomizer - allows to customize the internal process builder, may be null for none
        args - the process arguments for the script including python arguments (first), script and script arguments
        Returns:
        the created process
        Throws:
        java.io.IOException - if process creation fails
      • waitForAndKill

        public static int waitForAndKill​(java.lang.Process proc,
                                         java.lang.String script,
                                         java.util.function.Consumer<java.lang.String> cmdResult,
                                         java.lang.String resultFile)
        Wait for a (scripted) process until it is really dead.
        Parameters:
        proc - the process to wait for
        script - the script name for logging
        cmdResult - a consumer for the result, may be null for none
        resultFile - file to read result from, e.g., short lived processes, if null use standard in
        Returns:
        the process status, -1 if the process was not executed
        See Also:
        AbstractProcessService.redirectIO(InputStream, PrintStream)
      • callPythonWaitForAndKill

        public static int callPythonWaitForAndKill​(java.io.File dir,
                                                   java.lang.String script,
                                                   java.util.function.Consumer<java.lang.String> cmdResult,
                                                   java.lang.String resultFile,
                                                   java.util.function.Consumer<java.lang.ProcessBuilder> cust,
                                                   java.lang.String... args)
        Call python, wait for ending and kill it if needed. Catch all exceptions.
        Parameters:
        dir - the home dir where to find the script/run it within
        script - the script to execute
        cmdResult - a consumer for the result, may be null for none
        resultFile - file to read result from, e.g., short lived processes, if null use standard in
        cust - optional process customizer, may be null for none
        args - the process arguments for the script including python arguments (first), script and script arguments
        Returns:
        the process status, -1 if the process was not executed
        See Also:
        createPythonProcess(File, String, Consumer, String...), waitForAndKill(Process, String, Consumer, String)