123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package rpcore
- import (
- "io"
- "os"
- "os/exec"
- "sync"
- "syscall"
- "time"
- )
- type CmdInfoClass struct {
- // Name will pass to exec.Command()
- Name string
- // Args will pass to exec.Command()
- Args []string
- // Env specifies the environment of the process.
- // Each entry is of the form "key=value".
- // If Env is nil, the new process uses the current process's
- // environment.
- // If Env contains duplicate environment keys, only the last
- // value in the slice for each duplicate key is used.
- // As a special case on Windows, SYSTEMROOT is always added if
- // missing and not explicitly set to the empty string.
- Env []string
- // Dir specifies the working directory of the command.
- // If Dir is the empty string, Run runs the command in the
- // calling process's current directory.
- Dir string
- // Stdin specifies the process's standard input.
- //
- // If Stdin is nil, the process reads from the null device (os.DevNull).
- //
- // If Stdin is an *os.File, the process's standard input is connected
- // directly to that file.
- //
- // Otherwise, during the execution of the command a separate
- // goroutine reads from Stdin and delivers that data to the command
- // over a pipe. In this case, Wait does not complete until the goroutine
- // stops copying, either because it has reached the end of Stdin
- // (EOF or a read error), or because writing to the pipe returned an error,
- // or because a nonzero WaitDelay was set and expired.
- Stdin io.Reader
- // Stdout and Stderr specify the process's standard output and error.
- //
- // If either is nil, Run connects the corresponding file descriptor
- // to the null device (os.DevNull).
- //
- // If either is an *os.File, the corresponding output from the process
- // is connected directly to that file.
- //
- // Otherwise, during the execution of the command a separate goroutine
- // reads from the process over a pipe and delivers that data to the
- // corresponding Writer. In this case, Wait does not complete until the
- // goroutine reaches EOF or encounters an error or a nonzero WaitDelay
- // expires.
- //
- // If Stdout and Stderr are the same writer, and have a type that can
- // be compared with ==, at most one goroutine at a time will call Write.
- Stdout io.Writer
- Stderr io.Writer
- // ExtraFiles specifies additional open files to be inherited by the
- // new process. It does not include standard input, standard output, or
- // standard error. If non-nil, entry i becomes file descriptor 3+i.
- //
- // ExtraFiles is not supported on Windows.
- ExtraFiles []*os.File
- // SysProcAttr holds optional, operating system-specific attributes.
- // Run passes it to os.StartProcess as the os.ProcAttr's Sys field.
- SysProcAttr *syscall.SysProcAttr
- // If WaitDelay is non-zero, it bounds the time spent waiting on two sources
- // of unexpected delay in Wait: a child process that fails to exit after the
- // associated Context is canceled, and a child process that exits but leaves
- // its I/O pipes unclosed.
- //
- // The WaitDelay timer starts when either the associated Context is done or a
- // call to Wait observes that the child process has exited, whichever occurs
- // first. When the delay has elapsed, the command shuts down the child process
- // and/or its I/O pipes.
- //
- // If the child process has failed to exit — perhaps because it ignored or
- // failed to receive a shutdown signal from a Cancel function, or because no
- // Cancel function was set — then it will be terminated using os.Process.Kill.
- //
- // Then, if the I/O pipes communicating with the child process are still open,
- // those pipes are closed in order to unblock any goroutines currently blocked
- // on Read or Write calls.
- //
- // If pipes are closed due to WaitDelay, no Cancel call has occurred,
- // and the command has otherwise exited with a successful status, Wait and
- // similar methods will return ErrWaitDelay instead of nil.
- //
- // If WaitDelay is zero (the default), I/O pipes will be read until EOF,
- // which might not occur until orphaned subprocesses of the command have
- // also closed their descriptors for the pipes.
- WaitDelay time.Duration
- }
- type ChildProcTableItem struct {
- ID int64
- Name string
- ProcessID int
- IsDaemon bool
- Status ProcStatus
- CmdInfo *CmdInfoClass
- Cmd *exec.Cmd
- LastStartTime time.Time
- Enabled bool
- TaskDoneCallback TaskDoneCallbackFunc
- TaskExecTimeout time.Duration
- ShutdownTimeout time.Duration
- ShutdownActor GracefulShutdownActor
- sync.Mutex
- }
- func GetExecCmdFromCmdInfo(cmdInfo *CmdInfoClass) *exec.Cmd {
- c := exec.Command(cmdInfo.Name, cmdInfo.Args...)
- if cmdInfo.Env != nil && len(cmdInfo.Env) > 0 {
- c.Env = cmdInfo.Env
- }
- if cmdInfo.Dir != "" {
- c.Dir = cmdInfo.Dir
- }
- if cmdInfo.Stdin != nil {
- c.Stdin = cmdInfo.Stdin
- }
- if cmdInfo.Stdout != nil {
- c.Stdout = cmdInfo.Stdout
- }
- if cmdInfo.Stderr != nil {
- c.Stderr = cmdInfo.Stderr
- }
- if cmdInfo.ExtraFiles != nil && len(cmdInfo.ExtraFiles) > 0 {
- c.ExtraFiles = cmdInfo.ExtraFiles
- }
- if cmdInfo.SysProcAttr != nil {
- c.SysProcAttr = cmdInfo.SysProcAttr
- }
- if cmdInfo.WaitDelay > 0 {
- c.WaitDelay = cmdInfo.WaitDelay
- }
- return c
- }
|