// keep a service alive and restart it if needed.
// stop restarting the service if it's failing too quickly many times in a row.
keep a service alive and restart it if needed.
stop restarting the service if it's failing too quickly many times in a row.
// keep a service alive and restart it if needed . // stop restarting the service if it ' s failing too quickly many times in a row .
func keepservicealive(f serviceFn, name string, port string, wg *sync.WaitGroup, quit <-chan struct{}) (chan struct{}, <-chan error) {
	err := make(chan error)
	ready := make(chan struct{})
	Debug.Printf("Starting %s watcher", name)
	wg.Add(1)
	go func() {
		defer Debug.Printf("%s watcher stopped", name)
		defer wg.Done()
		defer close(err)
		n := 0
		for {
			start := time.Now()
			e := f(port, ready, quit)
			end := time.Now()
			select {
			case <-quit:
				Debug.Printf("Quit %s watcher as requested", name)
				return
			default:
				if n > maxRestart-1 {
					Debug.Printf("%s did fail starting many times, returning an error", name)
					err <- e
					return
				}
			}
			if end.Sub(start) < time.Duration(10*time.Second) {
				n++
				Debug.Printf("%s failed to start, restart #%d.", name, n)
			} else {
				n = 0
				Debug.Printf("%s failed, but not immediately, reset as first restart.", name)
			}
		}
	}()
	return ready, err
}
func keepservicealive ( f serviceFn , name string , port string , wg * sync . WaitGroup , quit <- chan struct { } ) ( chan struct { } , <- chan error ) { err := make ( chan error ) ready := make ( chan struct { } ) Debug . Printf ( " " , name ) wg . Add ( 1 ) go func ( ) { defer Debug . Printf ( " " , name ) defer wg . Done ( ) defer close ( err ) n := 0 for { start := time . Now ( ) e := f ( port , ready , quit ) end := time . Now ( ) select { case <- quit : Debug . Printf ( " " , name ) return default : if n > maxRestart - 1 { Debug . Printf ( " " , name ) err <- e return } } if end . Sub ( start ) < time . Duration ( 10 * time . Second ) { n ++ Debug . Printf ( " " , name , n ) } else { n = 0 Debug . Printf ( " " , name ) } } } ( ) return ready , err }
keep a service alive and restart it if needed.
keep a service alive and restart it if needed .
// check for quitting request