Class GProcessTask

java.lang.Object
com.glitchybyte.glib.concurrent.GTask
com.glitchybyte.glib.process.GProcessTask
All Implemented Interfaces:
Runnable

public final class GProcessTask extends GTask
An encapsulation that represents and manages a system process.
  • Constructor Details

    • GProcessTask

      public GProcessTask(String[] command, Path dir, int maxOutputBufferLines)
      Creates a process from the given command and starting directory.
      Parameters:
      command - Array representing the parts of the command.
      dir - Starting directory. If null, it will inherit the current process current directory.
      maxOutputBufferLines - Max lines to keep in memory from console output of this process.
    • GProcessTask

      public GProcessTask(String[] command, Path dir, boolean autoPrintOutput)
      Creates a process from the given command and starting directory.
      Parameters:
      command - Array representing the parts of the command.
      dir - Starting directory. If null, it will inherit the current process current directory.
      autoPrintOutput - This convenience parameter makes it so output of the process gets continually printed to the current process' console.
    • GProcessTask

      public GProcessTask(String[] command, Path dir)
      Convenience constructor to quickly execute a process without any concern for its output.
      Parameters:
      command - Array representing the parts of the command.
      dir - Starting directory. If null, it will inherit the current process current directory.
    • GProcessTask

      public GProcessTask(String[] command)
      Convenience constructor to quickly execute a process without any concern for its output. It will also default to the current process' current directory.
      Parameters:
      command - Array representing the parts of the command.
  • Method Details

    • getProcessHandle

      public ProcessHandle getProcessHandle()
      Returns the process handle to query for other information about the process.
      Returns:
      The process handle to query for other information about the process.
    • run

      public void run()
    • getStatusCode

      public Integer getStatusCode()
      Returns the status code of the finished process, or null if the process was interrupted.
      Returns:
      The status code of the finished process, or null if the process was interrupted.
    • stop

      public void stop(long pid)
      Stops this process or any subprocess of this process.

      The implementation will send a SIGINT to the given pid.

      *IMPORTANT!* This is a convenience for when you need to inform a subprocess you want to exit for orderly shutdown up the chain. For example, an application started by a Windows batch script will not shut down if you send a SIGINT to the batch process.

      Parameters:
      pid - Pid of the descendant process to stop.
    • stop

      public void stop()
      Stops the process.

      The implementation will send a SIGINT.

    • getState

      public GProcessTask.State getState()
      Gets the process state.
      Returns:
      The process state.
    • awaitForState

      public boolean awaitForState(GProcessTask.State state, Duration timeout) throws InterruptedException
      Wait until a given state is reached or timeout expires.
      Parameters:
      state - Wanted state.
      timeout - Time to wait for state.
      Returns:
      True if the state has been reached.
      Throws:
      InterruptedException - If the wait was interrupted.
    • awaitForState

      public void awaitForState(GProcessTask.State state) throws InterruptedException
      Wait indefinitely until a given state is reached.
      Parameters:
      state - Wanted state.
      Throws:
      InterruptedException - If the wait was interrupted.
    • getOutput

      public List<String> getOutput()
      Returns all collected output until this moment. Resets collection buffer.
      Returns:
      All collected lines until this moment.