respawn.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package respawn
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "os/signal"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "syscall"
  12. "time"
  13. "github.com/codegangsta/cli"
  14. "github.com/rancher/os/log"
  15. )
  16. var (
  17. running = true
  18. processes = map[int]*os.Process{}
  19. processLock = sync.Mutex{}
  20. )
  21. func Main() {
  22. log.InitLogger()
  23. runtime.GOMAXPROCS(1)
  24. runtime.LockOSThread()
  25. app := cli.NewApp()
  26. app.Flags = []cli.Flag{
  27. cli.StringFlag{
  28. Name: "file, f",
  29. Usage: "Optional config file to load",
  30. },
  31. }
  32. app.Action = run
  33. app.Run(os.Args)
  34. }
  35. func setupSigterm() {
  36. sigtermChan := make(chan os.Signal)
  37. signal.Notify(sigtermChan, syscall.SIGTERM)
  38. go func() {
  39. for range sigtermChan {
  40. termPids()
  41. }
  42. }()
  43. }
  44. func run(c *cli.Context) error {
  45. setupSigterm()
  46. var stream io.Reader = os.Stdin
  47. var err error
  48. inputFileName := c.String("file")
  49. if inputFileName != "" {
  50. stream, err = os.Open(inputFileName)
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. }
  55. input, err := ioutil.ReadAll(stream)
  56. if err != nil {
  57. panic(err)
  58. }
  59. var wg sync.WaitGroup
  60. for _, line := range strings.Split(string(input), "\n") {
  61. if strings.TrimSpace(line) == "" || strings.Index(strings.TrimSpace(line), "#") == 0 {
  62. continue
  63. }
  64. wg.Add(1)
  65. go execute(line, &wg)
  66. }
  67. wg.Wait()
  68. return nil
  69. }
  70. func addProcess(process *os.Process) {
  71. processLock.Lock()
  72. defer processLock.Unlock()
  73. processes[process.Pid] = process
  74. }
  75. func removeProcess(process *os.Process) {
  76. processLock.Lock()
  77. defer processLock.Unlock()
  78. delete(processes, process.Pid)
  79. }
  80. func termPids() {
  81. running = false
  82. processLock.Lock()
  83. defer processLock.Unlock()
  84. for _, process := range processes {
  85. process.Signal(syscall.SIGTERM)
  86. }
  87. }
  88. func execute(line string, wg *sync.WaitGroup) {
  89. defer wg.Done()
  90. start := time.Now()
  91. count := 0
  92. for {
  93. args := strings.Split(line, " ")
  94. cmd := exec.Command(args[0], args[1:]...)
  95. cmd.Stdout = os.Stdout
  96. cmd.Stderr = os.Stderr
  97. cmd.SysProcAttr = &syscall.SysProcAttr{
  98. Setsid: true,
  99. }
  100. err := cmd.Start()
  101. if err != nil {
  102. log.Errorf("%s : %v", line, err)
  103. }
  104. if err == nil {
  105. addProcess(cmd.Process)
  106. err = cmd.Wait()
  107. removeProcess(cmd.Process)
  108. }
  109. if err != nil {
  110. log.Errorf("%s : %v", line, err)
  111. }
  112. if !running {
  113. log.Infof("%s : not restarting, exiting", line)
  114. break
  115. }
  116. count++
  117. if count > 10 {
  118. if start.Sub(time.Now()) <= (1 * time.Second) {
  119. log.Errorf("%s : restarted too fast, not executing", line)
  120. break
  121. }
  122. count = 0
  123. start = time.Now()
  124. }
  125. }
  126. }