respawn.go 2.5 KB

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