cp_item.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package rpcore
  2. import (
  3. "io"
  4. "os"
  5. "os/exec"
  6. "sync"
  7. "syscall"
  8. "time"
  9. )
  10. type CmdInfoClass struct {
  11. // Name will pass to exec.Command()
  12. Name string
  13. // Args will pass to exec.Command()
  14. Args []string
  15. // Env specifies the environment of the process.
  16. // Each entry is of the form "key=value".
  17. // If Env is nil, the new process uses the current process's
  18. // environment.
  19. // If Env contains duplicate environment keys, only the last
  20. // value in the slice for each duplicate key is used.
  21. // As a special case on Windows, SYSTEMROOT is always added if
  22. // missing and not explicitly set to the empty string.
  23. Env []string
  24. // Dir specifies the working directory of the command.
  25. // If Dir is the empty string, Run runs the command in the
  26. // calling process's current directory.
  27. Dir string
  28. // Stdin specifies the process's standard input.
  29. //
  30. // If Stdin is nil, the process reads from the null device (os.DevNull).
  31. //
  32. // If Stdin is an *os.File, the process's standard input is connected
  33. // directly to that file.
  34. //
  35. // Otherwise, during the execution of the command a separate
  36. // goroutine reads from Stdin and delivers that data to the command
  37. // over a pipe. In this case, Wait does not complete until the goroutine
  38. // stops copying, either because it has reached the end of Stdin
  39. // (EOF or a read error), or because writing to the pipe returned an error,
  40. // or because a nonzero WaitDelay was set and expired.
  41. Stdin io.Reader
  42. // Stdout and Stderr specify the process's standard output and error.
  43. //
  44. // If either is nil, Run connects the corresponding file descriptor
  45. // to the null device (os.DevNull).
  46. //
  47. // If either is an *os.File, the corresponding output from the process
  48. // is connected directly to that file.
  49. //
  50. // Otherwise, during the execution of the command a separate goroutine
  51. // reads from the process over a pipe and delivers that data to the
  52. // corresponding Writer. In this case, Wait does not complete until the
  53. // goroutine reaches EOF or encounters an error or a nonzero WaitDelay
  54. // expires.
  55. //
  56. // If Stdout and Stderr are the same writer, and have a type that can
  57. // be compared with ==, at most one goroutine at a time will call Write.
  58. Stdout io.Writer
  59. Stderr io.Writer
  60. // ExtraFiles specifies additional open files to be inherited by the
  61. // new process. It does not include standard input, standard output, or
  62. // standard error. If non-nil, entry i becomes file descriptor 3+i.
  63. //
  64. // ExtraFiles is not supported on Windows.
  65. ExtraFiles []*os.File
  66. // SysProcAttr holds optional, operating system-specific attributes.
  67. // Run passes it to os.StartProcess as the os.ProcAttr's Sys field.
  68. SysProcAttr *syscall.SysProcAttr
  69. // If WaitDelay is non-zero, it bounds the time spent waiting on two sources
  70. // of unexpected delay in Wait: a child process that fails to exit after the
  71. // associated Context is canceled, and a child process that exits but leaves
  72. // its I/O pipes unclosed.
  73. //
  74. // The WaitDelay timer starts when either the associated Context is done or a
  75. // call to Wait observes that the child process has exited, whichever occurs
  76. // first. When the delay has elapsed, the command shuts down the child process
  77. // and/or its I/O pipes.
  78. //
  79. // If the child process has failed to exit — perhaps because it ignored or
  80. // failed to receive a shutdown signal from a Cancel function, or because no
  81. // Cancel function was set — then it will be terminated using os.Process.Kill.
  82. //
  83. // Then, if the I/O pipes communicating with the child process are still open,
  84. // those pipes are closed in order to unblock any goroutines currently blocked
  85. // on Read or Write calls.
  86. //
  87. // If pipes are closed due to WaitDelay, no Cancel call has occurred,
  88. // and the command has otherwise exited with a successful status, Wait and
  89. // similar methods will return ErrWaitDelay instead of nil.
  90. //
  91. // If WaitDelay is zero (the default), I/O pipes will be read until EOF,
  92. // which might not occur until orphaned subprocesses of the command have
  93. // also closed their descriptors for the pipes.
  94. WaitDelay time.Duration
  95. }
  96. type ChildProcTableItem struct {
  97. ID int64
  98. Name string
  99. ProcessID int
  100. IsDaemon bool
  101. Status ProcStatus
  102. CmdInfo *CmdInfoClass
  103. Cmd *exec.Cmd
  104. LastStartTime time.Time
  105. Enabled bool
  106. TaskDoneCallback TaskDoneCallbackFunc
  107. TaskExecTimeout time.Duration
  108. ShutdownTimeout time.Duration
  109. ShutdownActor GracefulShutdownActor
  110. sync.Mutex
  111. }
  112. func GetExecCmdFromCmdInfo(cmdInfo *CmdInfoClass) *exec.Cmd {
  113. c := exec.Command(cmdInfo.Name, cmdInfo.Args...)
  114. if cmdInfo.Env != nil && len(cmdInfo.Env) > 0 {
  115. c.Env = cmdInfo.Env
  116. }
  117. if cmdInfo.Dir != "" {
  118. c.Dir = cmdInfo.Dir
  119. }
  120. if cmdInfo.Stdin != nil {
  121. c.Stdin = cmdInfo.Stdin
  122. }
  123. if cmdInfo.Stdout != nil {
  124. c.Stdout = cmdInfo.Stdout
  125. }
  126. if cmdInfo.Stderr != nil {
  127. c.Stderr = cmdInfo.Stderr
  128. }
  129. if cmdInfo.ExtraFiles != nil && len(cmdInfo.ExtraFiles) > 0 {
  130. c.ExtraFiles = cmdInfo.ExtraFiles
  131. }
  132. if cmdInfo.SysProcAttr != nil {
  133. c.SysProcAttr = cmdInfo.SysProcAttr
  134. }
  135. if cmdInfo.WaitDelay > 0 {
  136. c.WaitDelay = cmdInfo.WaitDelay
  137. }
  138. return c
  139. }