Class SpawnProcess

java.lang.Object
com.kosdev.kos.core.service.spawn.SpawnProcess
Direct Known Subclasses:
Adapter

public class SpawnProcess extends Object
Class that represents a single process that can be spawned.
Since:
1
Version:
10
  • Field Details

  • Constructor Details

    • SpawnProcess

      public SpawnProcess()
      Create a new SpawnProcess .
      Since:
      1
  • Method Details

    • isDaemon

      public boolean isDaemon()
      Return true if this is a daemon process. Daemon processes are long running and will be automatically restarted if they crash.
      Since:
      10
    • setDaemon

      public SpawnProcess setDaemon(boolean enable)
      Set if this is a daemon process. A daemon process will auto-restart if it crashes and will continue running until stopped via the service. A daemon process is inherently headless, meaning it cannot stream I/O.

      Cannot be changed after the process is started.

      Parameters:
      enable - if true, process will be a daemon process
      Since:
      10
    • isStreaming

      public boolean isStreaming()
      Return true if this process supports I/O streaming back to java. Streaming is disabled by default. Daemon processes are don't allow streaming, and streaming will be set to false when the process is started.
      Since:
      10
    • setStreaming

      public SpawnProcess setStreaming(boolean enable)
      Enable I/O streaming for this process. This allows java applications to write to stdin of the application and read stdout / stderr. This works even in multi-node environments where the spawner may be on a different node. Daemon processes don't support I/O streaming and this flag will be set false when the daemon process starts.

      If streaming is enabled and a log file is specified, stdout/stderr will be directed to the log file. In this case the stdout/stderr input streams will be null. It is still possible to write stdout to the application however.

      Cannot be changed after the process is started.

      Parameters:
      enable - if true, enable I/O streaming support
      Since:
      10
    • setPath

      public SpawnProcess setPath(File file)
      Set the path to the executable.

      Cannot be changed after the process is started.

      Parameters:
      file - the path to the executable
      Since:
      1
    • setPath

      public SpawnProcess setPath(String path)
      Set the path to the executable.

      Cannot be changed after the process is started.

      Parameters:
      path - path to the executable
      Since:
      1
    • setWorkingDir

      public SpawnProcess setWorkingDir(File dir)
      Set the working directory.

      Cannot be changed after the process is started.

      Parameters:
      dir - the working directory
      Since:
      1
    • setWorkingDir

      public SpawnProcess setWorkingDir(String path)
      Set the working directory.

      Cannot be changed after the process is started.

      Parameters:
      path - path to the working directory
      Since:
      1
    • setLogFile

      public SpawnProcess setLogFile(String logFile)
      Redirect stdout/stderr to the specified file name in the /mnt/logs directory. The file must be a valid filename. If a path is specified, it will be stripped off and only the final filename will be used. When set, this file will be created in the log directory and will be subject to standard log processing processes.

      If a log file is specified, I/O streaming of stdout/stderr is disabled.

      Cannot be changed after the process is started.

      Parameters:
      logFile - the name of the log file
      Since:
      1
    • getUser

      public String getUser()
      Return the user that the program should be run as.
      Returns:
      the desired user for the program
      Since:
      1
    • setUser

      public SpawnProcess setUser(String user)
      Set the user (and group) that the program should be run as. This will lookup the specified user and switch to the associated uid and default gid for the user. If this user doesn't exist, the program will fail to spawn. Leave unset or set to null to run as the same user as the adapter (typically root).

      Cannot be changed after the process is started.

      Parameters:
      user - the user used to run the program
      Since:
      1
    • getGroup

      public String getGroup()
      Return the group that the program should be run as.
      Returns:
      the desired group for the program
      Since:
      1
    • setGroup

      public SpawnProcess setGroup(String group)
      Set the group that the program should be run as. If setUser() was called, this will already set the group to the default group for the user. Use this to switch the user to another non-default group. If the group doesn't exist, the program will fail to spawn.

      Cannot be changed after the process is started.

      Parameters:
      group - the group used to run the program
      Since:
      1
    • getListener

      public SpawnProcessListener getListener()
      Return the listener attached to the process.
      Since:
      10
    • setListener

      public void setListener(SpawnProcessListener listener)
      Set the listener to receive lifecycle events from the process.
      Parameters:
      listener - the listener to attach
      Since:
      10
    • addArg

      public SpawnProcess addArg(Object arg)
      Add a command line argument.

      Cannot be changed after the process is started.

      Parameters:
      arg - the argument to add
      Since:
      1
    • setPriority

      public void setPriority(int priority)
      Set the priority of the process. This can only be called until the process is created. After this you must use SpawnService.setPriority().
      Parameters:
      priority - the priority of the process -20..19 (unix semantics)
      Since:
      1
    • getExitStatus

      public SpawnProcess.ExitStatus getExitStatus()
      Return the exit status of the process. If the process is a daemon, it will only be set until process is restarted. Alternately, attach a listener to the process to receive exit callbacks with the exit status information.
      Since:
      10
    • remove

      public void remove()
      Remove the process from the run list, killing the process if it's currently running. This is typically used to remove a daemon process from SpawnService . This is equivalent to calling SpawnService.removeProcess() .
      Since:
      10
    • restart

      public void restart()
      Kill the daemon process and let the service restart it. This can be an effective way to reset a daemon process that is not acting correctly

      This only works for daemon processes. This does nothing for non-daemon processes.

      Since:
      10
    • waitForStart

      public boolean waitForStart(long timeout)
      Wait for the process to start.
      Parameters:
      timeout - how long to wait
      Returns:
      true if started
      Since:
      1
    • getStdinOutputStream

      public OutputStream getStdinOutputStream()
      Return an OutputStream that writes data to stdin of the running process. This allows java to write input to running programs. This will return null until the process has been added to SpawnService .

      Daemon processes don't support I/O streaming. This will return null if the process is a daemon.

      Since:
      10
    • getStdoutInputStream

      public InputStream getStdoutInputStream()
      Return an InputStream that reads from stdout of the running process. This allows java to read output from running programs. This will return null until the process has been added to SpawnService .

      If the process is configured to output to a log file, streaming I/O is not available and this will return null.

      Daemon processes don't support I/O streaming. This will return null if the process is a daemon.

      Since:
      10
    • getStderrInputStream

      public InputStream getStderrInputStream()
      Return an InputStream that reads from stderr of the running process. This allows java to read output from running programs. This will return null until the process has been added to SpawnService .

      If the process is configured to output to a log file, streaming I/O is not available and this will return null.

      Daemon processes don't support I/O streaming. This will return null if the process is a daemon.

      Since:
      10
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getPath

      public String getPath()
    • getWorkingDir

      public String getWorkingDir()
    • getArgs

      public List<String> getArgs()
    • getLogFile

      public String getLogFile()
    • getPid

      public int getPid()
    • getPriority

      public int getPriority()