respawn.go 2.3 KB

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